1 | initial version |
To make your Discord bot confirm whether or not it is already present in a voice channel, you can use the following code:
@client.command(name="join")
async def join(ctx):
if ctx.author.voice:
channel = ctx.author.voice.channel
if ctx.voice_client is None:
await channel.connect()
await ctx.send(f"Joined {channel}")
else:
await ctx.voice_client.move_to(channel)
await ctx.send(f"Moved to {channel}")
else:
await ctx.send("You are not in a voice channel.")
This code checks if the author issuing the command is in a voice channel. If the author is in a voice channel, then the bot checks if it is already connected to a voice channel. If the bot is not already connected, it will join the channel and send a confirmation message. If the bot is already connected to a voice channel, it will move to the channel of the author and send a confirmation message. If the author is not in a voice channel, the bot will send a message indicating that they are not in a voice channel.