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

时间:2020-11-07 08:46:52

标签: python-3.x discord.py discord.py-rewrite

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

这里是我当前的代码:

@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.')

当我在语音通道中时,它会加入并发送消息,但是当我不在语音通道中时,它将在终端中返回此错误:

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

1 个答案:

答案 0 :(得分:1)

Member.voice将为“无”,您需要检查

下面是修改后的代码:

@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.')