io.github.freya022.botcommands.internal.parameters.resolvers.DoubleResolver Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of BotCommands Show documentation
Show all versions of BotCommands Show documentation
A Kotlin-first (and Java) framework that makes creating Discord bots a piece of cake, using the JDA library.
package io.github.freya022.botcommands.internal.parameters.resolvers;
import io.github.freya022.botcommands.api.commands.text.BaseCommandEvent;
import io.github.freya022.botcommands.api.core.service.annotations.Resolver;
import io.github.freya022.botcommands.api.parameters.ClassParameterResolver;
import io.github.freya022.botcommands.api.parameters.resolvers.ComponentParameterResolver;
import io.github.freya022.botcommands.api.parameters.resolvers.SlashParameterResolver;
import io.github.freya022.botcommands.api.parameters.resolvers.TextParameterResolver;
import io.github.freya022.botcommands.internal.commands.application.slash.SlashCommandInfo;
import io.github.freya022.botcommands.internal.commands.text.TextCommandVariation;
import io.github.freya022.botcommands.internal.components.handler.ComponentDescriptor;
import kotlin.reflect.KParameter;
import net.dv8tion.jda.api.events.interaction.component.GenericComponentInteractionCreateEvent;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.interactions.commands.CommandInteractionPayload;
import net.dv8tion.jda.api.interactions.commands.OptionMapping;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.regex.Pattern;
@Resolver
public class DoubleResolver
extends ClassParameterResolver
implements TextParameterResolver,
SlashParameterResolver,
ComponentParameterResolver {
public DoubleResolver() {
super(Double.class);
}
@Override
@Nullable
public Double resolve(@NotNull TextCommandVariation variation, @NotNull MessageReceivedEvent event, @NotNull String @NotNull [] args) {
try {
return Double.valueOf(args[0]);
} catch (NumberFormatException e) {
return null;
}
}
@Override
@NotNull
public Pattern getPattern() {
return Pattern.compile("([-+]?[0-9]*[.,]?[0-9]+)");
}
@Override
@NotNull
public String getTestExample() {
return "1234.42";
}
@NotNull
@Override
public String getHelpExample(@NotNull KParameter parameter, @NotNull BaseCommandEvent event, boolean isID) {
return "3.14159";
}
@Override
@NotNull
public OptionType getOptionType() {
return OptionType.NUMBER;
}
@Override
@Nullable
public Double resolve(@NotNull SlashCommandInfo info, @NotNull CommandInteractionPayload event, @NotNull OptionMapping optionMapping) {
try {
return optionMapping.getAsDouble();
} catch (NumberFormatException e) { //Can't have discord to send us actual input when autocompleting lmao
return 0d;
}
}
@Override
@Nullable
public Double resolve(@NotNull ComponentDescriptor descriptor, @NotNull GenericComponentInteractionCreateEvent event, @NotNull String arg) {
return Double.valueOf(arg);
}
}