discord.py执行addqueue命令报50006无法发送空消息错误如何解决
问题修复方案
核心错误原因
- 错误1:
list.append()是原地修改方法,无返回值。你执行queue = queue.append(xxx)后,queue 会被赋值为None,这是触发空消息报错的直接原因。 - 错误2:你将
queue = []定义在命令函数内部,每次触发命令都会重置队列,无法累计存储之前的请求;且f字符串里错误调用了ctx.author.send方法,你实际需要的是用户标识,应该用ctx.author.id或ctx.author.name。 - 错误3:
discord.py的send方法中embed参数必须传入discord.Embed类的实例,不能直接传入列表对象,类型不匹配。
修复代码示例
import discord from discord.ext import commands # 队列定义在命令函数外部,实现全局持久化存储 queue = [] client = commands.Bot(command_prefix='!', intents=discord.Intents.all()) @client.command(name='addqueue', pass_context=True) async def addqueue(ctx,*,reason:str): # 直接调用append修改原列表,不要重新赋值 queue.append(f'{ctx.author.id}: {reason}') print(f'{ctx.author.id}: {reason}\n') # 构造Embed实例再发送,此处示例是把整个队列内容展示在Embed描述中 embed = discord.Embed(title="已添加到队列", description="\n".join(queue), color=0x00ff00) await ctx.channel.send(embed=embed)
可选优化说明
- 如果你不需要展示整个队列,只需要提示单条添加成功,就不用拼接整个队列到Embed里,直接把刚添加的单条内容放进去即可。
- 如果要避免队列无限增长,可以加长度限制,超过长度后自动移除最早的条目。
内容的提问来源于stack exchange,提问作者Yash Sharma
相关产品推荐
相关产品推荐

