Value of field "type" must be one of (4,) in a Modal using discord.py 2.0
Question:
I am trying to show the user a Modal after they display a button, which contains a dropdown select menu from which they can choose multiple options. This code has functioned in the past, but is not causing an exception. Specifically:
[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:UsersadriaPycharmProjectssblBotvenvlibsite-packagesdiscorduiview.py", line 425, in _scheduled_task
await item.callback(interaction)
File "C:UsersadriaPycharmProjectssblBotmain.py", line 1131, in test_button_callback
await interaction.response.send_modal(TestModal())
File "C:UsersadriaPycharmProjectssblBotvenvlibsite-packagesdiscordinteractions.py", line 852, in send_modal
await adapter.create_interaction_response(
File "C:UsersadriaPycharmProjectssblBotvenvlibsite-packagesdiscordwebhookasync_.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,).
I have reduced my code to the minimal reproducible example of my issue. Here is the code for the Modal:
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()
And here is the code for the view with the button (the 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)
And finally, the command to send the button view:
@client.command(hidden=True)
async def test(ctx):
await ctx.send(view=TestButtonView())
Answers:
ui.Modal
does only support items with type 4
, which is a ui.TextInput
. Means ui.Select
is not a supported item (yet).
See Component Types table: 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
I am trying to show the user a Modal after they display a button, which contains a dropdown select menu from which they can choose multiple options. This code has functioned in the past, but is not causing an exception. Specifically:
[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:UsersadriaPycharmProjectssblBotvenvlibsite-packagesdiscorduiview.py", line 425, in _scheduled_task
await item.callback(interaction)
File "C:UsersadriaPycharmProjectssblBotmain.py", line 1131, in test_button_callback
await interaction.response.send_modal(TestModal())
File "C:UsersadriaPycharmProjectssblBotvenvlibsite-packagesdiscordinteractions.py", line 852, in send_modal
await adapter.create_interaction_response(
File "C:UsersadriaPycharmProjectssblBotvenvlibsite-packagesdiscordwebhookasync_.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,).
I have reduced my code to the minimal reproducible example of my issue. Here is the code for the Modal:
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()
And here is the code for the view with the button (the 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)
And finally, the command to send the button view:
@client.command(hidden=True)
async def test(ctx):
await ctx.send(view=TestButtonView())
ui.Modal
does only support items with type 4
, which is a ui.TextInput
. Means ui.Select
is not a supported item (yet).
See Component Types table: 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