You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 19:10:18