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

How to add options to an argument of a slash command in discord.py?

import discord
from discord import app_commands

class aclient(discord.Client):
    def __init__(self):
        super().__init__(intents=discord.Intents.default())
        self.synced = False

    async def on_ready(self):
        await self.wait_until_ready()
        if not self.synced:
            await tree.sync(guild=discord.Object(id=748244591387607111))
            self.synced = True
        print('---------------------Bot is ready---------------------')

client = aclient()
tree = app_commands.CommandTree(client)

@tree.command(name='test')
async def test(interaction: discord.Interaction, option: str):
    pass

client.run('MYTOKEN')

That’s my code. I want to add an option where I can select between several categories, for example test1 and test2. I don’t want to use an external library like pycord or nextcord, I want to do it in the native discord.py. Not asking for any code, just an example of how I’d go about doing it. Everything I found online was either outdated or used nextcord, so I decided to ask here. Hope someone can help : )

>Solution :

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

Here’s an example of how to specify options for a command parameter:

@tree.command(name='test')
@app_commands.describe(option="This is a description of what the option means")
@app_commands.choices(option=[
        app_commands.Choice(name="Option 1", value="1"),
        app_commands.Choice(name="Option 2", value="2")
    ])
async def test(interaction: discord.Interaction, option: app_commands.Choice[str]):
    pass

To make the option optional, you’d have to import typing and change the type to:

async def test(interaction: discord.Interaction, option: typing.Optional[app_commands.Choice[str]]):
    pass
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