discord.py how to add required text/attachment on a slash command

Question:

I am in the process of updating a discord bot and i want to make it so it has slash commands instead of using the message content route now that discord.py has slash commands, the following is the code i have and this works for making the command and having the user be able to run it but i have had a look at the api reference and cant seem to get it so it has fields for text arguments and making it so it has a required file attachment field.

client = discord.Client(intents=intents)
tree = app_commands.CommandTree(client)


@tree.command(name="test", description = "this is a test command")
async def first_command(interaction: discord.Interaction):
    await interaction.response.send_message(f"hello {name}", ephemeral=True)

I tried using a bunch of different methods of adding an attachment field in the command line but it kept telling me things werent defined and that i had invalid syntax

Asked By: georgielang

||

Answers:

The syntax for slash command arguments is the same as regular commands. Just add a parameter to your callback & give it a type annotation.

...
async def first_command(interaction, arg1: str, arg2: discord.Attachment):
    ...

For more examples refer to the official app commands examples: https://github.com/Rapptz/discord.py/blob/master/examples/app_commands/basic.py

Answered By: stijndcl
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.