Yusuf Abbas
Yusuf Abbas

Reputation: 21

Discord py join voice command when no one is in a voice channel

I had a question about the join command for discord.py. I have this join function, which works fine, but I wanted specific detection for situations where a user enters the command while not in a VC.

I have a message for if it joins successfully, and then the other option is if someone enters the command while already in a VC. I just need an elif for if someone enters the command without being in a voice call. Not sure how to go about this, any help would be appreciated.

  voiceChannel = ctx.author.voice.channel
  voice = discord.utils.get(bot.voice_clients, guild = ctx.guild)
  if voice == None:
    await voiceChannel.connect()
    await ctx.send(f"Joined **{voiceChannel}**")\
  
  else:
    await ctx.send("I'm already in a VC")```

Upvotes: 0

Views: 195

Answers (1)

Dominik
Dominik

Reputation: 3602

The error which is given out is an AttributeError when the user is not in a channel.

You have to rebuild your code like this:

@client.command() / @bot.command() / @commands.command()
async def join(ctx):
    try: # Build in a try
    [Your Code here]
    except AttributeError: # If not in a voice channel 
        return await ctx.send("You have to be in a channel to do that!")
  • [Your Code here] = Just insert your if and else code with the correct indentation.
  • Depending on the method used, you may need to modify the command and add a self.

Upvotes: 0

Related Questions