I am trying to write a slash command version of my help command, and I want to check if a user can run a command.
This is my code here:
@app_commands.command(name="help")
async def help(self, interaction: discord.Interaction, input: str = None):
embed = discord.Embed(title="Test")
cmd = self.bot.get_command(input)
ctx: commands.Context = await self.bot.get_context(interaction)
try:
await cmd.can_run(ctx)
embed.add_field(name="Usable by you:", value="Yes")
except commands.CommandError as exc:
embed.add_field(name="Usable by you:", value=f"No:\n{exc}")
await ctx.send(embed=embed)
Now this works fine if I want to check for a normal command (commands.Command), however this does not work with Hybrid Commands (commands.HybridCommand). I read the docs for bot.get_context where it says:
In order for the custom context to be used inside an interaction-based context (such as HybridCommand) then this method must be overridden to return that class.
However I am not sure what this means exactly. How do I override it and what class do I need to return?
For completions sake, here is the error I get when I try to use this on a HybridCommand:
discord.app_commands.errors.CommandInvokeError: Command 'help' raised an exception: AttributeError: '_MissingSentinel' object has no attribute 'guild'
Again, when I use this on a normal command it works fine.
I would greatly appreciate any pointer!
Edit: An example of what I mean:
@commands.command()
@commands.has_permissions(administrator=True)
async def test1(self, ctx):
print("Test1")
@commands.hybrid_command()
@commands.has_permissions(administrator=True)
async def test2(self, ctx):
print("Test 2")
If you input /help test1, it will work fine, but error out on /help test2. Without the permission check it seems to also work fine.