Making a choice #8236
-
Hello! Please help me make my choice of slash commands, because I get an error in the code:
My code:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
OverviewI'm a bit confused to as if this is discord.py or not. The How to create Choices?There's 3 main ways you can create choices. Using the Which style is best for me?It's all up to you and your needs! Personally speaking, I'm a fan of the How can I use these?Let's walk through a couple examples. @app_commands.choicesimport discord
from discord.ext import commands
from discord import app_commands
bot = commands.Bot(command_prefix=commands.when_mentioned_or('!'), intents=discord.Intents.default())
# our bot instance creates a command tree for us! We can use this
# CommandTree to manage our app commands.
tree: app_commands.CommandTree[commands.Bot] = bot.tree
# Define a new tree command
@tree.command(name='fruit_select_decorator')
# Use the decorator, `@app_commands.choices` to create
# some choices for the user to select from.
@app_commands.choices(fruits=[
app_commands.Choice(name='apple', value=1),
app_commands.Choice(name='banana', value=2),
app_commands.Choice(name='cherry', value=3),
])
# Use "app_commands.describe" to give the "fruits" parameter a description
@app_commands.describe(fruits='fruits to choose from')
# Now define our command
async def fruit_select_decorator(interaction: discord.Interaction, fruits: app_commands.Choice[int]) -> None:
await interaction.response.send_message(f'Your favourite fruit is {fruits.name}.') typing.Literalfrom typing import Literal
# Define command
@tree.command(name='fruit_select_literal')
# Use "app_commands.describe" to give the "fruits" parameter a description
@app_commands.describe(fruits='fruits to choose from')
# Now we can use "Literal" as an annotation to create choices. Discord.py does some magic for us here.
async def fruit_select_literal(interaction: discord.Interaction, fruits: Literal['apple', 'banana', 'cherry']) -> None:
await interaction.response.send_message(f'Your favourite fruit is {fruits}.') enum.Enumimport enum
# Let's make an enum called "Fruits" with our choices
# Similar to how how `@app_commands.choices` works, this enum works
# on a "name = value" system. So for instance,
# name = "apple"
# value = 1
# Just like defining an "app_commands.Choice" but a bit nicer!
class Fruits(enum.Enum):
apple = 1
banana = 2
cherry = 3
# Define our command
@tree.command(name='fruit_select_enum')
# Use "app_commands.describe" to give the "fruits" parameter a description
@app_commands.describe(fruits='fruits to choose from')
# Now we can annotate the "fruits" parameter with the "Fruits" enum we defined.
async def fruit_select_enum(interaction: discord.Interaction, fruits: Fruits) -> None:
return await interaction.response.send_message(f'Your favourite fruit is {fruits}.') |
Beta Was this translation helpful? Give feedback.
Overview
I'm a bit confused to as if this is discord.py or not. The
@slash.command
isn't really valid unless you named your command tree "slash", but you referenced a choice withapp_commands.Choice
? Weird, regardless lets take a look:How to create Choices?
There's 3 main ways you can create choices. Using the
@app_commands.choices
decorator, usingtyping.Literal
as an annotation, and usingenum.Enum
as an annotation.Which style is best for me?
It's all up to you and your needs! Personally speaking, I'm a fan of the
@app_commands.choices
decorator. I would test around with the options available and see which one fits your needs the most.How can I use these?
Let's walk through a couple …