#adding select menu options through a function

1 messages · Page 1 of 1 (latest)

hoary badge
#

Hi, I'm trying to add select menu options through a function.

This is the class where I create the view with the select menu and a function in it that creates the options for the menu:

lass SelectTrainerName(discord.ui.View):
    def __init__(self, team_data: dict):
        super().__init__(timeout=None)
        self.team_data = team_data

    def select_name_generation(self):
        selection_menu_list = []

        for _ in range(7):
            name: str = helper.names.generate_name(self.team_data["co_trainer_na"], "male")
            selection_menu_list.append(
                discord.SelectOption(
                    label=name,
                    value=f"{name.lower()}"
                )
            )

        for _ in range(3):
            name: str = helper.names.generate_name(self.team_data["co_trainer_na"], "female")
            selection_menu_list.append(
                discord.SelectOption(
                    label=name,
                    value=f"{name.lower()}"
                )
            )

        return selection_menu_list

    @discord.ui.select(custom_id='select_co-trainer-name', placeholder="🧑🏽 | Co. Namen auswählen", options=select_name_generation)
    async def select_callback(self, select, interaction):
        embed = discord.Embed(
            description=f'Willst du `{self.team_data["team_alias"]}` vor dein Name haben?')
        
        self.team_data["co_trainer_name"] = select.values[0]
        
        await interaction.message.edit(embed=embed, view=ClickChangeNameComfirmation(self.team_data))

The problem is that when I don't have the brackets here: options=select_name_generation I get this error:

Error 1:

TypeError: 'function' object is not iterable

when I add brackets to the function name like this: options=select_name_generation()

then I get this error:

Error 2:

SelectTrainerName.select_name_generation() missing 1 required positional argument: 'self'

As I can see the error indicates that there is missing an argument. But I can't give it the self argument because the line isn't in a function.

How can I fix this error?

#

Full error message Error 2:

Traceback (most recent call last):
  File "C:\Users\Pro 8\AppData\Local\Programs\Python\Python311\Lib\site-packages\discord\cog.py", line 774, in _load_from_module_spec
    spec.loader.exec_module(lib)  # type: ignore
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap_external>", line 940, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "C:\Users\Pro 8\Documents\Programmieren\Discord Bots\ZZGamesucht\football-minigame\cogs\create_team.py", line 163, in <module>
    class SelectTrainerName(discord.ui.View):
  File "C:\Users\Pro 8\Documents\Programmieren\Discord Bots\ZZGamesucht\football-minigame\cogs\create_team.py", line 191, in SelectTrainerName
    @discord.ui.select(custom_id='select_co-trainer-name', placeholder=":adult_tone3: | Co. Namen auswählen", options=select_name_generation())
                                                                                                           ^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: SelectTrainerName.select_name_generation() missing 1 required positional argument: 'self'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\Pro 8\Documents\Programmieren\Discord Bots\ZZGamesucht\football-minigame\main.py", line 33, in <module>
    client.load_extension('cogs.create_team')
  File "C:\Users\Pro 8\AppData\Local\Programs\Python\Python311\Lib\site-packages\discord\cog.py", line 910, in load_extension
    self._load_from_module_spec(spec, name)
  File "C:\Users\Pro 8\AppData\Local\Programs\Python\Python311\Lib\site-packages\discord\cog.py", line 777, in _load_from_module_spec
    raise errors.ExtensionFailed(key, e) from e
discord.errors.ExtensionFailed: Extension 'cogs.create_team' raised an error: TypeError: SelectTrainerName.select_name_generation() missing 1 required positional argument: 'self'
mighty inlet
#

or at the end of __init__, you can set self.select_callback.options = self.select_name_generation()

hoary badge
mighty inlet
#

then do my last method

#

(and remove options=select_name_generation from the decorator)

hoary badge
# mighty inlet then do my last method

like this:

init:

    def __init__(self, team_data: dict):
        super().__init__(timeout=None)
        self.team_data = team_data
        self.select_callback.options = self.select_name_generation()

select menu:

    @discord.ui.select(custom_id='select_co-trainer-name', placeholder="🧑🏽 | Co. Namen auswählen")
mighty inlet
#

yeah

hoary badge
#

ok I will try it

#

that worked thanks

#

.close

next runeBOT
#

Done with your help thread?

Please close your own help thread by using </close:1009144375709814897> with @solid compass.

Backup bot: </solved:1109625445990793246> (or .solved) with @next rune.

hoary badge
#

.solved