【问题标题】:How to tell if the user is not in a voice channel如何判断用户是否不在语音频道中
【发布时间】:2021-01-31 00:04:04
【问题描述】:

如果用户不在语音频道中,我正在尝试让机器人发送错误消息。这是我到目前为止尝试过的方法,但它不起作用。

    @commands.command()
    async def join(self, ctx):
        channel = ctx.message.author.voice.channel
        voice = get(self.client.voice_clients, guild=ctx.guild)
        try:
            if voice and voice.is_connected():
                await voice.move_to(channel)
                await ctx.send(f'Moved to {channel}')
            else:
                voice = await channel.connect()
                await ctx.send(f'Connected to {channel}')
        except:
            await ctx.send('You are not in a voice channel.')

【问题讨论】:

  • “不起作用”是什么意思?你有任何错误吗?您是否打印了任何变量以查看它们包含的内容?我最好的猜测是 voice 实际上是 None 当他们不在频道中时,所以你不能请求它的 channel 属性。

标签: python asynchronous discord.py


【解决方案1】:

您必须使用Member.voice 来检查它们是否连接到语音通道。

以下是修改后的代码:

@commands.command()
async def join(self, ctx):
    if not ctx.author.voice:
        return await ctx.send('You are not in a voice channel.')
    channel = ctx.message.author.voice.channel
    voice = ctx.voice_client
    if voice and voice.is_connected():
        await voice.move_to(channel)
        await ctx.send(f'Moved to {channel}')
    else:
        voice = await channel.connect()
        await ctx.send(f'Connected to {channel}')

【讨论】:

  • 谢谢。这就是我一直在寻找的。 @Poojan
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-30
  • 1970-01-01
  • 2021-10-22
  • 1970-01-01
  • 1970-01-01
  • 2021-09-08
相关资源
最近更新 更多