Discord.py音乐机器人checkqueue命令有歌曲时无法发送Embed问题排查
问题根因
- 核心错误:
checkqueue命令中构造Embed时使用了不存在的discord.colour(小写c)类,正确写法是首字母大写的discord.Colour(英式拼写)或者discord.Color(美式拼写)。你其他命令比如search里用的是大写开头的discord.Colour.blue()可以正常运行,checkqueue里写错了大小写,导致代码运行到此处抛出AttributeError,无法后续发送Embed。 - 次要问题1:
stop命令逻辑错误,await ctx.voice_client.disconnect()之前加了return,导致后续发送"Disconnected"提示的代码永远不会执行。 - 次要问题2:仅在
on_ready事件中初始化服务器队列,机器人运行后新加入的服务器没有对应队列初始化,会触发KeyError报错。 - 注意点:你当前的
play逻辑中,第一首播放的歌曲不会存入队列,只有正在播放歌曲时新加的歌曲才会进队列。如果只添加了一首歌曲正在播放,此时队列本身就是空的,属于正常逻辑。
修复代码
1. 修复checkqueue命令的颜色类大小写错误
@commands.command() async def checkqueue(self, ctx): if len(self.song_queue[ctx.guild.id]) == 0: return await ctx.send("Queue is empty") else: # 把discord.colour改成discord.Colour embed = discord.Embed(title="Song Queue", description="", colour=discord.Colour.blue()) i = 1 for url in self.song_queue[ctx.guild.id]: embed.description += f"{i}) {url}\n" i += 1 await ctx.send(embed=embed)
2. 修复stop命令的无效代码
@commands.command() async def stop(self, ctx): if ctx.voice_client is not None: await ctx.voice_client.disconnect() return await ctx.send("Disconnected") else: return await ctx.send("I am not connected to a voice channel")
3. 新增新加入服务器的队列初始化逻辑
在Cog中新增监听函数:
@commands.Cog.listener() async def on_guild_join(self, guild): self.song_queue[guild.id] = []
内容的提问来源于stack exchange,提问作者dogluva
相关产品推荐
相关产品推荐

