Modals¶
You can use modal forms as an alternative form to gather user input.
To do this, you have to return a Modal
object instead of a string or a Message as the result of an interaction callback.
Constructing a Modal form¶
In order to show a Modal to an user, you must return either an instance of Modal class or subclass it yourself.
To construct them, you have to pass a Custom ID, the modal’s Title
and the fields, which must be a list of ActionRow
s.
@discord.command()
def modal_example(ctx):
fields = [
ActionRow(
TextInput(
"user_input_name",
"Enter your name"
)
)
]
return Modal("example_modal", "Hello there", fields)
Defining a modal handler¶
Modal handlers are callbacks that receive Context
objects and
can return messages as usual. However, you cannot return another Modal
as the response for a Modal.
They are defined the same way you would register a callback for a Button or another component:
@discord.command()
def modal_example(ctx):
fields = [
ActionRow(
[
TextInput(
"user_input_name",
"Enter your name"
)
]
)
]
return Modal("example_modal", "Hello there", fields)
@discord.custom_handler("example_modal")
def modal_callback(ctx):
components = ctx.components
action_row = components[0]
text_input = action_row.components[0]
name = text_input.value
return f"Hello {name}!"
There is also a convenience method for getting a specified component based on the ID:
@discord.custom_handler("example_modal")
def modal_callback(ctx):
text_input = ctx.get_component("user_input_name")
name = text_input.value
return f"Hello {name}!"
Full API¶
- class flask_discord_interactions.Modal(custom_id: Optional[Union[str, list]] = None, title: Optional[str] = None, components: Optional[List[Component]] = None)¶
Represents a Modal form window.
- components¶
A list of
ActionRow
objects representing the rows of the form. Must have at least 1 row, at most 5 rows.
- class flask_discord_interactions.TextInput(custom_id: Optional[Union[str, List]] = None, label: Optional[str] = None, style: int = 1, value: Optional[str] = None, placeholder: Optional[str] = None, min_length: int = 0, max_length: int = 4000, required: bool = True, type: int = 4)¶
Represents a TextInput modal component.
- Parameters:
label (str) – The label displayed for the field.
style (int) – The style of the text input (see
TextStyles
).value (str) – A pre-filled value for this component, max 4000 characters.
placeholder (str) – The placeholder displayed when the select menu is empty.
min_length (int) – The minimum input length for a text input, min 0, max 4000
max_length (int) – The maximum input length for a text input, min 1, max 4000
required (bool) – Whether the text input is required.