Commands¶
Info
If you're new to JDA and Discord Bots in general, please make yourself familiar with the JDA wiki first. We assume that the basic structure of interactions is known.
Slash Commands¶
SlashCommands are defined by annotating a method with @SlashCommand
.
The first parameter must always be a CommandEvent
.
The name and other metadata of the command is passed to the annotation.
@SlashCommand(value = "example", desc = "This is an example command")
public void onCommand(CommandEvent event) {...}
Sub Commands & Sub Command Groups¶
In contrast to JDA, JDA-Commands doesn't differentiate between slash commands, sub command groups and sub commands. JDA-Commands determines the type automatically based on the command names.
Let's say we have the following commands in our moderation bot:
@SlashCommand("delete")
public void onDeleteMessages(CommandEvent event) {...}
@SlashCommand("moderation warn")
public void onWarnMember(CommandEvent event) {...}
@SlashCommand("moderation kick")
public void onKickMember(CommandEvent event) {...}
@SlashCommand("moderation ban")
public void onBanMember(CommandEvent event) {...}
Debugging
JDA-Commands will log this tree on log-level DEBUG
. This might help you with debugging, for example when command
doesn't show up.
In our example the following commands will be registered:
/delete
/moderation warn
/moderation kick
/moderation ban
To simplify things, you can also use the @Interaction
to add a base name to all slash commands in a command controller:
@Interaction("moderation")
public class ModerationCommands {
@SlashCommand("warn")
public void onWarnMember(CommandEvent event) {...}
@SlashCommand("kick")
public void onKickMember(CommandEvent event) {...}
@SlashCommand("ban")
public void onBanMember(CommandEvent event) {...}
}
Command Options¶
You can add command options by simply adding a parameter to the method.
@SlashCommand("ban")
public void onBanMember(CommandEvent event, Member target, String reason, int delDays) {
(...)
}
The parameters will automatically be mapped to the correct option type. You can find this mapping here.
Name & Description¶
Use the @Param
annotation to set a name and a description for a command option. By default, the parameter name will be used as the
option name.
@SlashCommand("ban")
public void onBanMember(CommandEvent event,
@Param("The member to ban") Member target,
@Param("The reason to ban the member") String reason,
@Param(name = "deletion days", value = "The number of days to delete messages for") int delDays) {
(...)
}
Danger
In order for JDA-Commands to use the parameter name as the command option name, you must enable the -parameters
compiler flag.
If you compile your project with IntelliJ during development go to Settings > Compiler > Java Compiler
and add the -parameters
flag:
Optional¶
In order to make a command option optional, annotate the parameter with @Optional
.
You can also pass a default value that will be used (and type adapted) if no user input is present.
@SlashCommand("ban")
public void onBanMember(CommandEvent event, Member target, @Optional String reason, @Optional("7") int delDays) {
(...)
}
Note
Required options must be added before non-required options.
Choices¶
Use the @Choices
annotation to add choices to a command option:
@SlashCommand("ban")
public void onBanMember(CommandEvent event,
Member target,
@Choices({"Harassment", "Scam", "Advertising"}) String reason,
int delDays) {
(...)
}
Auto Complete¶
Failure
The Auto Complete API will be refactored soon. This wiki will cover Auto Complete as soon as the refactoring is done.
Min & Max Value¶
Use the @Min
or @Max
annotation to set the minimum and maximum value for numeral options.
Example
Context Commands¶
Both types of context commands are defined by the same @ContextCommand
annotation. The first parameter must always be a CommandEvent
.
The name and other metadata of the command is passed to the annotation.
Message Context¶
For message context commands the second method parameter must be a Message
and the type
must be Command.Type.MESSAGE
.
@ContextCommand(value = "Delete this message", type = Command.Type.MESSAGE)
public void onDeleteMessage(CommandEvent event, Message target) { ... }
User Context¶
For user context commands the second method parameter must be a User
and the type
must be Command.Type.USER
.
@ContextCommand(value = "Ban this user", type = Command.Type.USER)
public void onBanMember(CommandEvent event, User user) { ... }
Additional Settings¶
Both the @SlashCommand
annotation and the
@ContextCommand
annotation share the following fields.
isGuildOnly¶
Sets whether a command is only usable in a guild. This only has an effect if the command is registered globally.
The default value is false
.
isNSFW¶
Sets whether a command can only be executed in NSFW channels. The default value is false
.
enabledFor¶
Sets the Discord Permissions
a command will be enabled for. By default, a command will be enabled for every permission.
Danger
Guild admins can modify these permissions at any time! If you want to enforce permissions or secure a critical command further you should use the permissions system of JDA-Commands. You can read more about it here.
scope (Guild & Global Commands)¶
Sets whether a command should be registered as a global
or as a guild
command. The default value is global
.
Note
User installable apps are currently not supported by JDA-Commands.
When having guild scoped commands you have to use the GuildScopeProvider
to tell JDA-Commands what guilds a command should be registered for.
Let's say we have a paid feature in our bot:
Example
We then need to implement a GuildScopeProvider
to only register this command for guilds that have paid for that feature:
Example
Finally, we have to register our PremiumGuildsProvider
. We can either pass it to the builder:
Example
or simply annotate the PremiumGuildsProvider
class with @Implementation
.
Note
Using the @Implementation
annotation requires the guice integration (shipped by default). You can read more about it here.