#(newbie) How to capture response from a StringSelectMenu?

1 messages · Page 1 of 1 (latest)

wispy lantern
#

discord-py-interactions 5.11.0
Python 3.11.6
I think the try statement is at fault?

`
from config import TOKEN, GUILD_ID
from interactions import Client, slash_command
from interactions import StringSelectMenu

bot = Client(token=TOKEN)

@slash_command(name="show_menu", description="Show a StringSelectMenu", scopes=[GUILD_ID])
async def show_menu(ctx):
select_menu = StringSelectMenu(
"Option 1",
"Option 2",
"Option 3",
placeholder="Choose an option...",
min_values=1,
max_values=1,
)

response = await ctx.send("Which option?:", components=[select_menu])

try:
    interaction = await bot.wait_for("select_option",checks=[],timeout=60)
    selected_option = interaction.values[0]

    # Validate the response
    if selected_option == "Option 1":
        await interaction.send_message("You selected Option 1!")
    elif selected_option == "Option 2":
        await interaction.send_message("You selected Option 2!")
    elif selected_option == "Option 3":
        await interaction.send_message("You selected Option 3!")

except TimeoutError:
    await response.send("You took too long to respond. Please try again.")

bot.start()
`

ancient portalBOT
#

Hey! Once your issue is solved, press the button below to close this thread!

ancient portalBOT
#

To send your Python code in Discord, please format it using this way:

```py
YOUR CODE HERE
```

This will result in a much better formatting and will help to make your code clearer

polar heron
#

@wispy lantern

wispy lantern
#
from config import TOKEN, GUILD_ID
from interactions import Client, slash_command
from interactions import StringSelectMenu

bot = Client(token=TOKEN)

@slash_command(name="show_menu", description="Show a StringSelectMenu", scopes=[GUILD_ID])
async def show_menu(ctx):
    select_menu = StringSelectMenu(
        "Option 1",
        "Option 2",
        "Option 3",
        placeholder="Choose an option...",
        min_values=1,
        max_values=1,
    )

    response = await ctx.send("Which option?:", components=[select_menu])

    try:
        interaction = await bot.wait_for("select_option",checks=[],timeout=60)
        selected_option = interaction.values[0]

        # Validate the response
        if selected_option == "Option 1":
            await interaction.send_message("You selected Option 1!")
        elif selected_option == "Option 2":
            await interaction.send_message("You selected Option 2!")
        elif selected_option == "Option 3":
            await interaction.send_message("You selected Option 3!")

    except TimeoutError:
        await response.send("You took too long to respond. Please try again.")

bot.start()

#

I have spent quite some time reading that document. It's good but it assumes a lot of prior knowledge. I'm further confused by the many examples found using google which are not v5

#

So I'm looking at component_callback in the Responding section

olive cypress
#

interaction = await bot.wait_for("select_option",checks=[],timeout=60)
the problem here is that you don't have any component called "select_option"

#

try to use wait_for_component, and pass it directly the select menu object

wispy lantern
#

I have resolved the issue, as you @olive cypress said I hadn't got a component called "select_option" and I've changed to using persistent_callbacks . Thank you very much for your help 😊