discord.py 2.0中Cogs加载正常但无法响应命令问题
解决Discord.py Cog命令无响应问题
问题根源
你在bot.py中自定义了on_message事件,但没有调用await bot.process_commands(message)——这会覆盖Discord.py默认的命令处理逻辑,导致所有命令(包括Cog中的ping)无法被识别和执行。
解决方案
修改bot.py中的on_message事件,在函数末尾添加命令处理调用:
@bot.event async def on_message(message): if message.author == bot.user: return username = str(message.author) user_message = str(message.content) channel = str(message.channel) print(f"{username} said: '{user_message}' ({channel})") # 添加这行,让bot处理命令 await bot.process_commands(message)
可选检查点
- 确认
cogs文件夹与bot.py在同一目录下,且ping.py确实在cogs文件夹内 - 可在
on_ready事件中添加代码,验证Cog是否成功加载:@bot.event async def on_ready(): print(f'{bot.user} is now running.') print(f"已加载的Cog列表: {list(bot.cogs.keys())}")
内容的提问来源于stack exchange,提问作者user16393861
相关产品推荐
相关产品推荐

