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.
- Generate the menu items dynamically
- Tailor this to the discord user; meaning I need to be able to get the interaction object into the menu builder.
- 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))```
