discord.py TypeError: unsupported type annotation <class 'discord.interactions.Interaction'>

Question:

Original code here:

@app_commands.command(name='clearmsg', description="clear the number of messages you want.")
@app_commands.rename(num='quantity')
@app_commands.describe(num='For how many messages you would like to delete (In Arabic numerals).')    
async def clear(self, ctx, num : int, interaction:discord.Interaction):
   await ctx.channel.purge(limit=num)
   await interaction.response.send_message(f"{num} messages were cleared successfully.", embed=EmbedTemplate, ephemeral=True)

I was trying to make a slash command let the bot delete certain number of messages

/clearmsg {quantity} to delete {quantity} message

But the error raised as below:

TypeError: unsupported type annotation <class 'discord.interactions.Interaction'>

or trying to ignore the interaction argument:

@app_commands.command(name='clearmsg', description="clear the number of messages you want.")
@app_commands.rename(num='quantity')
@app_commands.describe(num='For how many messages you would like to delete (In Arabic numerals).')
async def clear(self, ctx, num : int):
  await ctx.channel.purge(limit=num)
  await ctx.respond(f"{num} messages were cleared successfully.", embed=EmbedTemplate, ephemeral=True)

another error raised as below:

<class 'discord.app_commands.errors.CommandInvokeError'>: Command 'clearmsg' raised an exception: AttributeError: 'Interaction' object has no attribute 'respond'

are there any ways to fix this while slash command, embed, and ephemeral still remain?

Asked By: Lefengisbee

||

Answers:

The parameters are wrong. The first parameter must be of type discord.Interaction. Also, you need to use type annotation on all parameters, because the API needs to know what the user is allowed/needs to type in as an argument.

Change the function definition to this:

async def clear(self, interaction: discord.Interaction, num: int):

The 2nd error is caused by ctx.respond. Interaction doesn’t have a method called respond, neither does Context, but it has an attribute response which returns an InteractionResponse object. And this object has a method called send_message.

So, the end result would look like that:

@app_commands.command(name='clearmsg', description="clear the number of messages you want.")
@app_commands.rename(num='quantity')
@app_commands.describe(num='For how many messages you would like to delete (In Arabic numerals).')
async def clear(self, interaction: discord.Interaction, num: int):
  await interaction.channel.purge(limit=num)
  await interaction.response.send_message(f"{num} messages were cleared successfully.", embed=EmbedTemplate, ephemeral=True)

Here are a few more examples: https://gist.github.com/AbstractUmbra/a9c188797ae194e592efe05fa129c57f#file-extension_no_group-py


References:
Answered By: puncher
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.