我写了一个机器人来播放音乐,但它总是打印和错误:
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'NoneType' object has no attribute 'is_connected'
这是我的功能:
async def play(ctx, url):
if ctx.message.author.voice:
channel = ctx.message.author.voice.channel
voice = discord.utils.get(client.voice_clients, guild=ctx.guild)
if not voice.is_connected():
await channel.connect()
我认为错误是由这个函数产生的:
voice.is_connected()
不知道为什么它不起作用......
答案 0 :(得分:1)
我认为这行得通
channel = ctx.message.author.voice.channel
voice = discord.utils.get(ctx.guild.voice_channels, name=channel.name)
voice_client = discord.utils.get(self.client.voice_clients, guild=ctx.guild)
if voice_client == None:
await voice.connect()
else:
await voice_client.move_to(channel)
答案 1 :(得分:0)
这一行:
texttoencode.Text = new string( texttoencode.Text.SelectMany( c => new char[] { c, ' '}).ToArray());
正在返回 voice = discord.utils.get(client.voice_clients, guild=ctx.guild)
,这意味着 None
中根本没有任何内容,或者它们都不符合 client.voice_clients
条件。在这种情况下,可能是前者,因为我猜你的机器人还没有连接到任何语音频道,所以 guild
是空的。
在 client.voice_clients
为 voice
时稍作修改:
None