使用discord.py 2.0 的模态中“type”字段的值必须是 (4,) 之一

Adr*_*sso 6 python discord discord.py

我试图在用户显示按钮后向他们显示一个模态,其中包含一个下拉选择菜单,他们可以从中选择多个选项。此代码过去曾运行过,但未导致异常。具体来说:

[2022-09-02 22:30:47] [ERROR   ] discord.ui.view: Ignoring exception in view <TestButtonView timeout=180.0 children=1> for item <Button style=<ButtonStyle.primary: 1> url=None disabled=False label='Test' emoji=None row=None>
Traceback (most recent call last):
  File "C:\Users\adria\PycharmProjects\sblBot\venv\lib\site-packages\discord\ui\view.py", line 425, in _scheduled_task
    await item.callback(interaction)
  File "C:\Users\adria\PycharmProjects\sblBot\main.py", line 1131, in test_button_callback
    await interaction.response.send_modal(TestModal())
  File "C:\Users\adria\PycharmProjects\sblBot\venv\lib\site-packages\discord\interactions.py", line 852, in send_modal
    await adapter.create_interaction_response(
  File "C:\Users\adria\PycharmProjects\sblBot\venv\lib\site-packages\discord\webhook\async_.py", line 220, in request
    raise HTTPException(response, data)
discord.errors.HTTPException: 400 Bad Request (error code: 50035): Invalid Form Body
In data.components.0.components.0: Value of field "type" must be one of (4,).
Run Code Online (Sandbox Code Playgroud)

我已将代码简化为问题的最小可重现示例。这是模态框的代码:

class TestModal(discord.ui.Modal, title='Test'):

    def __init__(self, **kw):
        super().__init__(**kw)

    select = discord.ui.Select(
        placeholder='Select a tier.',
        options=[discord.SelectOption(label='test')]
    )

    async def on_submit(self, interaction: discord.Interaction):
        await interaction.response.defer()
Run Code Online (Sandbox Code Playgroud)

这是带有按钮(f)的视图的代码:

class TestButtonView(discord.ui.View):
    def __init__(self, **kw):
        super().__init__(**kw)
        self.add_buttons()

    def add_buttons(self):
        test_button = discord.ui.Button(label='Test', style=discord.ButtonStyle.blurple)

        async def test_button_callback(interaction: discord.Interaction):
            await interaction.response.send_modal(TestModal())

        test_button.callback = test_button_callback

        self.add_item(test_button)
Run Code Online (Sandbox Code Playgroud)

最后,发送按钮视图的命令:

@client.command(hidden=True)
async def test(ctx):
    await ctx.send(view=TestButtonView())
Run Code Online (Sandbox Code Playgroud)

pun*_*her 4

ui.Modal仅支持 type 的项目4,即 ui.TextInput. Meansui.Select还不是受支持的项目。
请参阅组件类型表: Discord API 文档

该错误非常不准确,但出于将来的目的,错误处理没有做得更好。
请参阅此处: 为添加到模态的不正确项目添加更好的错误