Search code examples
pythondiscordbotspycorddisnake

Creating selection in select menu discord python


I want after selecting the first option, for me to have a choice between two other options, like a question within a question. Is it possible to do this? Here is my code:

import discord
from discord.ext import commands

class Dropdown(discord.ui.Select):
    def __init__(self):
        options = [
            discord.SelectOption(label='Акрополь', description='Будет отправлен баннер "Акрополь"', emoji='🏛️'),
            discord.SelectOption(label='Год Кролика', description='Будет отправлен баннер "Год Кролика"', emoji='🐇'),
            discord.SelectOption(label='Зевс', description='Будет отправлен баннер "Зевс"', emoji='⚡'),
        ]

        super().__init__(placeholder='Выберете ваш любимый цвет...', min_values=1, max_values=1, options=options)

    async def callback(self, interaction: discord.Interaction):
        if self.values[0] == 'Акрополь':
            await interaction.response.send_message('Баннер "Акрополь"',file=discord.File(r'C:\Users\Timir\AppData\Local\Discord\Akropol.png'))
        elif self.values[0] == 'Год Кролика':
            await interaction.response.send_message('Баннер "Год Кролика"',file=discord.File(r'C:\Users\Timir\AppData\Local\Discord\Kroll.png'))
        elif self.values[0] == 'Зевс':
            await interaction.response.send_message('Баннер "Зевс"', file=discord.File(r'C:\Users\Timir\AppData\Local\Discord\Zevs.png'))

class DropdownView(discord.ui.View):
    def __init__(self):
        super().__init__()
        self.add_item(Dropdown())

class Bot(commands.Bot):
    def __init__(self):
        intents = discord.Intents.default()
        intents.message_content = True

        super().__init__(command_prefix=commands.when_mentioned_or('$'), intents=intents)

    async def on_ready(self):
        print(f'Logged in as {self.user} (ID: {self.user.id})')
        print('------')


intents = discord.Intents.all()
bot = commands.Bot(command_prefix='+', intents=intents)


@bot.command()
async def banner(ctx):
    """Отправляет сообщение с выбранным баннером"""

    view = DropdownView()

    await ctx.send('Выберете нужный баннер:', view=view)







@bot.command()
async def Молодец(ctx):
    await ctx.send(f"{ctx.author.mention} молодец")


bot.run('bottoken')

I couldn't find the necessary function and didn't really understand where to use it.


Solution

  • There's a couple different ways you can do this and it'll depend on what you want to achieve exactly.

    If you want a new Select with options then you could have a separate NewSelect class with your options and callback (similar to your existing one) and then instantiate it when your original select is selected. You can access the parent View in the Select using self.view and then do self.view.add_item that way to add a new select to the view. Make sure to update the message with the new view.

    Or, if you want to modify the options of the existing Select, you can change the select's options using self.options. And then update the message with the new view.

    Hope that gets you in the right direction!