I've decided to try making my discord bot play music, but I've gotten stuck already. Mainly due to the fact I can't find any sources to help with the current version, I've been winging everything from the docs. However, I can't figure out how to check if the bot is connected to a voice channel.
I have tried if not Client.is_connected():
, however that didn't work. If there are any updated sources to help me get the basics of discord.py's voice features, please give me a link :) Here is my code so far:
# ----- ATTEMPT AT VOICE COMMANDS ------
#discord.opus.load_opus() - what goes in bracket???
@client.command(name="join", pass_ctx=True)
async def join(ctx):
#if not is_connected(): - Client.is_connected() not working
user = ctx.message.author
vc = user.voice.channel
await vc.connect()
await ctx.send(f"Joined **{vc}**")
#else:
# await ctx.send("I'm already connected!")
@client.command(name="disconnect", pass_ctx=True)
async def disconnect(ctx):
# if not is_connected(): - once again can't work it out
vc = ctx.message.guild.voice_client # i don't even know how this worked :D
await vc.disconnect()
#else:
# await ctx.send("I'm not connected to any channels")
@client.command(name="play", pass_ctx=True)
async def play(ctx, songurl=None):
if not songurl: # this works at least
await ctx.send("Please specify a song")
return
if not is_connected(): # once again, how to check if bot is connected?
vc = ctx.message.author.voice.channel
if not vc: # i think this should work
await ctx.send("You're not in a voice channel!")
await vc.connect()
# haven't even worked out anything past this point and it's broken
ps: sorry for just dumping my whole vc section but i don't understand a lot
Really all that matters here is the play command, but I included the others just because (as you can see from my comments) I don't understand LOTS of what is going on. How should I go about this? Are there any good sources for the current version? Thanks in advance.
The . some() method will return true if the member and the bot is in the same channel, false otherwise. So you can use it like this: const inSameChannel = client.
At the moment, the only way to restrict bots to one channel only is to manually remove the bot's chat permissions in each channel that you don't want it in. The more channels you have in a server, the more tedious it becomes.
A bot can be connected to voice in multiple guilds at the same time, so you need to get the VoiceClient
for the appropriate guild from Client.voice_clients
and then check VoiceClient.is_connected
:
def is_connected(ctx):
voice_client = get(ctx.bot.voice_clients, guild=ctx.guild)
return voice_client and voice_client.is_connected()
you could also do
client.command()
async def join(ctx):
user = ctx.message.author
vc = user.voice.channel
voice = discord.utils.get(client.voice_clients, guild=ctx.guild) # This allows for more functionality with voice channels
if voice == None: # None being the default value if the bot isnt in a channel (which is why the is_connected() is returning errors)
await vc.connect()
await ctx.send(f"Joined **{vc}**")
else:
await ctx.send("I'm already connected!")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With