我正在使用Python为我大学的校友创建一个discord机器人。 ui.Select 无法正常工作

问题描述 投票:0回答:1

我创建了一个命令“/embed”。此命令应打开一个模式,用户可以在其中输入其姓名和批次年份。这将为他们分配校友的角色以及他们的批次年份。如果我使用 ui.TextInput 让校友像这样手动输入批次年份,则该命令工作正常

name = ui.TextInput(label='Enter your name')
batch_year = ui.TextInput(label='Enter your batch year (e.g., 2021)', placeholder='Enter a valid batch year')

但是如果我尝试使用 ui.Select 使其成为下拉列表,则会抛出错误

name = ui.TextInput(label='Enter your name')
batch_year = ui.Select(
    placeholder="Select your batch year",
    options=[
        discord.SelectOption(label="2020", value="2020"),
        discord.SelectOption(label="2021", value="2021"),
        discord.SelectOption(label="2022", value="2022"),
        discord.SelectOption(label="2023", value="2023"),
        discord.SelectOption(label="2024", value="2024"),
    ]
)

这是全班同学

class BatchSelection(ui.Modal, title='Batch Information'):
    name = ui.TextInput(label='Enter your name')
    batch_year = ui.TextInput(label='Enter your batch year (e.g., 2021)', placeholder='Enter a valid batch year')

    async def on_submit(self, interaction: discord.Interaction):
        selected_year = self.batch_year.value  # Get the value of the TextInput field
        guild = interaction.guild
        member = interaction.user

        # Assign the "Alumni" role by default
        alumni_role = discord.utils.get(guild.roles, id=ALUMNI_ROLE_ID)
        if alumni_role:
            await member.add_roles(alumni_role)
            confirmation_message = f"Assigned default role: {alumni_role.name}\n"
        else:
            confirmation_message = "Alumni role not found.\n"

        batch_role = discord.utils.get(guild.roles, name=selected_year)

        if not batch_role:
            # Create the batch role
            batch_role = await guild.create_role(name=selected_year, reason=f"Creating batch role for {selected_year}")
            confirmation_message += f"Created and assigned new role: {selected_year}\n"
        else:
            confirmation_message += f"Assigned existing role: {selected_year}\n"

        # Assign the batch role to the member
        if batch_role:
            await member.add_roles(batch_role)

        # Create an embed for confirmation
        embed = discord.Embed(title=f"Name: {self.name.value}", description=f"Batch Year: {selected_year}", color=discord.Colour.random())
        embed.set_footer(text=confirmation_message)

        await interaction.channel.send(embed=embed)
        await interaction.response.send_message(f"Roles updated", ephemeral=True)

这是我遇到的错误

Traceback (most recent call last):
  File "D:\projects\alumini_bot\home\user\bot\.venv\Lib\site-packages\discord\app_commands\commands.py", line 858, in _do_call
    return await self._callback(interaction, **params)  # type: ignore
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\projects\alumini_bot\home\user\bot\bot.py", line 69, in create_embed
    await interaction.response.send_modal(BatchSelection())
  File "D:\projects\alumini_bot\home\user\bot\.venv\Lib\site-packages\discord\interactions.py", line 1041, in send_modal
    await adapter.create_interaction_response(
  File "D:\projects\alumini_bot\home\user\bot\.venv\Lib\site-packages\discord\webhook\async_.py", line 223, in request
    raise HTTPException(response, data)
discord.errors.HTTPException: 400 Bad Request (error code: 50035): Invalid Form Body
In data.components.1.components.0: Value of field "type" must be one of (4,).

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

Traceback (most recent call last):
  File "D:\projects\alumini_bot\home\user\bot\.venv\Lib\site-packages\discord\app_commands\tree.py", line 1310, in _call
    await command._invoke_with_namespace(interaction, namespace)
  File "D:\projects\alumini_bot\home\user\bot\.venv\Lib\site-packages\discord\app_commands\commands.py", line 883, in _invoke_with_namespace
    return await self._do_call(interaction, transformed_values)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\projects\alumini_bot\home\user\bot\.venv\Lib\site-packages\discord\app_commands\commands.py", line 876, in _do_call
    raise CommandInvokeError(self, e) from e
discord.app_commands.errors.CommandInvokeError: Command 'embed' raised an exception: HTTPException: 400 Bad Request (error code: 50035): Invalid Form Body
In data.components.1.components.0: Value of field "type" must be one of (4,).```
python discord.py
1个回答
0
投票

您无法在模式中使用选择菜单。如果您想要选择菜单,则需要在单独的消息中发送。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.