cloud-jda5#
Cloud integration for JDA5 slash commands.
An example bot using cloud-jda5 can be found here.
Installation#
Cloud for JDA5 is available through Maven Central.
<dependencies>
<dependency>
<groupId>org.incendo</groupId>
<artifactId>cloud-jda5</artifactId>
<version>1.0.0-beta.3</version>
</dependency>
</dependencies>
implementation("org.incendo:cloud-jda5:1.0.0-beta.3")
implementation 'org.incendo:cloud-jda5:1.0.0-beta.3'
Usage#
Command Manager#
// Using the "native" JDAInteraction sender type:
JDA5CommandManager<JDAInteraction> commandManager = new JDA5CommandManager<>(
executionCoordinator,
JDAInteraction.InteractionMapper.identity()
);
// Using a custom sender type:
JDA5CommandManager<YourSenderType> commandManager = new JDA5CommandManager<>(
executionCoordinator,
interaction -> yourSenderType
);
where executionCoordinator is an
ExecutionCoordinator instance.
You then register the command manager as a JDA event listener. Example:
JDABuilder.createDefault(yourToken)
.addEventListeners(commandManager.createListener())
.build();
The event listener handles command synchronization, command execution and
autocompletion. You may manually synchronize commands using
JDA5CommandManager.registerGuildCommands(Guild) or
JDA5CommandManager.registerGlobalCommands(JDA).
Parsers#
Mappings to all existing option types are supported:
- INTEGER:
IntegerParser - NUMBER:
DoubleParser - BOOLEAN:
BooleanParser - STRING:
StringParser - USER:
JdaParser.userParser() - CHANNEL:
JdaParser.channelParser() - ROLE:
JdaParser.roleParser() - MENTIONABLE:
JdaParser.mentionableParser() - ATTACHMENT:
JdaParser.attachmentParser()
Subcommands and subcommand groups are mapped to Cloud literals.
Other parsers are by default be mapped to OptionType.STRING.
Choices#
The suggestion providers will be invoked for option types that support
auto completions.
You may also use constant choices by using DiscordChoices as the suggestion provider. Example:
commandBuilder.required(
"integer",
integerParser(),
DiscordChoices.integers(1, 2, 3)
)
Choices may be used for custom parsers that get mapped to OptionType.STRING.
Reply Setting#
ReplySetting may be used to have the slash command interactions be deferred automatically.
You apply the setting to the command builder using:
// Don't defer the reply:
commandBuilder.apply(ReplySetting.doNotDefer());
// Defer with an ephemeral response:
commandBuilder.apply(ReplySetting.defer(true));
// Defer with a non-ephemeral response:
commandBuilder.apply(ReplySetting.defer(false));
Annotations:#
If using annotated commands you may use the @ReplySetting annotation. You must first install
the builder modifier:
ReplySettingBuilderModifier.install(annotationParser);
You may then use the annotation:
@ReplySetting(defer = true, ephemeral = true)
@Command("command")
public void yourCommand() { /* ... */ }
Command Scopes#
You may choose where a command is registered to by using CommandScope. You apply the scope to the command builder using:
// Available everywhere (DMs, guilds, etc):
commandBuilder.apply(CommandScope.global());
// Available in all guilds:
commandBuilder.apply(CommandScope.guilds());
// Available in specific guilds:
commandBuilder.apply(CommandScope.guilds(some, guild, ids));
You may implement custom filtering by overriding the command scope predicate for the command factory:
commandManager.commandFactory()
.commandScopePredicate((node, scope) -> yourLogicHere);
Annotations:#
If using annotated commands you may use the @CommandScope annotation. You must first install the builder modifier:
CommandScopeBuilderModifier.install(annotationParser);
You may then use the annotation:
@CommandScope(guilds = { 1337 })
@Command("command")
public void yourCommand() { /* ... */ }
Permissions#
You may set the default permissions by using DiscordPermission.of(Long) as the command permission. Example:
// Using JDA's Permission helper class.
commandBuilder.permission(DiscordPermission.of(Permission.ALL_PERMISSION))
You may use ordinary permissions by setting the permission function in JDA5CommandManager.
Settings#
You may modify certain behaviors using the DiscordSettings. You may do this by
accessing the Configurable<DiscordSetting> instance using JDA5CommandManager.discordSettings().