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

Discord.py无法加载Cog:自定义test命令无响应问题求助

排查Discord机器人Cog中Test命令无响应的问题

以下是针对问题的具体排查步骤:

  • 启用Message Content Intent
    Discord.py 2.0+版本要求必须开启message_content权限才能读取用户消息中的命令。先在Discord开发者门户的机器人页面开启该Intent,再在代码中配置:

    intents = discord.Intents.default()
    intents.message_content = True
    bot = commands.Bot(command_prefix='%', intents=intents)
    
  • 检查Cog内命令的写法
    确保Cog中的命令正确绑定到类实例,装饰器和参数都没写错:

    from discord.ext import commands
    
    class MyCog(commands.Cog):
        def __init__(self, bot):
            self.bot = bot
    
        @commands.command()
        async def test(self, ctx):
            await ctx.send("Test命令已触发")
    

    注意不能用全局的@bot.command()装饰Cog内的命令,必须用@commands.command(),且方法第一个参数必须是self。

  • 确认扩展加载方式正确
    推荐使用setup_hook异步加载扩展,避免同步加载导致的问题:

    async def setup_hook():
        await bot.load_extension('mycog')  # 确保mycog.py和main.py在同一目录
    
    bot.setup_hook = setup_hook
    

    不要在bot.start()前直接同步调用bot.load_extension(),这会破坏异步流程。

  • 验证命令前缀匹配
    确认机器人初始化时设置的command_prefix是%,和你调用时的%test完全一致,前缀不匹配会导致命令无法被识别。

  • 排查命令冲突或禁用情况
    检查是否有其他全局命令或Cog使用了test命令名,导致被覆盖;同时确认代码中没有bot.remove_command('test')这类禁用命令的语句。

内容的提问来源于stack exchange,提问作者hiraki

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.08 07:50:18