gpt4 book ai didi

python - Discord Bot 不会告诉某人加入语音 channel ,而是会引发错误。其他部分完全正常

转载 作者:行者123 更新时间:2023-12-04 22:51:39 29 4
gpt4 key购买 nike

我已经为我的 Discord 机器人开发此功能已有一段时间了,终于启动并运行了大部分功能,但我遇到了“连接到语音 channel ”警告的问题。代码段如下:

 @commands.command(name="play", help="Plays a selected song from youtube")
async def p(self, ctx, *args):
query = " ".join(args)

voice_channel = ctx.message.author.voice.channel
if voice_channel is None:
#you need to be connected so that the bot knows where to go
await message.channel.send("Connect to a voice channel!")
else:
song = self.search_yt(query)
if type(song) == type(True):
await ctx.send("Could not download the song. Incorrect format try another keyword. This could be due to playlist or a livestream format.")
else:
await ctx.send("Song added to the queue")
self.music_queue.append([song, voice_channel])
现在,除了“如果 voice_channel 为 None”部分之外,这一切似乎都运行良好。不断抛出错误说“NoneType”对象没有属性“ channel ”。
我确信这相当简单,并且我理解问题的要点(显然不是我第一次看到“NoneType”对象错误,但我似乎无法弄清楚我需要在这里更改什么。
正如我之前所说,只要我在语音 channel 中,一切都会完美无缺。机器人连接、下载和播放音乐。

最佳答案

如果用户不在语音 channel 中,语音将为无,因此将没有属性“语音”。
尝试:

voice_channel = ctx.message.author.voice.channel if ctx.message.author.voice else None
https://discordpy.readthedocs.io/en/stable/api.html#discord.VoiceState

关于python - Discord Bot 不会告诉某人加入语音 channel ,而是会引发错误。其他部分完全正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70790400/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com