0

I'm creating a menu system for my Discord bot by using buttons and select menus. So far I've had to control this flow by calling the next function from within the button, or else my code will continue without waiting for a decision to be made, and if I try using while loops it will just repeatedly spam the menu.

This currently will display the drop down menu of characters to select when added to a standard View() and sent to the user, but the problem lies in the callback.

I would prefer my code waits for the choice to be made and the value returned to the "main loop" for further operations to be made. Is this possible?

import os
from discord.ext import commands
import discord

class Select_character(discord.ui.Select):
    def __init__(self, choices):
        super().__init__()
        for each in choices:
            self.options.append(discord.SelectOption(label=each['name'], description=each['unit']))
    async def callback(self, ctx):
        return self.values


TOKEN = '{bot_token}'
prefix = "!"
client = commands.Bot(command_prefix=prefix, case_insensitive=True)

test_case = [{"name" : "John", "unit" : "Fighter"}, {"name" : "Filip", "unit" : "Monk"}]

@client.event
async def on_ready():
    print(f"We have logged in as {client.user}")

@client.event
async def on_message(ctx):
    if not ctx.author.bot:
        choices = Select_character(test_case)
        view = discord.ui.View()
        view.add_item(choices)
        await ctx.channel.send(content="Make a Choice", view=view)
        print(choice_made)

client.run(TOKEN)
Aceinfurno
  • 36
  • 4
  • 1
    You probably are looking for [`bot.wait_for`](https://discordpy.readthedocs.io/en/stable/api.html#discord.Client.wait_for). See https://stackoverflow.com/a/68721338/16136190, https://stackoverflow.com/questions/52571844/discord-py-rewrite-wait-for-how-do-i-use and https://www.google.com/search?q=discord.py+%22wait_for%22+site%3Astackoverflow.com. – The Amateur Coder Jul 16 '22 at 14:12
  • @TheAmateurCoder I've tried but bot.wait_for doesn't allow for the sending of views – Aceinfurno Jul 16 '22 at 14:15
  • What do you mean? Could you please [edit] your post to include how you've tried using `bot.wait_for()`? – The Amateur Coder Jul 16 '22 at 14:17

0 Answers0