Home>

Hello, I'm writing a discord music bot and now I'm writing basic commands for it. I want to make the bot execute commands like below only if the user is in the same voice chat with it. Question: how to implement it? I tried to do it below, but eventually noticed that the command works if the user is in ANY voice chat. Question: how to fix this and make the bot react only to the commands of the person who is in the same voice with him? Thanks in advance

async def skip(ctx):
    """Пропускает песню и начинает играть следующую в очереди"""
    voice= discord.utils.get(bot.voice_clients, guild=server)
    if not ctx.author.voice is None:
        if voice.is_playing():
            voice.stop()
        else:
            await ctx.channel.send(f'{ctx.author.mention}, ничего не воспроизводится')
    else:
        await ctx.channel.send(f'{ctx.author.mention}, Вы должны находиться в голосовом канале, чтобы использовать эту команду')
  • Answer # 1
    async def skip(ctx):
        """Skips song and starts playing next in line"""
        user= bot id
        voice= discord.utils.get(bot.voice_clients, guild=server)
        if ctx.author.voice and ctx.author.voice.channel== ctx.user.voice and ctx.user.voice.channel:
            if voice.is_playing():
                voice.stop()
            else:
                await ctx.channel.send(f'{ctx.author.mention}, nothing is playing')
        else:
            await ctx.channel.send(f'{ctx.author.mention}, You must be in a voice channel to use this command')
    

    I changedif not ctx.author.voice is None:on the if ctx.author.voice and ctx.author.voice.channel== ctx.user.voice and ctx.user.voice.channel:and addeduser= id bot

    In theory, this should help you!

  • Answer # 2
    async def skip(ctx):
        """Skips song and starts playing next in line"""
        user= bot id
        voice= discord.utils.get(bot.voice_clients, guild=server)
        if ctx.author.voice and ctx.author.voice.channel== ctx.user.voice and ctx.user.voice.channel:
            if voice.is_playing():
                voice.stop()
            else:
                await ctx.channel.send(f'{ctx.author.mention}, nothing is playing')
        else:
            await ctx.channel.send(f'{ctx.author.mention}, You must be in a voice channel to use this command')
    

    I changedif not ctx.author.voice is None:on the if ctx.author.voice and ctx.author.voice.channel== ctx.user.voice and ctx.user.voice.channel:and addeduser= id bot

    In theory, this should help you!