How to get user's choice from discord dropdown list?

Viewed 19

I'm trying to get the user's choice from the dropdown list, but I don't understand how to do it. I created a value variable in the Dropdown class and I'm trying to intercept its values, which are then checked in the color function

class Dropdown(discord.ui.Select):
    def __init__(self):
    self.value = None

        # Set the options that will be presented inside the dropdown
        options = [
            discord.SelectOption(label='Red', description='Your favourite colour is red', emoji=':red_square:'),
            discord.SelectOption(label='Green', description='Your favourite colour is green', emoji=':green_square:'),
            discord.SelectOption(label='Blue', description='Your favourite colour is blue', emoji=':blue_square:'),
        ]

        super().__init__(placeholder='Choose your favourite colour...', min_values=1, max_values=1, options=options)

    async def callback(self, interaction: discord.Interaction):
        # Use the interaction object to send a response message containing
        # the user's favourite colour or choice. The self object refers to the
        # Select object, and the values attribute gets a list of the user's
        # selected options. We only want the first one.
        await interaction.response.send_message(f'Your favourite colour is {self.values[0]}')
        self.value = True


class DropdownView(discord.ui.View):
    def __init__(self):
        super().__init__()

        # Adds the dropdown to our view object.
        drop = Dropdown()
        self.add_item(Dropdown())


@bot.command()
async def colour(ctx):
    """Sends a message with our dropdown containing colours"""

    # Create the view containing our dropdown
    view = DropdownView()

    # Sending a message containing our view
    await ctx.send('Pick your favourite colour:', view=view)
    await view.wait()
    if view.value is None:
        print('Timed out...')
    elif view.value:
        print(f'Received...')
0 Answers
Related