Search code examples
discorddiscord.pybots

How to fix TypeError: unsupported type annotation <class 'discord.ext.commands.context.Context'>?


I'm coding a Discord bot using discord.py 2.3.2 with the Interactions API. When running my snippet of code:

@commands.hybrid_command(name='help', with_app_command=True)
async def help(self, ctx: commands.Context):
    await ctx.reply("Test was good!")

I get the error

line 65, in <module>
    async def help(self, ctx: commands.Context):
TypeError: unsupported type annotation <class 'discord.ext.commands.context.Context'>

What is causing this and how should I go about fixing it?

I have done my best to follow the Interactions API documentation, to no avail. The error I'm getting is in a line copied directly from a doc example. My command

@bot.tree.command()
async def test(interaction: discord.Interaction) -> None:
    await interaction.response.send_message("test successful")

works just fine, but not the hybrid command.


Solution

  • From what I understood in the comments you are not using a Cog, thatswhy you can't use the self keyword and so the first paramater would be Context. Also change your decorator to

    @bot.hybrid_command(name="help", with_app_command=True)
    

    So your final code would look like this:

    @bot.hybrid_command(name="help", with_app_command=True)
    async def help(ctx: commands.Context):
        await ctx.reply("Test was good!")