Answer the question
In order to leave comments, you need to log in
How to make the discord bot command work only if the person is in voice chat (python)?
Hello, I am writing a music bot for discord and now I am writing basic commands to 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
Here is my current code:
@bot.command()
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 the question
In order to leave comments, you need to log in
As always, read the documentation.
Member.voice can contain either None or a VoiceState object . And VoiceState, in turn, has a channel field .
Guess next?
If not, one approach is to check the members field of the received VoiceChannel object. But it is possible in another way.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question