Error in Discord bot due to missing parameter type annotation (Python)

Encountering type annotation error in my Discord bot command

I’m developing a Discord bot and I keep hitting a roadblock. Each time I launch the bot, it shows an error indicating that one of the parameters in my command function lacks a type annotation.

The exact error reads: TypeError: parameter 'reason' is missing a type annotation in callback 'moderation.kick'

Here’s the code I’m currently using:

@app_commands.command(name='kick', description='Kicks a member from the server if you have the necessary permissions.')
@has_permissions(kick_members=True)
async def kick(self, interaction: discord.Interaction, member: discord.Member, *, reason='None'):
    await interaction.response.send_message(f'User {member} has been kicked.')
    await member.kick(reason=reason)

@kick.error
async def kick_error(self, interaction: discord.Interaction, error):
    if isinstance(error, commands.MissingPermissions):
        await interaction.response.send_message('You lack the permissions to kick members!')

I assumed that having a default string value would suffice, but it seems that isn’t the case. I even tried to incorporate a return type annotation, but that didn’t resolve the issue. What am I overlooking here? The reason parameter already has a default string value, so I’m puzzled by the need for further annotation.

Had the exact same problem when working on my moderation bot last month. The solution is adding the type annotation to your reason parameter. Discord’s app_commands framework requires explicit type hints for all parameters, regardless of whether they have default values or not. Your line should be *, reason: str = 'None' instead of just *, reason='None'. This happens because the slash command system needs to know the exact parameter types to properly register commands with Discord’s API. Without the type annotation, the framework cannot determine how to handle the parameter during command registration. Once you add the type hint, the error should disappear completely. I also recommend considering reason: str = None and handling the None case in your logic instead of using the string ‘None’ as default, but that depends on your specific use case.

The issue stems from Discord.py’s strict requirement for type annotations on all command parameters when using app_commands. Even though you provided a default value, the library still needs explicit type information for proper command registration and validation. Change your parameter declaration from *, reason='None' to *, reason: str = 'None'. This tells Discord exactly what type of data to expect for that parameter. I encountered this same problem when migrating from traditional commands to slash commands. The app_commands framework is much more strict about type hints compared to the old command system. Without proper annotations, Discord cannot generate the appropriate slash command interface on their end. Also consider using Optional[str] = None instead of defaulting to the string ‘None’ if you want to handle cases where no reason is actually provided.

yh, totally get ur frustation! the error is about the missing type hint. try changing ur code to reason: str = 'None'. discord.py wants clear type annotations for all parameters, even with defaults. hope this helps!