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

Discord.py Cog模块下embed反应触发命令无法运行问题咨询

问题原因及修复方案

核心错误点

  • 装饰器使用错误:Cog内的命令不能使用@bot.command注册,必须使用@commands.command装饰器,原写法放在Cog类中时命令不会被注册,无法被触发。
  • bot实例引用错误:代码中await bot.wait_for()直接调用全局bot实例,在Cog中需要通过类属性引用实例,需要替换为await self.bot.wait_for()。

常见遗漏配置

  • Cog类必须继承discord.ext.commands.Cog
  • 需要在Cog初始化方法中接收bot实例并绑定到self属性:
from discord.ext import commands

class 自定义Cog类名(commands.Cog):
    def __init__(self, bot):
        self.bot = bot # 绑定bot实例供类内方法调用
  • 机器人启动前需要完成Cog加载,写法参考:
# 主启动文件中加载Cog
await bot.add_cog(自定义Cog类名(bot))

修正后的命令代码

@commands.command(aliases=['test','t'])
async def Test(self, ctx):
    TestEmbed = discord.Embed(title="Definition", description="This is just a test embed", color=11027200)
    TestEmbed.add_field(name="\u200b", value="▹❁❁▹❁◃❁❁◃",inline=False)
    emojis = ['⬅️','➡️']

    TestEmbedAdd = await ctx.send(embed=TestEmbed)
    for emoji in emojis:
        await TestEmbedAdd.add_reaction(emoji)

    def check(reaction, user):
        return user == ctx.author and str(reaction.emoji) in ['⬅️', '➡️']

    try:
        reaction, user = await self.bot.wait_for('reaction_add', timeout=5, check=check)
        if reaction.emoji == '➡️':
            await ctx.send("你触发了向右箭头反应!")
        elif reaction.emoji == '⬅️':
            await ctx.send("你触发了向左箭头反应!")

    except asyncio.TimeoutError:
        await ctx.send("等待超时!")

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 15:00:04