Python开发的Discord Bot无法响应命令,请求技术协助
问题分析与解决方案
你的代码存在两个核心问题,导致Bot无法响应命令:
1. on_message 函数返回逻辑错误
在on_message函数中,当检测到消息作者是机器人时,你使用了return message——这不仅毫无必要(on_message作为事件回调不需要返回值),更会直接终止函数执行,使得后续的await bot.process_commands(message)根本无法运行。
修正后的on_message函数:
@bot.event async def on_message(message: discord.Message) -> None: if message.author == bot.user or message.author.bot: return # 直接终止函数,无需返回内容 await bot.process_commands(message)
2. 未启用message_content意图
Discord.py v2.0及以上版本要求,读取消息内容必须明确开启message_content特权意图,否则Bot无法获取消息文本,自然识别不了命令前缀。
修复步骤:
- 登录Discord开发者平台,进入你的Bot应用页面,在
Privileged Gateway Intents中开启Message Content Intent - 在代码中补充意图设置:
intents = discord.Intents.default() intents.members = True intents.message_content = True # 新增该行开启消息内容意图
验证操作
完成上述修改后重启Bot,测试$ping命令:
- 确认Bot在对应频道拥有发送消息权限
- 检查命令前缀确实为
$,无输入错误
内容的提问来源于stack exchange,提问作者KaliUser
相关产品推荐
相关产品推荐

