Custom Commands homeCustom Commands
  • Blog
  • Privacy
  • Premium
  • Support
Back to blog
// blog

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

·1 min read

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 image

Hooks (pre)

First create the modal

py
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

py
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

py
#! 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

py
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.

DiscordJoin support server
Custom CommandsCustom Commands

The #1 custom commands Discord bot — build commands, events and databases with zero boilerplate.

Links

HomeBlogPrivacySupport

Contact

[email protected]

2 Frederick StreetLondon, WC1X 0ND

{ / } custom commands

© WEiRDSOFT LTD. All rights reserved.