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 how to add required text/attachment on a slash command

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

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

>Solution :

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

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