discord.py 如果作者不在语音频道中,请发送消息

discord.py send a message if author isnt in a voice channel

我希望我的机器人在我键入命令时不在语音通道中时发送消息。

这是我当前的代码:

@client.command()
async def play(ctx):
    channel = ctx.author.voice.channel 
    if channel:
        await channel.connect() 
        await ctx.send('Joining voicechat.')
    elif channel is None:
        await ctx.send('You have to be in a voice channel first.')

当我在语音频道时它加入并发送消息,但当我不在时,它returns终端出现这个错误:

Command raised an exception: AttributeError: 'NoneType' object has no attribute 'channel'

Member.voice 将是 None,您需要检查

修改后的代码如下:

@client.command()
async def play(ctx):
    channel = ctx.author.voice
    if channel:
        await channel.channel.connect() 
        await ctx.send('Joining voicechat.')
    else:
        await ctx.send('You have to be in a voice channel first.')