Advertisement
Tranquility3

Dynamically Created DropDown menu

Nov 13th, 2021
411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.82 KB | None | 0 0
  1. #=========================================#
  2. # This code creates a dynamically created #
  3. # dropdown menu and attaches it to an     #
  4. # embed.                                  #
  5. #=========================================#
  6. import nextcord
  7.  
  8. from nextcord.ext import commands
  9.  
  10. class Dropdown(nextcord.ui.Select):
  11.     def __init__(self, options):
  12.  
  13.         # Set the options that will be presented inside the dropdown
  14.        
  15.  
  16.         # The placeholder is what will be shown when no option is chosen
  17.         # The min and max values indicate we can only pick one of the three options
  18.         # The options parameter defines the dropdown options. We defined this above
  19.         super().__init__(placeholder='Please select from the options below...', min_values=1, max_values=1, options=options)
  20.  
  21.     async def callback(self, interaction: nextcord.Interaction):
  22.         # Use the interaction object to send a response message containing
  23.         # the user's favourite colour or choice. The self object refers to the
  24.         # Select object, and the values attribute gets a list of the user's
  25.         # selected options. We only want the first one.
  26.         print("ctx.author.id: ",self.view.ctx.author.id)
  27.         print("interaction.user.id: ",interaction.user.id)
  28.         if interaction.user.id == self.view.ctx.author.id:
  29.             await interaction.response.send_message(f'Your favourite colour is {self.values[0]}', ephemeral=True)
  30.             #Sets the view.value to the label of the button clicked
  31.             self.view.value = self.values[0]
  32.  
  33.             #Ends the interation to stop other buttons being clicked
  34.             self.view.stop()
  35.  
  36.  
  37. class DropdownView(nextcord.ui.View):
  38.     def __init__(self, ctx, options):
  39.         super().__init__()
  40.         self.value = None
  41.         self.ctx = ctx
  42.         # Adds the dropdownto our view object.
  43.         self.add_item(Dropdown(options))
  44.  
  45. async def ddcreate(self, ctx, options, msg_title, msg_desc):
  46.     """Sends a message with our dropdown containing colours"""
  47.  
  48.     # Create the view containing our dropdown
  49.     view = DropdownView(ctx, options)
  50.     embed = nextcord.Embed(
  51.         title=msg_title,
  52.         description=msg_desc,
  53.         colour = self.bot.colors["BRANN"],
  54.     )
  55.  
  56.  
  57.     embed.set_thumbnail(url=self.bot.user.avatar.url)
  58.     #await ctx.send(embed=embed)
  59.  
  60.     # Sending a message containing our view
  61.     msg = await ctx.send(embed=embed, view=view)
  62.  
  63.     # Wait for the View to stop listening for input...
  64.     await view.wait()
  65.  
  66.     #
  67.    
  68.     if view.value is None:
  69.         print('Timed out...')
  70.         await msg.delete
  71.         return
  72.    
  73.     else:
  74.         return(view.value)
  75.  
  76. #=========================================#
  77. # This is the command that launches the above code and recieves the chosen option from the dropdowns
  78. #=========================================#
  79.  
  80.  @commands.command(
  81.         name='ping',
  82.         #aliases=['xpch'],
  83.         description="test function",
  84.  
  85.     )
  86.     @commands.has_role("DiscordAdmin")
  87.     @commands.has_permissions(send_messages=True)
  88.     async def ping(self, ctx):
  89.         #CREATE YOUR OPTIONS FOR THE DROP DOWN
  90.         options = [
  91.             nextcord.SelectOption(label='Red', description='Your favourite colour is red', emoji='🟥'),
  92.             nextcord.SelectOption(label='Yellow', description='Your favourite colour is Yellow', emoji='🟩'),
  93.             nextcord.SelectOption(label='Blue', description='Your favourite colour is blue', emoji='🟦')
  94.         ]
  95.         # SET THE TITLE AND DESCRIPTION FOR THE DROPDOWN
  96.         msg_title= "Colour Select!"
  97.         msg_desc= f"Hello {ctx.author.mention}, Please choose your fav colour!"
  98.         #Launch the dropdown
  99.         option_select = await ddcreate(self, ctx, options, msg_title, msg_desc)
  100.         await ctx.send(f"The button pressed: {option_select}")
  101.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement