diff --git a/command/src/main/java/pw/chew/jdachewtils/command/OptionHelper.java b/command/src/main/java/pw/chew/jdachewtils/command/OptionHelper.java new file mode 100644 index 00000000..c8ac0259 --- /dev/null +++ b/command/src/main/java/pw/chew/jdachewtils/command/OptionHelper.java @@ -0,0 +1,202 @@ +/* + * Copyright 2016-2021 John Grosh (jagrosh) & Kaidan Gustave (TheMonitorLizard) & Olivia (Chew) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package pw.chew.jdachewtils.command; + +import net.dv8tion.jda.api.entities.GuildChannel; +import net.dv8tion.jda.api.entities.IMentionable; +import net.dv8tion.jda.api.entities.Member; +import net.dv8tion.jda.api.entities.MessageChannel; +import net.dv8tion.jda.api.entities.Role; +import net.dv8tion.jda.api.entities.User; +import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; +import net.dv8tion.jda.api.interactions.commands.OptionMapping; +import org.jetbrains.annotations.Contract; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.List; + +/** + * A collection of useful methods for working with Options. + */ +public final class OptionHelper { + private OptionHelper() {} + + /** + * Guarantees a String option value by providing a default value. + * + * @param event The slash command event to get options from + * @param option The option we want + * @param defaultValue The fallback option in case of the absence of the option value + * @return The provided option, or the default value if the option is not present + */ + @Nullable + @Contract("_, _, !null -> !null") + public static String optString(@NotNull SlashCommandEvent event, @NotNull String option, @Nullable String defaultValue) { + List options = event.getOptionsByName(option); + + return options.isEmpty() ? defaultValue : options.get(0).getAsString(); + } + + /** + * Guarantees a boolean option value by providing a default value. + * + * @param event The slash command event to get options from + * @param option The option we want + * @param defaultValue The fallback option in case of the absence of the option value + * @return The provided option, or the default value if the option is not present + */ + public static boolean optBoolean(@NotNull SlashCommandEvent event, @NotNull String option, boolean defaultValue) { + List options = event.getOptionsByName(option); + + return options.isEmpty() ? defaultValue : options.get(0).getAsBoolean(); + } + + /** + * Guarantees a long option value by providing a default value. + * + * @param event The slash command event to get options from + * @param option The option we want + * @param defaultValue The fallback option in case of the absence of the option value + * @return The provided option, or the default value if the option is not present + */ + public static long optLong(@NotNull SlashCommandEvent event, @NotNull String option, long defaultValue) { + List options = event.getOptionsByName(option); + + return options.isEmpty() ? defaultValue : options.get(0).getAsLong(); + } + + /** + * Guarantees a double option value by providing a default value. + * + * @param event The slash command event to get options from + * @param option The option we want + * @param defaultValue The fallback option in case of the absence of the option value + * @return The provided option, or the default value if the option is not present + */ + public static double optDouble(@NotNull SlashCommandEvent event, @NotNull String option, double defaultValue) { + List options = event.getOptionsByName(option); + + return options.isEmpty() ? defaultValue : options.get(0).getAsDouble(); + } + + /** + * Guarantees a Guild Channel option value by providing a default value. + * + * @param event The slash command event to get options from + * @param option The option we want + * @param defaultValue The fallback option in case of the absence of the option value + * @return The provided option, or the default value if the option is not present + */ + @Nullable + @Contract("_, _, !null -> !null") + public static GuildChannel optGuildChannel(@NotNull SlashCommandEvent event, @NotNull String option, @Nullable GuildChannel defaultValue) { + List options = event.getOptionsByName(option); + + return options.isEmpty() ? defaultValue : options.get(0).getAsGuildChannel(); + } + + /** + * Guarantees a Member option value by providing a default value. + * + * @param event The slash command event to get options from + * @param option The option we want + * @param defaultValue The fallback option in case of the absence of the option value + * @return The provided option, or the default value if the option is not present + */ + @Nullable + @Contract("_, _, !null -> !null") + public static Member optMember(@NotNull SlashCommandEvent event, @NotNull String option, @Nullable Member defaultValue) { + List options = event.getOptionsByName(option); + + return options.isEmpty() ? defaultValue : options.get(0).getAsMember(); + } + + /** + * Guarantees a IMentionable option value by providing a default value. + * + * @param event The slash command event to get options from + * @param option The option we want + * @param defaultValue The fallback option in case of the absence of the option value + * @return The provided option, or the default value if the option is not present + */ + @Nullable + @Contract("_, _, !null -> !null") + public static IMentionable optMentionable(@NotNull SlashCommandEvent event, @NotNull String option, @Nullable IMentionable defaultValue) { + List options = event.getOptionsByName(option); + + return options.isEmpty() ? defaultValue : options.get(0).getAsMentionable(); + } + + /** + * Guarantees a Role option value by providing a default value. + * + * @param event The slash command event to get options from + * @param option The option we want + * @param defaultValue The fallback option in case of the absence of the option value + * @return The provided option, or the default value if the option is not present + */ + @Nullable + @Contract("_, _, !null -> !null") + public static Role optRole(@NotNull SlashCommandEvent event, @NotNull String option, @Nullable Role defaultValue) { + List options = event.getOptionsByName(option); + + return options.isEmpty() ? defaultValue : options.get(0).getAsRole(); + } + + /** + * Guarantees a User option value by providing a default value. + * + * @param event The slash command event to get options from + * @param option The option we want + * @param defaultValue The fallback option in case of the absence of the option value + * @return The provided option, or the default value if the option is not present + */ + @Nullable + @Contract("_, _, !null -> !null") + public static User optUser(@NotNull SlashCommandEvent event, @NotNull String option, @Nullable User defaultValue) { + List options = event.getOptionsByName(option); + + return options.isEmpty() ? defaultValue : options.get(0).getAsUser(); + } + + /** + * Guarantees a MessageChannel option value by providing a default value. + * + * @param event The slash command event to get options from + * @param option The option we want + * @param defaultValue The fallback option in case of the absence of the option value + * @return The provided option, or the default value if the option is not present + */ + @Nullable + @Contract("_, _, !null -> !null") + public static MessageChannel optMessageChannel(@NotNull SlashCommandEvent event, @NotNull String option, @Nullable MessageChannel defaultValue) { + List options = event.getOptionsByName(option); + + return options.isEmpty() ? defaultValue : options.get(0).getAsMessageChannel(); + } + + /** + * Checks to see if the event has an option. + * + * @param event the slash command event to get options from + * @param option the option we want + * @return true if the option exists, false otherwise + */ + public static boolean hasOption(@NotNull SlashCommandEvent event, @NotNull String option) { + return !event.getOptionsByName(option).isEmpty(); + } +}