我试图向用户显示一个模式后,他们显示一个按钮,其中包含一个下拉 Select 菜单,他们可以从中 Select 多个选项.此代码在过go 运行过,但不会导致异常.具体地说,就是:

[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,).

我已经将我的代码减少到我的问题的最小可重现示例.以下是莫代尔的代码:

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()

下面是带有按钮(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)

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

@client.command(hidden=True)
async def test(ctx):
    await ctx.send(view=TestButtonView())

推荐答案

ui.Modal只支持ui.TextInput作为项.装置ui.Select不是受支持的项目(还没有).

This error means that only items are allowed with type 4. Type 4 is a text input.
Reference: Discord API Documentation

The error is quite inaccurate, but it's intended that the error handling is not done better because of future purposes.
See here: Add better errors for incorrect items added to modal

Python相关问答推荐

如何随着收件箱的增加动态添加到HTML表的右下角?

如何最好地处理严重级联的json

如何将 map 数组组合到pyspark中每列的单个 map 中

调试回归无法解决我的问题

如何使用scikit-learn Python库中的Agglomerative集群算法以及集群中声明的对象数量?

如何使用函数正确索引收件箱?

Flask:如何在完整路由代码执行之前返回验证

如何知道标志是否由用户传递或具有默认值?

有没有方法可以修复删除了换码字符的无效的SON记录?

使用FASTCGI在IIS上运行Django频道

韦尔福德方差与Numpy方差不同

将jit与numpy linSpace函数一起使用时出错

如何制作10,000年及以后的日期时间对象?

如何在给定的条件下使numpy数组的计算速度最快?

pyscript中的压痕问题

计算天数

如何在Pyplot表中舍入值

Python全局变量递归得到不同的结果

未调用自定义JSON编码器

下三角形掩码与seaborn clustermap bug