Interface CommandInteractionPayload
- All Superinterfaces:
Interaction
,ISnowflake
- All Known Subinterfaces:
CommandAutoCompleteInteraction
,CommandInteraction
,ContextInteraction<T>
,MessageContextInteraction
,SlashCommandInteraction
,UserContextInteraction
- All Known Implementing Classes:
CommandAutoCompleteInteractionEvent
,GenericCommandInteractionEvent
,GenericContextInteractionEvent
,MessageContextInteractionEvent
,SlashCommandInteractionEvent
,UserContextInteractionEvent
This is an abstraction for
CommandAutoCompleteInteraction
and CommandInteraction
.-
Method Summary
Modifier and TypeMethodDescriptiondefault String
The command id
This is the id generated when a command is created viaGuild.updateCommands()
or similar.long
The command id.default String
Gets the display string for this command.TheType
of command this interaction is for.default String
getName()
The command name.default OptionMapping
Finds the first option with the specified name.default <T> T
getOption
(String name, Function<? super OptionMapping, ? extends T> resolver) Finds the first option with the specified name.default <T> T
getOption
(String name, Supplier<? extends T> fallback, Function<? super OptionMapping, ? extends T> resolver) Finds the first option with the specified name.default <T> T
getOption
(String name, T fallback, Function<? super OptionMapping, ? extends T> resolver) Finds the first option with the specified name.The options provided by the user when this command was executed.default List<OptionMapping>
getOptionsByName
(String name) Gets all options for the specified name.default List<OptionMapping>
getOptionsByType
(OptionType type) Gets all options for the specified type.The subcommand group name.The subcommand name.default boolean
Whether the used command is a global command.boolean
Whether the used command is a guild command.Methods inherited from interface net.dv8tion.jda.api.interactions.Interaction
getChannel, getChannelId, getChannelIdLong, getChannelType, getEntitlements, getGuild, getGuildChannel, getGuildLocale, getJDA, getMember, getMessageChannel, getToken, getType, getTypeRaw, getUser, getUserLocale, isAcknowledged, isFromGuild
Methods inherited from interface net.dv8tion.jda.api.entities.ISnowflake
getId, getIdLong, getTimeCreated
-
Method Details
-
getCommandType
TheType
of command this interaction is for.- Returns:
- The command type
-
getName
The command name.
This can be useful for abstractions.Note that commands can have these following structures:
/name subcommandGroup subcommandName
/name subcommandName
/name
getFullCommandName()
to simplify your checks.- Returns:
- The command name
-
getSubcommandName
The subcommand name.
This can be useful for abstractions.Note that commands can have these following structures:
/name subcommandGroup subcommandName
/name subcommandName
/name
getFullCommandName()
to simplify your checks.- Returns:
- The subcommand name, or null if this is not a subcommand
-
getSubcommandGroup
The subcommand group name.
This can be useful for abstractions.Note that commands can have these following structures:
/name subcommandGroup subcommandName
/name subcommandName
/name
getFullCommandName()
to simplify your checks.- Returns:
- The subcommand group name, or null if this is not a subcommand group
-
getFullCommandName
Combination ofgetName()
,getSubcommandGroup()
, andgetSubcommandName()
.
This will format the command into a path such asmod mute
wheremod
would be thegetName()
andmute
thegetSubcommandName()
.Examples:
/mod ban -> "mod ban"
/admin config owner -> "admin config owner"
/ban -> "ban"
- Returns:
- The command path
-
getCommandString
Gets the display string for this command.
This is similar to the string you see when clicking the interaction name in the client. For non-slash command types, this simply returnsgetName()
instead.Example return for an echo command:
/say echo phrase: Say this
- Returns:
- The display string for this command
-
getCommandIdLong
long getCommandIdLong()The command id.
This is the id generated when a command is created viaGuild.updateCommands()
or similar.It is usually preferred to discriminate commands by the
command names
instead.- Returns:
- The command id
-
getCommandId
The command id
This is the id generated when a command is created viaGuild.updateCommands()
or similar.It is usually preferred to discriminate commands by the
command names
instead.- Returns:
- The command id
-
isGuildCommand
boolean isGuildCommand()Whether the used command is a guild command.Guild commands can be created with
Guild.upsertCommand(CommandData)
.- Returns:
- True, if the used command is a guild command
-
isGlobalCommand
default boolean isGlobalCommand()Whether the used command is a global command.Global commands can be created with
JDA.upsertCommand(CommandData)
.- Returns:
- True, if the used command is a global command
-
getOptions
The options provided by the user when this command was executed.
Each option has a name and value.For
CommandAutoCompleteInteraction
, this might be incomplete and unvalidated. Auto-complete interactions happen on incomplete command inputs and are not validated.- Returns:
- The options passed for this command
- See Also:
-
getOptionsByName
Gets all options for the specified name.For
CommandAutoCompleteInteraction
, this might be incomplete and unvalidated. Auto-complete interactions happen on incomplete command inputs and are not validated.- Parameters:
name
- The option name- Returns:
- The list of options
- Throws:
IllegalArgumentException
- If the provided name is null- See Also:
-
getOptionsByType
Gets all options for the specified type.For
CommandAutoCompleteInteraction
, this might be incomplete and unvalidated. Auto-complete interactions happen on incomplete command inputs and are not validated.- Parameters:
type
- The option type- Returns:
- The list of options
- Throws:
IllegalArgumentException
- If the provided type is null- See Also:
-
getOption
Finds the first option with the specified name.For
CommandAutoCompleteInteraction
, this might be incomplete and unvalidated. Auto-complete interactions happen on incomplete command inputs and are not validated.You can use the second and third parameter overloads to handle optional arguments gracefully. See
getOption(String, Function)
andgetOption(String, Object, Function)
.- Parameters:
name
- The option name- Returns:
- The option with the provided name, or null if that option is not provided
- Throws:
IllegalArgumentException
- If the name is null- See Also:
-
getOption
@Nullable default <T> T getOption(@Nonnull String name, @Nonnull Function<? super OptionMapping, ? extends T> resolver) Finds the first option with the specified name.
A resolver is used to get the value if the option is provided. If no option is provided for the given name, this will simply return null instead. You can usegetOption(String, Object, Function)
to provide a fallback for missing options.For
CommandAutoCompleteInteraction
, this might be incomplete and unvalidated. Auto-complete interactions happen on incomplete command inputs and are not validated.Example
You can understand this as a shortcut for these lines of code:
Which can be written with this resolver as:OptionMapping opt = event.getOption("reason"); String reason = opt == null ? null : opt.getAsString();
String reason = event.getOption("reason", OptionMapping::getAsString);
- Type Parameters:
T
- The type of the resolved option value- Parameters:
name
- The option nameresolver
- The mapping resolver function to use if there is a mapping available, the provided mapping will never be null!- Returns:
- The resolved option with the provided name, or null if that option is not provided
- Throws:
IllegalArgumentException
- If the name or resolver is null- See Also:
-
getOption
@Contract("_,null,_->null") default <T> T getOption(@Nonnull String name, @Nullable T fallback, @Nonnull Function<? super OptionMapping, ? extends T> resolver) Finds the first option with the specified name.
A resolver is used to get the value if the option is provided. If no option is provided for the given name, this will simply return your provided fallback instead. You can usegetOption(String, Function)
to fall back tonull
.For
CommandAutoCompleteInteraction
, this might be incomplete and unvalidated. Auto-complete interactions happen on incomplete command inputs and are not validated.Example
You can understand this as a shortcut for these lines of code:
Which can be written with this resolver as:OptionMapping opt = event.getOption("reason"); String reason = opt == null ? "ban by mod" : opt.getAsString();
String reason = event.getOption("reason", "ban by mod", OptionMapping::getAsString);
- Type Parameters:
T
- The type of the resolved option value- Parameters:
name
- The option namefallback
- The fallback to use if the option is not provided, meaninggetOption(String)
returns nullresolver
- The mapping resolver function to use if there is a mapping available, the provided mapping will never be null!- Returns:
- The resolved option with the provided name, or
fallback
if that option is not provided - Throws:
IllegalArgumentException
- If the name or resolver is null- See Also:
-
getOption
@UnknownNullability default <T> T getOption(@Nonnull String name, @Nullable Supplier<? extends T> fallback, @Nonnull Function<? super OptionMapping, ? extends T> resolver) Finds the first option with the specified name.
A resolver is used to get the value if the option is provided. If no option is provided for the given name, this will simply return your provided fallback instead. You can usegetOption(String, Function)
to fall back tonull
.For
CommandAutoCompleteInteraction
, this might be incomplete and unvalidated. Auto-complete interactions happen on incomplete command inputs and are not validated.Example
You can understand this as a shortcut for these lines of code:
Which can be written with this resolver as:OptionMapping opt = event.getOption("reason"); String reason = opt == null ? context.getFallbackReason() : opt.getAsString();
String reason = event.getOption("reason", context::getFallbackReason , OptionMapping::getAsString);
- Type Parameters:
T
- The type of the resolved option value- Parameters:
name
- The option namefallback
- The fallback supplier to use if the option is not provided, meaninggetOption(String)
returns nullresolver
- The mapping resolver function to use if there is a mapping available, the provided mapping will never be null!- Returns:
- The resolved option with the provided name, or
fallback
if that option is not provided - Throws:
IllegalArgumentException
- If the name or resolver is null- See Also:
-