Package pw.chew.jdachewtils.command
Class OptionHelper
java.lang.Object
pw.chew.jdachewtils.command.OptionHelper
Utility class containing useful methods for getting values of Slash command arguments.
Example
public class MyCommand extends SlashCommand {
public MyCommand() {
this.name = "example";
this.help = "Example command";
this.options = Arrays.asList(
new OptionData(OptionType.STRING, "string", "A String option").setRequired(true),
new OptionData(OptionType.USER, "user", "A optional User")
);
}
@Override
protected void execute(SlashCommandEvent event) {
// get "string" option as String. Defaults to null if not found
String arg1 = OptionHelper.optString(event, "string");
// Get the provided user, or use the executor if they did not provide one
User optionalUser = OptionHelper.optUser(event, "user", event.getUser());
}
}
-
Method Summary
Modifier and TypeMethodDescriptionstatic boolean
hasOption
(@NotNull net.dv8tion.jda.api.events.interaction.SlashCommandEvent event, @NotNull String key) Will return if the provided key resolves into a provided Option for the SlashCommand.static boolean
optBoolean
(@NotNull net.dv8tion.jda.api.events.interaction.SlashCommandEvent event, @NotNull String key) Gets the provided Option Key as a boolean value, or returnsfalse
if the option cannot be found.static boolean
optBoolean
(@NotNull net.dv8tion.jda.api.events.interaction.SlashCommandEvent event, @NotNull String key, boolean defaultValue) Gets the provided Option Key as a boolean value, or returns the default one if the option cannot be found.static double
optDouble
(@NotNull net.dv8tion.jda.api.events.interaction.SlashCommandEvent event, @NotNull String key) Gets the provided Option Key as a double value, or returns0.0
if the option cannot be found.static double
optDouble
(@NotNull net.dv8tion.jda.api.events.interaction.SlashCommandEvent event, @NotNull String key, double defaultValue) Gets the provided Option Key as a double value, or returns the default one if the option cannot be found.static @Nullable net.dv8tion.jda.api.entities.GuildChannel
optGuildChannel
(@NotNull net.dv8tion.jda.api.events.interaction.SlashCommandEvent event, @NotNull String key) Gets the provided Option Key as a GuildChannel value, or returnsnull
if the option cannot be found.static @Nullable net.dv8tion.jda.api.entities.GuildChannel
optGuildChannel
(@NotNull net.dv8tion.jda.api.events.interaction.SlashCommandEvent event, @NotNull String key, @Nullable net.dv8tion.jda.api.entities.GuildChannel defaultValue) Gets the provided Option Key as a GuildChannel value, or returns the default one if the option cannot be found.static long
optLong
(@NotNull net.dv8tion.jda.api.events.interaction.SlashCommandEvent event, @NotNull String key) Gets the provided Option Key as a long value, or returns0
if the option cannot be found.static long
optLong
(@NotNull net.dv8tion.jda.api.events.interaction.SlashCommandEvent event, @NotNull String key, long defaultValue) Gets the provided Option Key as a long value, or returns the default one if the option cannot be found.static @Nullable net.dv8tion.jda.api.entities.Member
optMember
(@NotNull net.dv8tion.jda.api.events.interaction.SlashCommandEvent event, @NotNull String key) Gets the provided Option Key as a Member value, or returnsnull
if the option cannot be found.static @Nullable net.dv8tion.jda.api.entities.Member
optMember
(@NotNull net.dv8tion.jda.api.events.interaction.SlashCommandEvent event, @NotNull String key, @Nullable net.dv8tion.jda.api.entities.Member defaultValue) Gets the provided Option Key as a Member value, or returns the default one if the option cannot be found.static @Nullable net.dv8tion.jda.api.entities.IMentionable
optMentionable
(@NotNull net.dv8tion.jda.api.events.interaction.SlashCommandEvent event, @NotNull String key) Gets the provided Option Key as a IMentionable value, or returnsnull
if the option cannot be found.static @Nullable net.dv8tion.jda.api.entities.IMentionable
optMentionable
(@NotNull net.dv8tion.jda.api.events.interaction.SlashCommandEvent event, @NotNull String key, @Nullable net.dv8tion.jda.api.entities.IMentionable defaultValue) Gets the provided Option Key as a IMentionable value, or returns the default one if the option cannot be found.static @Nullable net.dv8tion.jda.api.entities.MessageChannel
optMessageChannel
(@NotNull net.dv8tion.jda.api.events.interaction.SlashCommandEvent event, @NotNull String key) Gets the provided Option Key as a MessageChannel value, or returnsnull
if the option cannot be found.static @Nullable net.dv8tion.jda.api.entities.MessageChannel
optMessageChannel
(@NotNull net.dv8tion.jda.api.events.interaction.SlashCommandEvent event, @NotNull String key, @Nullable net.dv8tion.jda.api.entities.MessageChannel defaultValue) Gets the provided Option Key as a MessageChannel value, or returns the default one if the option cannot be found.static @Nullable net.dv8tion.jda.api.entities.Role
optRole
(@NotNull net.dv8tion.jda.api.events.interaction.SlashCommandEvent event, @NotNull String key) Gets the provided Option Key as a Role value, or returnsnull
if the option cannot be found.static @Nullable net.dv8tion.jda.api.entities.Role
optRole
(@NotNull net.dv8tion.jda.api.events.interaction.SlashCommandEvent event, @NotNull String key, @Nullable net.dv8tion.jda.api.entities.Role defaultValue) Gets the provided Option Key as a Role value, or returns the default one if the option cannot be found.static @Nullable String
optString
(@NotNull net.dv8tion.jda.api.events.interaction.SlashCommandEvent event, @NotNull String key) Gets the provided Option Key as a String value, or returnsnull
if the option cannot be found.static @Nullable String
optString
(@NotNull net.dv8tion.jda.api.events.interaction.SlashCommandEvent event, @NotNull String key, @Nullable String defaultValue) Gets the provided Option Key as a String value, or returns the default one if the option cannot be found.static @Nullable net.dv8tion.jda.api.entities.User
optUser
(@NotNull net.dv8tion.jda.api.events.interaction.SlashCommandEvent event, @NotNull String key) Gets the provided Option Key as a User value, or returnsnull
if the option cannot be found.static @Nullable net.dv8tion.jda.api.entities.User
optUser
(@NotNull net.dv8tion.jda.api.events.interaction.SlashCommandEvent event, @NotNull String key, @Nullable net.dv8tion.jda.api.entities.User defaultValue) Gets the provided Option Key as a User value, or returns the default one if the option cannot be found.
-
Method Details
-
optString
@Nullable public static @Nullable String optString(@NotNull @NotNull net.dv8tion.jda.api.events.interaction.SlashCommandEvent event, @NotNull @NotNull String key) Gets the provided Option Key as a String value, or returnsnull
if the option cannot be found.- Parameters:
event
- The slash command event to get options fromkey
- The option we want- Returns:
- The provided option, or null if the option is not present
-
optString
@Nullable @Contract("_, _, !null -> !null") public static @Nullable String optString(@NotNull @NotNull net.dv8tion.jda.api.events.interaction.SlashCommandEvent event, @NotNull @NotNull String key, @Nullable @Nullable String defaultValue) Gets the provided Option Key as a String value, or returns the default one if the option cannot be found.- Parameters:
event
- The slash command event to get options fromkey
- The option we wantdefaultValue
- Nullable default value used in the absence of the option value- Returns:
- The provided option, or the default value if the option is not present
-
optBoolean
public static boolean optBoolean(@NotNull @NotNull net.dv8tion.jda.api.events.interaction.SlashCommandEvent event, @NotNull @NotNull String key) Gets the provided Option Key as a boolean value, or returnsfalse
if the option cannot be found.- Parameters:
event
- The slash command event to get options fromkey
- The option we want- Returns:
- The provided option, or false if the option is not present
-
optBoolean
public static boolean optBoolean(@NotNull @NotNull net.dv8tion.jda.api.events.interaction.SlashCommandEvent event, @NotNull @NotNull String key, boolean defaultValue) Gets the provided Option Key as a boolean value, or returns the default one if the option cannot be found.- Parameters:
event
- The slash command event to get options fromkey
- The option we wantdefaultValue
- The fallback option in case of the absence of the option value- Returns:
- The provided option, or the default value if the option is not present
-
optLong
public static long optLong(@NotNull @NotNull net.dv8tion.jda.api.events.interaction.SlashCommandEvent event, @NotNull @NotNull String key) Gets the provided Option Key as a long value, or returns0
if the option cannot be found.- Parameters:
event
- The slash command event to get options fromkey
- The option we want- Returns:
- The provided option, or 0 if the option is not present
-
optLong
public static long optLong(@NotNull @NotNull net.dv8tion.jda.api.events.interaction.SlashCommandEvent event, @NotNull @NotNull String key, long defaultValue) Gets the provided Option Key as a long value, or returns the default one if the option cannot be found.- Parameters:
event
- The slash command event to get options fromkey
- The option we wantdefaultValue
- The fallback option in case of the absence of the option value- Returns:
- The provided option, or the default value if the option is not present
-
optDouble
public static double optDouble(@NotNull @NotNull net.dv8tion.jda.api.events.interaction.SlashCommandEvent event, @NotNull @NotNull String key) Gets the provided Option Key as a double value, or returns0.0
if the option cannot be found.- Parameters:
event
- The slash command event to get options fromkey
- The option we want- Returns:
- The provided option, or 0.0 if the option is not present
-
optDouble
public static double optDouble(@NotNull @NotNull net.dv8tion.jda.api.events.interaction.SlashCommandEvent event, @NotNull @NotNull String key, double defaultValue) Gets the provided Option Key as a double value, or returns the default one if the option cannot be found.- Parameters:
event
- The slash command event to get options fromkey
- The option we wantdefaultValue
- The fallback option in case of the absence of the option value- Returns:
- The provided option, or the default value if the option is not present
-
optGuildChannel
@Nullable public static @Nullable net.dv8tion.jda.api.entities.GuildChannel optGuildChannel(@NotNull @NotNull net.dv8tion.jda.api.events.interaction.SlashCommandEvent event, @NotNull @NotNull String key) Gets the provided Option Key as a GuildChannel value, or returnsnull
if the option cannot be found.
This will always return null when the SlashCommandEvent was not executed in a Guild.- Parameters:
event
- The slash command event to get options fromkey
- The option we want- Returns:
- The provided option, or null if the option is not present
-
optGuildChannel
@Nullable @Contract("_, _, !null -> !null") public static @Nullable net.dv8tion.jda.api.entities.GuildChannel optGuildChannel(@NotNull @NotNull net.dv8tion.jda.api.events.interaction.SlashCommandEvent event, @NotNull @NotNull String key, @Nullable @Nullable net.dv8tion.jda.api.entities.GuildChannel defaultValue) Gets the provided Option Key as a GuildChannel value, or returns the default one if the option cannot be found.
This will always return the default value when the SlashCommandEvent was not executed in a Guild.- Parameters:
event
- The slash command event to get options fromkey
- The option we wantdefaultValue
- Nullable default value used in the absence of the option value- Returns:
- The provided option, or the default value if the option is not present
-
optMember
@Nullable public static @Nullable net.dv8tion.jda.api.entities.Member optMember(@NotNull @NotNull net.dv8tion.jda.api.events.interaction.SlashCommandEvent event, @NotNull @NotNull String key) Gets the provided Option Key as a Member value, or returnsnull
if the option cannot be found.
This will always return null when the SlashCommandEvent was not executed in a Guild.- Parameters:
event
- The slash command event to get options fromkey
- The option we want- Returns:
- The provided option, or null if the option is not present
-
optMember
@Nullable @Contract("_, _, !null -> !null") public static @Nullable net.dv8tion.jda.api.entities.Member optMember(@NotNull @NotNull net.dv8tion.jda.api.events.interaction.SlashCommandEvent event, @NotNull @NotNull String key, @Nullable @Nullable net.dv8tion.jda.api.entities.Member defaultValue) Gets the provided Option Key as a Member value, or returns the default one if the option cannot be found.
This will always return the default value when the SlashCommandEvent was not executed in a Guild.- Parameters:
event
- The slash command event to get options fromkey
- The option we wantdefaultValue
- Nullable default value used in the absence of the option value- Returns:
- The provided option, or the default value if the option is not present
-
optMentionable
@Nullable public static @Nullable net.dv8tion.jda.api.entities.IMentionable optMentionable(@NotNull @NotNull net.dv8tion.jda.api.events.interaction.SlashCommandEvent event, @NotNull @NotNull String key) Gets the provided Option Key as a IMentionable value, or returnsnull
if the option cannot be found.- Parameters:
event
- The slash command event to get options fromkey
- The option we want- Returns:
- The provided option, or null if the option is not present
-
optMentionable
@Nullable @Contract("_, _, !null -> !null") public static @Nullable net.dv8tion.jda.api.entities.IMentionable optMentionable(@NotNull @NotNull net.dv8tion.jda.api.events.interaction.SlashCommandEvent event, @NotNull @NotNull String key, @Nullable @Nullable net.dv8tion.jda.api.entities.IMentionable defaultValue) Gets the provided Option Key as a IMentionable value, or returns the default one if the option cannot be found.- Parameters:
event
- The slash command event to get options fromkey
- The option we wantdefaultValue
- Nullable default value used in the absence of the option value- Returns:
- The provided option, or the default value if the option is not present
-
optRole
@Nullable public static @Nullable net.dv8tion.jda.api.entities.Role optRole(@NotNull @NotNull net.dv8tion.jda.api.events.interaction.SlashCommandEvent event, @NotNull @NotNull String key) Gets the provided Option Key as a Role value, or returnsnull
if the option cannot be found.
This will always return null when the SlashCommandEvent was not executed in a Guild.- Parameters:
event
- The slash command event to get options fromkey
- The option we want- Returns:
- The provided option, or null if the option is not present
-
optRole
@Nullable @Contract("_, _, !null -> !null") public static @Nullable net.dv8tion.jda.api.entities.Role optRole(@NotNull @NotNull net.dv8tion.jda.api.events.interaction.SlashCommandEvent event, @NotNull @NotNull String key, @Nullable @Nullable net.dv8tion.jda.api.entities.Role defaultValue) Gets the provided Option Key as a Role value, or returns the default one if the option cannot be found.
This will always return the default value when the SlashCommandEvent was not executed in a Guild.- Parameters:
event
- The slash command event to get options fromkey
- The option we wantdefaultValue
- Nullable default value used in the absence of the option value- Returns:
- The provided option, or the default value if the option is not present
-
optUser
@Nullable public static @Nullable net.dv8tion.jda.api.entities.User optUser(@NotNull @NotNull net.dv8tion.jda.api.events.interaction.SlashCommandEvent event, @NotNull @NotNull String key) Gets the provided Option Key as a User value, or returnsnull
if the option cannot be found.- Parameters:
event
- The slash command event to get options fromkey
- The option we want- Returns:
- The provided option, or null if the option is not present
-
optUser
@Nullable @Contract("_, _, !null -> !null") public static @Nullable net.dv8tion.jda.api.entities.User optUser(@NotNull @NotNull net.dv8tion.jda.api.events.interaction.SlashCommandEvent event, @NotNull @NotNull String key, @Nullable @Nullable net.dv8tion.jda.api.entities.User defaultValue) Gets the provided Option Key as a User value, or returns the default one if the option cannot be found.- Parameters:
event
- The slash command event to get options fromkey
- The option we wantdefaultValue
- Nullable default value used in the absence of the option value- Returns:
- The provided option, or the default value if the option is not present
-
optMessageChannel
@Nullable public static @Nullable net.dv8tion.jda.api.entities.MessageChannel optMessageChannel(@NotNull @NotNull net.dv8tion.jda.api.events.interaction.SlashCommandEvent event, @NotNull @NotNull String key) Gets the provided Option Key as a MessageChannel value, or returnsnull
if the option cannot be found.- Parameters:
event
- The slash command event to get options fromkey
- The option we want- Returns:
- The provided option, or null if the option is not present
-
optMessageChannel
@Nullable @Contract("_, _, !null -> !null") public static @Nullable net.dv8tion.jda.api.entities.MessageChannel optMessageChannel(@NotNull @NotNull net.dv8tion.jda.api.events.interaction.SlashCommandEvent event, @NotNull @NotNull String key, @Nullable @Nullable net.dv8tion.jda.api.entities.MessageChannel defaultValue) Gets the provided Option Key as a MessageChannel value, or returns the default one if the option cannot be found.- Parameters:
event
- The slash command event to get options fromkey
- The option we wantdefaultValue
- Nullable default value used in the absence of the option value- Returns:
- The provided option, or the default value if the option is not present
-
hasOption
public static boolean hasOption(@NotNull @NotNull net.dv8tion.jda.api.events.interaction.SlashCommandEvent event, @NotNull @NotNull String key) Will return if the provided key resolves into a provided Option for the SlashCommand.- Parameters:
event
- the slash command event to get options fromkey
- the option we want- Returns:
- true if the option exists, false otherwise
-