【问题标题】:Does not work Context Menus in cog - Discord.pycog 中的上下文菜单不起作用 - Discord.py
【发布时间】:2022-12-03 13:22:34
【问题描述】:

我想在我的机器人中创建一个上下文菜单。例如,他拿了文档code

@app_commands.context_menu(name='react')
async def react_(self, interaction: discord.Interaction, message: discord.Message):
    await interaction.response.send_message('Very cool message!', ephemeral=True)

但是在控制台启动代码时,出现如下错误:TypeError: context menus cannot be defined inside a class。我怎样才能解决这个问题?

【问题讨论】:

    标签: discord.py


    【解决方案1】:

    您不能使用装饰器在 Cogs 中创建上下文菜单,如 Danny here 所解释的那样。

    在 cogs 中创建它们的快速方法是使用 app_commands.ContextMenu 类来创建它们。像这样:

    class MyCog(commands.Cog):
        def __init__(self, bot: commands.Bot) -> None:
            self.bot = bot
            self.ctx_menu = app_commands.ContextMenu(
                name='Cool Command Name',
                callback=self.my_cool_context_menu, # set the callback of the context menu to "my_cool_context_menu"
            )
            self.bot.tree.add_command(self.ctx_menu) # add the context menu to the tree
    
        async def my_cool_context_menu(self, interaction, message):
            ...
    

    您可以查看 Danny 的解释以获取更多信息。

    【讨论】:

      猜你喜欢
      • 2022-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多