forked from JDA-Applications/JDA-Utilities
-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an OptionHelper class with useful methods for working with options (
#31) * Add an OptionHelper class with useful methods for working with options * final class and private constructor * Add more annotations and update method docs * Add license header and missing annotation * Fix comment for optBoolean
- Loading branch information
Showing
1 changed file
with
202 additions
and
0 deletions.
There are no files selected for viewing
202 changes: 202 additions & 0 deletions
202
command/src/main/java/pw/chew/jdachewtils/command/OptionHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<OptionMapping> 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<OptionMapping> 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<OptionMapping> 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<OptionMapping> 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<OptionMapping> 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<OptionMapping> 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<OptionMapping> 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<OptionMapping> 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<OptionMapping> 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<OptionMapping> 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(); | ||
} | ||
} |