Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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:
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading