Interactions
In Dawncord, you can interact with Discord through various means, including slash commands, message components, and modals. This guide will show you how to handle each type of interaction.
Slash Command Interaction
Slash commands allow users to interact with your bot by typing a command prefixed with a forward slash (/) in Discord.
Registering Slash Commands
To register a slash command, you can use the registerSlashCommands method of the Dawncord class. Here's how you can register a slash command named "ping":
Example
Using Slash Commands
To use slash commands in your Discord bot, you can follow this example code snippet:
Example
In the first example, the bot checks the command name inside the onSlashCommand event handler. In the second example, the command name is directly provided in the onSlashCommand method, so the bot doesn't need to check it inside the event handler.
Handling Slash Command options
Here are two examples how to handle Slash command options in your Discord bot:
Example
Handling options
Example
Subcommands
Subcommands in Discord's slash commands allow you to organize related functionalities under a single parent command, streamlining command structure and enhancing user experience.
To use subcommands in your Discord bot, you can follow this example code snippet:
Example
Subcommand Groups
Subcommand groups in Discord's slash commands allow you to organize related subcommands under a common category. They provide a hierarchical structure for your commands, enhancing organization and clarity.
To use subcommand groups in your Discord bot, you can follow this example code snippet:
Example
SlashCommand command = new SlashCommandBuilder("user", "User")
.addSubCommandGroup(
new SubCommandGroupBuilder("list", "List")
.addSubCommand(
new SubCommandBuilder("offline", "List offline users").build())
.addSubCommand(
new SubCommandBuilder("online", "List online users").build())
.build())
.build();
Message Component Interaction
Message components allow users to interact with messages by clicking buttons, selecting options from dropdown menus, etc.
Button Component
Button components allow users to trigger actions by clicking on buttons embedded within messages.
To use button interactions in your Discord bot, you can follow this example code snippet:
Example
Select Menu Component
Select menu components, also known as dropdown menus, allow users to choose from a list of options presented in a dropdown menu format.
To use select menu interactions in your Discord bot, you can follow this example code snippet:
Example
bot.onSlashCommand("selectmenu", event -> {
event.reply(message -> {
message.setComponents(
Component.actionRow(
SelectMenu.create(SelectMenuType.USER, "user-select")
)
);
});
});
bot.onSelectMenu("user-select", event -> {
event.reply("Selected: " + event.getValues().asMembers().get(0).getUser().getUsername());
});
Modal Interaction
Modal components allow users to interact with your bot through dialog boxes or pop-up modals, enabling more complex interactions.
To create modal, you can follow this example code snippet:
Example
bot.onSlashCommand("modal", event -> {
Modal modal = new ModalBuilder("Submit bag", "submit-bag",
List.of(
new Element("Title", "title", TextInputStyle.SHORT),
new Element("Operating system", "os", TextInputStyle.SHORT),
new Element("Browser", "browser", TextInputStyle.SHORT),
new Element("Details", "details", TextInputStyle.PARAGRAPH)
))
.build();
event.replyModal(modal);
});
bot.onSlashCommand("modal", event -> {
event.replyModal(modal -> {
modal.setTitle("Submit Bag");
modal.setCustomId("submit-bag");
modal.setElements(
new Element("Title", "title", TextInputStyle.SHORT),
new Element("Operating system", "os", TextInputStyle.SHORT),
new Element("Browser", "browser", TextInputStyle.SHORT),
new Element("Details", "details", TextInputStyle.PARAGRAPH)
);
});
});
Respond to modal