#Dynamic Dropdown Menu tailored to User

1 messages · Page 1 of 1 (latest)

wide vine
#

Although I was able to make this work, I wanted to post this to see if it looks correct, or if there may be a cleaner way to write this. Here is what I needed to solve.

  1. Generate the menu items dynamically
  2. Tailor this to the discord user; meaning I need to be able to get the interaction object into the menu builder.
  3. Use an asynchronous function to collect the users info.

Here is the code that worked for me. You'll note I used asyncinit to get past my challenge of using the async function. This will likely be unnecessary for most use cases. And I think I'm going to rewrite that function so it doesnt need to be called asynchronously. Would love for some python gurus to have a look and let me know whats good, whats bad, and what suggestions you have to improve it.

class Select_Listing_Options(discord.ui.Select):
    async def __init__(self, interaction):
        self.interaction = interaction
        self.discord_user = interaction.user.id
        self.users_listings = await get_users_listings(self.discord_user)
        options = [discord.SelectOption(label=listing, description=listing, value=listing) for listing in self.users_listings]
        super().__init__(
            placeholder="Select a listing...",
            min_values=1,
            max_values=1,
            options=options,
        )
        async def callback(self, interaction: discord.Interaction):
            self.value = self.values[0]
            message = f'You picked {self.value}'
            await interaction.response.send_message(content=message, ephemeral=True, delete_after=0)
        async def on_timeout(self):
            self.value = None


@asyncinit
class Select_Listing_View(discord.ui.View):
    async def __init__(self, interaction):
        self.interaction = interaction
        super().__init__(await Select_Listing_Options(self.interaction))```
#

Within interaction:

view = await Select_Listing_View(interaction)
await interaction.followup.send(view=view, delete_after=40, ephemeral=True)
await view.wait()```
compact fiber
#

?tag codeblock

near rivetBOT
#

Please put your code in a code block:
```py
Here is your Code
```

That makes reading code in Discord a lot easier:

print("This is an example.")
compact fiber
#

please blobpain

compact fiber
#

Oh boy. Async init. That's cursed tbh 💀

wide vine
#

I was able to solve it. I removed all the async stuff. But that didnt fix it, and I dont think that had anything to do with the issue. I just needed a proper stop statement at the end. Attached is the rev that worked

    def __init__(self, interaction):
        self.value = None
        self.interaction = interaction
        self.discord_user = interaction.user.id
        self.users_listings = get_users_listings(self.discord_user)
        options = [discord.SelectOption(label=listing) for listing in self.users_listings]
        super().__init__(
            placeholder="Select a listing...",
            min_values=1,
            max_values=1,
            options=options,
        )
    async def callback(self, interaction: discord.Interaction):
        self.value = self.values[0]
        await interaction.response.send_message(content='Thinking...', ephemeral=True, delete_after=0)
        self.view.disable_all_items()
        self.view.stop()






class Select_Listing_View(discord.ui.View):
    def __init__(self, interaction, *args, **kwargs) -> None:
        super().__init__(Select_Listing_Options(interaction))```

the self.view.stop() was the trick.  I was trying to use self.stop()
compact fiber
#

Oh cool

#

Well ig you resolved this on your own

#

.close

wispy irisBOT
#

Done with your help thread?

Please close your own help thread by using </close:1009144375709814897> with @jade sedge.

jade sedgeBOT
#

This thread was archived by the user that opened it.

wide vine
compact fiber
#

and you can have an implementation that doesnt use asyncint

#

call external_user_specific_list_function from the slash cmd rather than init

#

simple as that

opal crest
#

Thanks for the examples 👍

jade sedgeBOT
#

This thread was archived by a staff member.

opal crest
#

@prime sentinel Why did you re-open this?

prime sentinel
#

Sorry I didn't mean to. I must have miskicked

opal crest
#

👍