Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #=========================================#
- # This code creates a dynamically created #
- # dropdown menu and attaches it to an #
- # embed. #
- #=========================================#
- import nextcord
- from nextcord.ext import commands
- class Dropdown(nextcord.ui.Select):
- def __init__(self, options):
- # Set the options that will be presented inside the dropdown
- # The placeholder is what will be shown when no option is chosen
- # The min and max values indicate we can only pick one of the three options
- # The options parameter defines the dropdown options. We defined this above
- super().__init__(placeholder='Please select from the options below...', min_values=1, max_values=1, options=options)
- async def callback(self, interaction: nextcord.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.
- print("ctx.author.id: ",self.view.ctx.author.id)
- print("interaction.user.id: ",interaction.user.id)
- if interaction.user.id == self.view.ctx.author.id:
- await interaction.response.send_message(f'Your favourite colour is {self.values[0]}', ephemeral=True)
- #Sets the view.value to the label of the button clicked
- self.view.value = self.values[0]
- #Ends the interation to stop other buttons being clicked
- self.view.stop()
- class DropdownView(nextcord.ui.View):
- def __init__(self, ctx, options):
- super().__init__()
- self.value = None
- self.ctx = ctx
- # Adds the dropdownto our view object.
- self.add_item(Dropdown(options))
- async def ddcreate(self, ctx, options, msg_title, msg_desc):
- """Sends a message with our dropdown containing colours"""
- # Create the view containing our dropdown
- view = DropdownView(ctx, options)
- embed = nextcord.Embed(
- title=msg_title,
- description=msg_desc,
- colour = self.bot.colors["BRANN"],
- )
- embed.set_thumbnail(url=self.bot.user.avatar.url)
- #await ctx.send(embed=embed)
- # Sending a message containing our view
- msg = await ctx.send(embed=embed, view=view)
- # Wait for the View to stop listening for input...
- await view.wait()
- #
- if view.value is None:
- print('Timed out...')
- await msg.delete
- return
- else:
- return(view.value)
- #=========================================#
- # This is the command that launches the above code and recieves the chosen option from the dropdowns
- #=========================================#
- @commands.command(
- name='ping',
- #aliases=['xpch'],
- description="test function",
- )
- @commands.has_role("DiscordAdmin")
- @commands.has_permissions(send_messages=True)
- async def ping(self, ctx):
- #CREATE YOUR OPTIONS FOR THE DROP DOWN
- options = [
- nextcord.SelectOption(label='Red', description='Your favourite colour is red', emoji='🟥'),
- nextcord.SelectOption(label='Yellow', description='Your favourite colour is Yellow', emoji='🟩'),
- nextcord.SelectOption(label='Blue', description='Your favourite colour is blue', emoji='🟦')
- ]
- # SET THE TITLE AND DESCRIPTION FOR THE DROPDOWN
- msg_title= "Colour Select!"
- msg_desc= f"Hello {ctx.author.mention}, Please choose your fav colour!"
- #Launch the dropdown
- option_select = await ddcreate(self, ctx, options, msg_title, msg_desc)
- await ctx.send(f"The button pressed: {option_select}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement