diff options
| author | Robin5605 <[email protected]> | 2021-07-28 18:53:06 -0500 |
|---|---|---|
| committer | GitHub <[email protected]> | 2021-07-28 19:53:06 -0400 |
| commit | 78aea51f508a14bb1b03b49933576c84b56a7459 (patch) | |
| tree | fe2dc1628dddfdab96a8fb736e28c77624409587 /examples | |
| parent | Add BaseUser.banner for all subclasses to access new banners (diff) | |
| download | discord.py-78aea51f508a14bb1b03b49933576c84b56a7459.tar.xz discord.py-78aea51f508a14bb1b03b49933576c84b56a7459.zip | |
Add an example for the new dropdowns
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/views/dropdown.py | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/examples/views/dropdown.py b/examples/views/dropdown.py new file mode 100644 index 00000000..db6d699a --- /dev/null +++ b/examples/views/dropdown.py @@ -0,0 +1,63 @@ +import typing + +import discord +from discord.ext import commands + +# Defines a custom Select containing colour options +# that the user can choose. The callback function +# of this class is called when the user changes their choice +class Dropdown(discord.ui.Select): + def __init__(self): + + # Set the options that will be presented inside the dropdown + options = [ + discord.SelectOption(label='Red', description='Your favourite colour is red', emoji='🟥'), + discord.SelectOption(label='Green', description='Your favourite colour is green', emoji='🟩'), + discord.SelectOption(label='Blue', description='Your favourite colour is blue', emoji='🟦') + ] + + # 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='Choose your favourite colour...', min_values=1, max_values=1, options=options) + + async def callback(self, interaction: discord.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. + await interaction.response.send_message(f'Your favourite colour is {self.values[0]}') + + +class DropdownView(discord.ui.View): + def __init__(self): + super().__init__() + + # Adds the dropdown to our view object. + self.add_item(Dropdown()) + + +class Bot(commands.Bot): + def __init__(self): + super().__init__(command_prefix=commands.when_mentioned_or('$')) + + async def on_ready(self): + print(f'Logged in as {self.user} (ID: {self.user.id})') + print('------') + + +bot = Bot() + + +async def colour(ctx): + """Sends a message with our dropdown containing colours""" + + # Create the view containing our dropdown + view = DropdownView() + + # Sending a message containing our view + await ctx.send('Pick your favourite colour:', view=view) + + +bot.run('token') |