import discord
from discord.ext import commands
from discord import FFmpegPCMAudio
import requests
import json
from apikeys import *
intents = discord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix = '!', intents=intents)
#client.event
async def on_ready():
print("The bot is now ready for use!")
print("-----------------------------")
#client.command(pass_context = True)
async def join(ctx):
if (ctx.author.voice):
channel = ctx.message.author.voice.channel
voice = await channel.connect()
source = FFmpegPCMAudio("song.wav")
player = voice.play(source)
else:
await ctx.send("You are not in a voice channel")
#client.command(pass_context = True)
async def leave(ctx):
if (ctx.voice_client):
await ctx.guild.voice_client.disconnect()
await ctx.send("I left the voice channel")
else:
await ctx.send("I am not in a voice channel")
client.run(BOTTOKEN)
Hey! I have recently started watching a tutorial series.
It works for the tutorial maker, but doesn't for me.
The bot is able to join the voice channel the user is in, but never starts playing "song.wav".
Sadly Visual Studio Code also doesn't output any Error messages.
Does anyone know a solution?
Related
in the following code of a discod bot (wrote in python) the FFmpegPCMAudio is not playing, the code and the mp3 audio are in the same folder, also, yesterday the bot worked well, but today when i write the command the bot enters the voice chat, but it doesn't play the mp3 audio, why?
import discord as ds
from discord.ext import commands
from discord import FFmpegPCMAudio
intents = ds.Intents.all()
bot = commands.Bot(command_prefix='$', intents = intents)
#bot.event
async def on_ready():
print('bot online.')
#bot.command(pass_context = True)
async def outro(ctx):
if (ctx.author.voice):
channel = ctx.message.author.voice.channel
voce = await channel.connect()
source = FFmpegPCMAudio('outro.mp3')
player = voce.play(source)
else:
await ctx.send('not in a voice chat')
#bot.command(pass_context = True)
async def esci(ctx):
if (ctx.voice_client):
await ctx.guild.voice_client.disconnect()
await ctx.send('adios #everyone')
else:
ctx.send('not in a voice chat')
bot.get_command('outro')
bot.get_command('esci')
bot.run('MTAwNDA0MjQ1MjcxMTI0NzkwNQ.GjUNFS.LecuRh7QuWxrdpK-lqyH3npMUzCiIyzehCQfjU')
If you're running this on Replit, it's because Replit no longer supports ffmpeg. If not, then you don't have it installed properly on your system.
I just started learning python recently and never coded a discord bot before. I've been trying to get the bot to connect to the voice or respond to ">hello", but it doesn't respond to my command and I don't know how to fix it. I'm using replit as hosting.
import discord
import os
from discord.ext import commands
intents = discord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix = '>', intents=intents)
#client.event
async def on_ready():
print('Log in as {0.user}'.format(client))
print('-------------------')
#client.command()
async def hello(ctx):
await ctx.send("Hello Im Deemo Bot")
#client.command(pass_context = True)
async def join(ctx):
if (ctx.author.voice):
channel = ctx.message.author.voice.channel
await channel.connect()
else:
await ctx.send('You are not in a voice channel')
#client.command(pass_context = True)
async def leave(ctx):
if (ctx.voice_client):
await ctx.guild.voice_client.disconnect()
await ctx.send('Deemo left voice')
else:
await ctx.send('Deemo is not in voice')
keep_alive()
client.run(os.environ['TOKEN'])
I'd like to write a bot with the ability to join; This is my code
import json
import discord
from discord.ext import commands
JSON_FILE = r"C:\JSON PATH"
bot = commands.Bot(command_prefix = "~")
with open(JSON_FILE, "r") as json_file:
TOKEN = json.load(json_file)["token"]
GUILD = json.load(json_file)["guild"]
bot = commands.Bot(command_prefix = "~")
#bot.event
async def on_ready():
print(f"{bot.user.name} launched and has arrived for further use")
#bot.command(name = "join")
async def join(ctx, voice_channel: commands.VoiceChannelConverter):
await voice_channel.connect()
await ctx.send(f"I have joined: {voice_channel}")
#bot.command(name = "leave")
async def leave(ctx):
server = ctx.message.server
voice_client = bot.voice_client_int(server)
await voice_client.disconnect()
bot.run(TOKEN)
It can join usual channels but it cannot join channels in categories. Any advice on how to do that?
Thank you in advance
The problem is probably, that you are using an outdated version of discord.py since you are using server, which is deprecated since v1.0, and not guild. If you update it, it will probably work, because it worked for me too when I tried it.
To code it to automatically join the voice channel of the user executing the command, just use ctx.author.voice.channel:
#bot.command(name = "join")
async def join(ctx, voice_channel: commands.VoiceChannelConverter):
if not ctx.author.voice is None:
await ctx.author.voice.channel.connect()
await ctx.send(f"I have joined your voice channel.")
else:
await ctx.send("You are not in a voice channel!")
References:
discord.Member.voice
Server is deprecated
I'm trying to connect my discord bot to a voice channel, but it's not working.
There isn't any error or anything, nothing happens when I do !join on my discord channel.
Here is my code. I tried looking for some tutorials, but most seems outdated.
Could someone help me?
import discord
from discord.ext import commands
from discord.utils import get
import os
token = os.environ.get('DISCORD_TOKEN')
client = commands.Bot(command_prefix='!')
#client.command(pass_context=True)
async def join(ctx):
channel = ctx.message.author.voice.channel
voice = get(client.voice_clients, guild=ctx.guild)
if voice and voice.is_connected():
await voice.move_to(channel)
else:
voice = await channel.connect()
await ctx.send(f'Joined {channel}')
client.run(token)
Edit:
import discord
from discord.ext import commands
from discord.utils import get
import os
import youtube_dl
token = os.environ.get('DISCORD_TOKEN')
client = commands.Bot(command_prefix='!')
#client.event
async def on_message(message):
channels = ['bot-commands']
if str(message.channel) in channels:
if message.content == '!help':
embed = discord.Embed(title='How to use the ImposterBot', description='Useful Commands')
embed.add_field(name='!help', value='Display all the commands')
embed.add_field(name='!music', value='Play a music playlist')
embed.add_field(name='!valorant', value='Get the most recent version of Valorant stats')
embed.add_field(name='!hello', value='Greet a user')
await message.channel.send(content=None, embed=embed)
elif message.content == '!hello':
await message.channel.send(f'Greeting, {message.author}!')
elif message.content == '!valorant':
await message.channel.send('This feature is not ready yet')
elif message.content == '!music':
await message.channel.send('This feature is not ready yet')
elif 'sustin' in message.content:
await message.channel.send(f"""it's sustin... {"<:monkas:392806765789446144>"} """)
#client.command(pass_context=True)
async def join(ctx):
channel = ctx.message.author.voice.channel
voice = get(client.voice_clients, guild=ctx.guild)
if voice and voice.is_connected():
await voice.move_to(channel)
else:
voice = await channel.connect()
await ctx.send(f'Joined {channel}')
client.run(token)
The error in this code is that commands will not be invoked by the bot if there is an on_message event, unless that event begins with this line:
async def on_message(message):
await bot.process_commands(message)
So I started working on a music bot and I keep getting this when I try to leave. Joining a voice channel works fine.
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'NoneType' object has no attribute 'disconnect'
And my code is
#client.command(name = "join",
pass_context = True)
async def join(ctx):
channel = ctx.message.author.voice.voice_channel
await client.join_voice_channel(channel)
print("Bot joined the voice channel")
#client.command(name = "leave",
pass_context = True)
async def leave(ctx):
server = ctx.message.server
voice_client = client.voice_client_in(server)
await voice_client.disconnect()
print("Bot left the voice channel")
Also im using the following libraries:
import discord
from discord.ext.commands import Bot
from discord.ext import commands
from discord import Game
import random
If you don't have a VoiceClient in that server, then Client.voice_client_in will return None. You should check for this before attempting to disconnect.
#client.command(name = "leave",
pass_context = True)
async def leave(ctx):
server = ctx.message.server
voice_client = client.voice_client_in(server)
if voice_client:
await voice_client.disconnect()
print("Bot left the voice channel")
else:
print("Bot was not in channel")
The fixed version with the input from #PatrickHaugh is:
Imports:
import discord
from discord.ext.commands import Bot
from discord.ext import commands
from discord import Game
from ctypes.util import find_library
import random
from discord import opus
import nacl
import asyncio
Code:
#client.command(name = "join",
pass_context = True)
async def join(ctx):
opus_path = find_library('opus')
discord.opus.load_opus(opus_path)
if not opus.is_loaded():
print('Opus was not loaded')
else:
channel = ctx.message.author.voice.voice_channel
await client.join_voice_channel(channel)
print("Bot joined the voice channel")
#client.command(name = "check",
pass_context = True)
async def check(ctx):
server = ctx.message.server
if client.is_voice_connected(server):
print("Yes")
else:
print("No")
#client.command(pass_context = True)
async def leave(ctx):
for x in client.voice_clients:
if(x.server == ctx.message.server):
return await x.disconnect()
return await client.say("I am not connected to any voice channel on this server!")