Skip to main content

How to Create a Dropdown Menu on Your Python Discord Bot

1. Log In to Your Panel


2. Select the ‘Files’ Tab

Cybrancee Panel in the files page, "Files" tab on the sidebar is highlighted

3. Create Your Main Bot’s File

Create a new file called index.py then paste the following code.

Pterodactyl Panel, Create File dialog. Enter File Name (index.py)

What this code does:

  • It creates a Discord bot that responds to the /menu command.
  • The bot displays a dropdown menu with three hosting plan options: Discord Bot Hosting, Game Hosting, and Web Hosting.
  • When the user selects an option, the bot sends them a private (ephemeral) message confirming their choice.
  • The code uses discord.py‘s latest UI components for improved functionality.
# Calling our requirements
import discord
from discord import app_commands

# Setting our variables
TOKEN = 'YOUR_BOT_TOKEN'

class MyBot(discord.Client):
    def __init__(self):
        # Setting our intents
        intents = discord.Intents.default()
        super().__init__(intents=intents)
        # Register the /menu command via the CommandTree
        self.tree = app_commands.CommandTree(self)

    async def setup_hook(self):
        # Synchronizes slash commands with Discord
        await self.tree.sync()
        print(f"Slash commands registered for {self.user}")

client = MyBot()

# The logic for the dropdown menu
class PlanDropdown(discord.ui.Select):
    def __init__(self):
        options = [
            discord.SelectOption(
                label="Discord Bot hosting",
                description="(cheapest option)",
                value="discord_hosting"
            ),
            discord.SelectOption(
                label="Game Hosting",
                description="(best resources)",
                value="game_hosting"
            ),
            discord.SelectOption(
                label="Web hosting",
                value="web_hosting"
            ),
        ]
        super().__init__(placeholder="Plan selector", min_values=1, max_values=1, options=options)

    async def callback(self, interaction: discord.Interaction):
        # This part handles the user's selection
        selection = self.values[0]
        
        if selection == 'discord_hosting':
            response = 'You selected **Discord Bot Hosting**.'
        elif selection == 'game_hosting':
            response = 'You selected **Game Hosting**.'
        elif selection == 'web_hosting':
            response = 'You selected **Web Hosting**.'
            
        await interaction.response.send_message(content=response, ephemeral=True)

# Container for the dropdown
class DropdownView(discord.ui.View):
    def __init__(self):
        super().__init__()
        self.add_item(PlanDropdown())

# The bot sends dropdown menu after the command was used
@client.tree.command(name="menu", description="Sends a dropdown menu!")
async def menu(interaction: discord.Interaction):
    # Sends the message with the dropdown view attached
    await interaction.response.send_message("Please select a hosting plan:", view=DropdownView())

client.run(TOKEN)

Dropdown Menu Example

Discord Bot Example of Dropdown Menu 1
Discord Bot Example of Dropdown Menu 2

Discord Bot Hosting

Starts at $1.49

External link icon
Was this article helpful?
Please Share Your Feedback
How Can We Improve This Article?
Table of Contents