Create a modal command using custom commands bot
in this blog we will learn how we can create a command that sends a modal
Md Shahriyar Alam
2 years ago
Custom Commands setup
First create a command and give it any name and description you want
Then disable Auto Defer so that the command does not get automatically deferred

Hooks (pre)
First create the modal
modal = ModalBuilder(title="Test Modal", custom_id="test_modal")
modal.add_text_input(
name="name",
label="Country",
placeholder="What country do you live in?"
)
modal.add_text_input(
name="description",
label="Description",
placeholder="Write something about your country",
style="long",
)I have added 2 input fields name and description and also gave the modal a custom_id test_modal you can change the custom id and make your own fields
Then you have to send the modal
await send_modal(modal)After that you can handle the modal submission using an Event or you can handle it inside the command's hooks
Handle using Event
if you want to handle the modal using events
#! test_modal
name = values["name"]
description = values["description"]
# Do what you want to do with these variables
defer_response(ephemeral=True)
respond_interaction(interaction, content="Thank you for submitting the modal")By the upper code you can access the values and do whatever you want
Handle using pre hooks
if you want to handle using pre hook
def check(event):
return (
event.interaction.user.id == user.id and
event.interaction.custom_id == "test_modal" # use your own custom id here
)
event = await wait_for("modal_submit", check=check)
values = get_modal_values(event.interaction)
await defer(event.interaction, ephemeral=False)
respond_interaction(
event.interaction,
content=f"Name: {values['name']}\nDescription: {values['description']}"
)Loading comments…
Need help?
Have a question, a suggestion, or stuck on something? Reach out — we're happy to help.