Chew / JDA-Chewtils

Chew's fork of JDA-Applications/JDA-Utilities, with support for modern features such as Slash Commands, Context Menus, and more.

Home Page:https://chew.pro/JDA-Chewtils

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Optional support for OptionHelper class.

portlek opened this issue · comments

Issue Checklist

  • I have checked for similar issues on the Issue tracker
  • I have updated to the latest version of JDA-Chewtils.
  • I have checked the branches or the maintainer's PRs for upcoming features and updates.

Affected Modules

Command

Description

Add java.util.Optional return type for each opt*** methods. For example;

    /**
     * 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 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
     */
    @NotNull
    public static Optional<String> optString(@NotNull SlashCommandEvent event, @NotNull String option, @Nullable String defaultValue) {
        return Optional.ofNullable(optString(event, option, defaultValue));
    }

Kinda pointless to have imo

it's a utility class so idk. but the purpose of this type of classes is this actually :d

A simple return OptionMapping == null ? defValue : OptionMapping#asWhatever() is sufficient enough.
Creating an Optional just for this is a waste of time and resources.
Also,there can never be more than one OptionMapping with a specific key.

if you think on this class, Optional.ofNullable(event.getOption("optionName")) is also enough for each opt**** methods. my point is this class called helper class which can have helper methods for every option mapping related methods.

To my knowledge, you can't have the same method name + same parameters with a different return type.

As has been stated, I don't really see the point of this. There's a whole debate of whether Optionals are good or not, I'm on the side of don't really care but optX already does everything I assume you'd want (you're guaranteed an option) which was the ultimate point of the thing.