如果我在键入命令时不在语音通道中,我希望我的机器人发送一条消息。
这里是我当前的代码:
@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'
答案 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.')