Tree command 2 arguments

Question:

I have seen bots like Dyno that are able to have 2 arguments in one slash command, I would like to do the same thing with a DM slash command that would be something like this: /dm @user Hello!

I have tried using this code in discord.py:

@tree.command(guild=discord.Object(id=guild_id), name='userping', description='DM the user to come back to the ticket')
@app_commands.describe(user="The user you want the DM to be sent to", reason="The reason to ping this user")
@app_commands.default_permissions(manage_channels=True)
@app_commands.checks.cooldown(3, 20, key=lambda i: (i.guild_id, i.user.id))
@app_commands.checks.bot_has_permissions(manage_channels=True)
async def userping(interaction: discord.Interaction, reason: str, user = discord.Member):
    if "ticket-for-" in interaction.channel.name:
        l=1
    else:
        await interaction.response.send_message("This isn't a ticket!", ephemeral=True)

It returns:

Traceback (most recent call last):
  File "/Users/Leo/Documents/code/Discord/TicketBot/discord_bot_python/bot2.py", line 215, in <module>
    async def userping(interaction: discord.Interaction, reason: str, user = discord.Member):
  File "/Users/Leo/Library/Python/3.9/lib/python/site-packages/discord/app_commands/tree.py", line 887, in decorator
    command = Command(
  File "/Users/Leo/Library/Python/3.9/lib/python/site-packages/discord/app_commands/commands.py", line 665, in __init__
    self._params: Dict[str, CommandParameter] = _extract_parameters_from_callback(callback, callback.__globals__)
  File "/Users/Leo/Library/Python/3.9/lib/python/site-packages/discord/app_commands/commands.py", line 370, in _extract_parameters_from_callback
    raise TypeError(f'parameter {parameter.name!r} is missing a type annotation in callback {func.__qualname__!r}')
TypeError: parameter 'user' is missing a type annotation in callback 'userping'
Asked By: Lor Lor

||

Answers:

"a type annotation" is like you did with reason; reason: str. With user = discord.Member, you are assigning user to the object discord.Member. To put it simply, change the = to :.

# Code shortened for readability of error correction
@tree.command(guild=discord.Object(id=guild_id), name='userping', description='DM the user to come back to the ticket')
async def userping(interaction: discord.Interaction, reason: str, user: discord.Member):
...
Answered By: Raymus
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.