-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Get an error when use context_menu in cogs #7823
Comments
It is not supported to define these within a class, they must be free standing functions. You can call |
Context menus do not support group contexts (e.g.
This is kind of ironic but this is one of those things that is actually significantly easier to do yourself rather than the library generalising it to fit to everyone's needs. For example, in a cog you can do the following: 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,
)
self.bot.tree.add_command(self.ctx_menu)
async def cog_unload(self) -> None:
self.bot.tree.remove_command(self.ctx_menu.name, type=self.ctx_menu.type)
# You can add checks too
@app_commands.checks.has_permissions(ban_members=True)
# @app_commads.guilds(12345)
async def my_cool_context_menu(self, interaction: discord.Interaction, message: discord.Message) -> None:
await interaction.response.send_message('hello...') This lets you manually add the context menu to the bot tree when a cog is created. The Python runtime handles the self binding for you as well, rather than the library. |
Thanks for the explanation |
…ure how I got it working before. Regardless, removed all decorators and registered the command manually in the cog init function. Since I'm not using decorators, also included a function to remove the commands when the cog is unloaded. Credit: solution based on code by Rapptz that can be found here: Rapptz/discord.py#7823 (comment)
But how do I handle the error? class MyCog(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.command()
async def echo(self, ctx, arg):
await ctx.send(arg)
@echo.error
async def echo_error(self, ctx, error):
await ctx.send("There was an error")
if isinstance(error, command.MissingRequiredArgument):
await ctx.send("MIssing Required Argument")
else:
raise error in this case 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,
)
self.bot.tree.add_command(self.ctx_menu)
@app_commands.checks.has_permissions(ban_members=True)
async def my_cool_context_menu(self, interaction: discord.Interaction, message: discord.Message) -> None:
await interaction.response.send_message('hello...')
@my_cool_context_menu.error
async def my_cool_context_menu_error(self, interaction: discord.Interaction, error: Exception) -> None:
await interaction.response.send_message('error...') This will report the error AttributeError: 'function' object has no attribute 'error' |
You would do |
oh,thanks |
Sorry - I know this issue is closed and the discussion is somewhat old, but I couldn't find much info in doing research. How would one define multiple context menu interactions in one single cog? |
You can definitely do more than one per Cog class. A small example: class MyCog(commands.Cog):
def __init__(self, bot: commands.Bot, /) -> None:
self.bot: commands.Bot = bot
self.ctx_one = app_commands.ContextMenu(name="Context Menu 1", callback=self.ctx_one_callback)
self.bot.tree.add_command(self.ctx_one)
self.ctx_two = app_commands.ContextMenu(name="Context Menu 2", callback=self.ctx_two_callback)
self.bot.tree.add_command(self.ctx_two)
def cog_unload(self) -> None:
# remove both on cog unload to not clog up the tree incorrectly
self.bot.tree.remove_command(self.ctx_one.name, type=self.ctx_one.type)
self.bot.tree.remove_command(self.ctx_two.name, type=self.ctx_two.type) Just define the callbacks as normal. |
Awesome, thanks Umbra! |
Summary
error for context menu
Reproduction Steps
I get this error when I use the
context_menu
in Cogs.Minimal Reproducible Code
Expected Results
I expected the menu to work properly and get to its parameters.
Actual Results
i get this error:
Extension 'cogs.qanda' raised an error: TypeError: context menu callbacks require 2 parameters, the first one being the annotation and the other one explicitly annotated with either discord.Message, discord.User, discord.Member, or a typing.Union of discord.Member and discord.User
Intents
all of them
System Information
Checklist
Additional Context
I looked for a solution on the library's Discord server but not found answer.
The text was updated successfully, but these errors were encountered: