Dropdowns (Select Menus)
How to use dropdowns/select menus with Pycord
This guide has been moved to https://namantech.me/pycord. Please visit the new guide instead, since this one might be outdated.
The new guide looks better, has better explanations, and is way better overall.
Docs: https://pycord.readthedocs.io/en/master/api.html?highlight=dropdown#discord.SelectMenu

Min Values: The Minimum number of values the user must select. A user can select multiple values in a select menu. If you want the user to be able to select only one option, set both min values and max values to 1.
Max Values: The Minimum number of values the user can select. A user can select multiple values in a select menu. If you want the user to be able to select only one option, set both min values and max values to 1.

Placeholder: The text to be displayed when no option is selected.

Way 1: Subclassing discord.ui.View
class MyView(discord.ui.View):
@discord.ui.select(placeholder='Pick your colour', min_values=1, max_values=1, options=[
discord.SelectOption(label='Red', description='Your favourite colour is red', emoji='🟥'),
discord.SelectOption(label='Green', description='Your favourite colour is green', emoji='🟩'),
discord.SelectOption(label='Blue', description='Your favourite colour is blue', emoji='🟦')
])
async def select_callback(self, select, interaction):
await interaction.response.send_message(f'Your favourite colour is {select.values[0]}', ephemeral=True)
@bot.command()
async def my_select(ctx):
await ctx.send('What is your favourite colour?', view=MyView(timeout=0))
Way 2: Sublcassing discord.ui.SelectMenu
class MySelect(discord.ui.Select):
def __init__(self):
options = [
discord.SelectOption(label='Red', description='Your favourite colour is red', emoji='🟥'),
discord.SelectOption(label='Green', description='Your favourite colour is green', emoji='🟩'),
discord.SelectOption(label='Blue', description='Your favourite colour is blue', emoji='🟦')
]
super().__init__(placeholder='Pick your colour', min_values=1, max_values=1, options=options)
async def callback(self, interaction):
await interaction.response.send_message(f'Your favourite colour is {self.values[0]}', ephemeral=True)
view = discord.ui.View(timeout=10) # timeout is optional, it can be defined in seconds
view.add_item(MySelect())
@bot.command()
async def my_select(ctx):
await ctx.send('What is your favourite colour?', view=view)
Rows
A message can have up to five "action rows" and each of these "action" rows have five slots where you can put message components. A button takes up one of these slots but a select menus takes up all five slots of a "action row". Keep this in mind when creating your views since you don't want to run out of space!
class MyView(discord.ui.View):
@discord.ui.select(min_values=1, max_values=1, options=[
...,
row=1)
async def select_callback(self, select, interaction):
...
@discord.ui.select(min_values=1, max_values=1, options=[
...,
row=2)
async def second_select_callback(self, select, interaction):
...
@bot.command()
async def my_select(ctx):
await ctx.send('What is your favourite colour?', view=MyView(timeout=0))
Last updated
Was this helpful?