Interface SlashCommandHandler
public interface SlashCommandHandler
Handler for slash command interactions.
Preferred approach: Use the @SlashCommand annotation directly on methods
in your @Plugin class:
@Plugin(name = "MyPlugin", version = "1.0.0", author = "Author")
public class MyPlugin {
@SlashCommand(name = "ping", description = "Check bot latency")
public void ping(SlashCommandInteractionEvent event) {
long ping = event.getJDA().getGatewayPing();
event.reply("Pong! " + ping + "ms").queue();
}
}
Alternative: Implement this interface and register via InteractionManager
for more control (e.g., custom command data, guild-specific registration):
public class PingCommand implements SlashCommandHandler {
@Override
public SlashCommandData getCommandData() {
return Commands.slash("ping", "Check bot latency");
}
@Override
public void handle(SlashCommandInteractionEvent event) {
long ping = event.getJDA().getGatewayPing();
event.reply("Pong! " + ping + "ms").queue();
}
}
-
Method Summary
Modifier and TypeMethodDescriptionnet.dv8tion.jda.api.interactions.commands.build.SlashCommandDataGet the slash command data for registration.default long[]Get the guild IDs where this command should be registered.voidhandle(net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent event) Handle the slash command interaction.default booleanisGlobal()Whether this command should be registered globally or per-guild.
-
Method Details
-
getCommandData
net.dv8tion.jda.api.interactions.commands.build.SlashCommandData getCommandData()Get the slash command data for registration.Use
Commands.slash(String, String)to create the command data.- Returns:
- the slash command data
-
handle
void handle(net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent event) Handle the slash command interaction.You must respond to the interaction within 3 seconds using one of: -
event.reply(...)-event.deferReply()followed byevent.getHook().sendMessage(...)- Parameters:
event- the slash command event
-
isGlobal
default boolean isGlobal()Whether this command should be registered globally or per-guild.Global commands can take up to 1 hour to propagate. Guild commands are instant but only work in that guild.
- Returns:
- true for global registration, false for guild-only
-
getGuildIds
default long[] getGuildIds()Get the guild IDs where this command should be registered.Only used if
isGlobal()returns false. Return null or empty array to register in all guilds the bot is in.- Returns:
- array of guild IDs, or null for all guilds
-