#๐Ÿ”’ How do I create a drop down menu for users to select from a list of options?

22 messages ยท Page 1 of 1 (latest)

uncut skiff
#

Using nextcord, when the user runs the command they should be prompted with a drop down menu with a selection of options to choose from. I'm not fully sure how to achieve this.

thorn oysterBOT
#

@uncut skiff

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

fluid jungle
#

hi

uncut skiff
fluid jungle
uncut skiff
#

yep

uncut skiff
#

hmm

#

would this work for more than one option?

fluid jungle
#

nextcord.ui.Select

#

options = [
            SelectOption(label="Option 1", description="This is the first option"),
            SelectOption(label="Option 2", description="This is the second option"),
            SelectOption(label="Option 3", description="This is the third option")
        ]
#

here is an example:


class Dropdown(nextcord.ui.Select):
    def __init__(self):
        options = [
            SelectOption(label="Option 1", description="This is the first option"),
            SelectOption(label="Option 2", description="This is the second option"),
            SelectOption(label="Option 3", description="This is the third option")
        ]
        super().__init__(placeholder="Choose an option...", min_values=1, max_values=1, options=options)

    async def callback(self, interaction: Interaction):
        await interaction.response.send_message(f"You selected: {self.values[0]}")


uncut skiff
#

ok interesting, I'll try this rq

fluid jungle
#

please create a command and put it in View() to display the dropdown

uncut skiff
#

view()?

#

elaborate

fluid jungle
#

dropdown = Dropdown()
view = nextcord.ui.View()
view.add_item(dropdown)

that would be an example

uncut skiff
#
@bot.slash_command(name='company', description='Search all companies.')
async def company(interaction: nextcord.Interaction):
    await interaction.response.defer()
    try:
        connection = mysql.connector.connect(**db_config)
        cursor = connection.cursor()
        
        query = "SELECT c_name FROM companies"
        cursor.execute(query)
        names = cursor.fetchall()
        
        class Dropdown(nextcord.ui.Select):
            def __init__(self):
                options = [
                    {'label': name[0], 'value': name[0]} for name in names]
        
        async def callback(self, interaction: Interaction):
            await interaction.response.send_message(f"You selected: {self.values[0]0}")

    except mysql.connector.Error as error:
        print(f"Error:", {error})
        await throw_error(interaction, "An error occured while processing your request.")

    finally:
        if connection.is_connected():
            cursor.close()
            connection.close()```
#

I have not a clue what I'm really doing lol @fluid jungle

fluid jungle
#

class Dropdown(nextcord.ui.Select):
    def __init__(self, options):
        super().__init__(placeholder="Choose a company...", options=options)

    async def callback(self, interaction: nextcord.Interaction):
        await interaction.response.send_message(f"You selected: {self.values[0]}")

@bot.slash_command(name='company', description='Search all companies.')
async def company(interaction: nextcord.Interaction):
    await interaction.response.defer()
    try:
        connection = mysql.connector.connect(**db_config)
        cursor = connection.cursor()

        query = "SELECT c_name FROM companies"
        cursor.execute(query)
        names = cursor.fetchall()

        options = [{'label': name[0], 'value': name[0]} for name in names]

        dropdown = Dropdown(options)
        view = nextcord.ui.View()
        view.add_item(dropdown)

        await interaction.followup.send("Please choose a company:", view=view)

    except mysql.connector.Error as error:
        print("Error:", error)
        await throw_error(interaction, "An error occurred while processing your request.")

    finally:
        if connection.is_connected():
            cursor.close()
            connection.close()

thorn oysterBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.