Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/lowercase parameters #12

Merged
merged 4 commits into from
Jun 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions src/main/java/gg/flyte/neptune/command/CommandDispatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,15 @@ public CommandDispatcher(@NotNull JDA jda, Object mainClass, @NotNull CommandMan

Map<String, Object> toInject = getRequiredInstantiations(mainClass);

for (Class<?> foundClass : findClasses(mainClass.getClass().getPackage().getName())) { // Loop all classes in the package
// Loop all classes in the package
for (Class<?> foundClass : findClasses(mainClass.getClass().getPackage().getName())) {
// If main class, ignore
if (foundClass.getName().equals(mainClass.getClass().getName())) continue;

Object instance = null;
boolean isListener = isListener(foundClass);
boolean isCommand = isCommand(foundClass);
boolean usesInject = usesInject(foundClass);
boolean shouldExclude = shouldExclude(foundClass);

if (isListener) instance = registerListener(foundClass);
Expand All @@ -67,14 +69,20 @@ public CommandDispatcher(@NotNull JDA jda, Object mainClass, @NotNull CommandMan
* ...it should be excluded, in which case don't go ahead and make a new instance
* Otherwise, make new instance as one won't already exist and we want one
*/
if (instance == null && usesInject(foundClass) && shouldExclude) {
if (instance == null && usesInject && shouldExclude) {
LOGGER.debug("Not instantiated " + foundClass.getSimpleName() + " despite its @Inject usage due to its @Exclude usage.");
continue;
} else {
}

// If it's still null but uses @Inject, we should make an instance to control
if (instance == null && usesInject) {
instance = foundClass.getDeclaredConstructor().newInstance();
LOGGER.debug("Instantiated " + foundClass.getSimpleName() + " due to its @Inject usage.");
}

// If still null, we cannot inject into it below
if (instance == null) continue;

for (Field field : foundClass.getDeclaredFields()) {
if (field.isAnnotationPresent(Inject.class)) {
field.setAccessible(true);
Expand Down Expand Up @@ -166,7 +174,7 @@ private void unregisterCommands() {
for (CommandMapping.NamedParameter param : mapping.getParameters())
commandData.addOption(
ArgumentConverter.toOptionType(param.type()),
lowercaseParameterName(param.name()),
param.name(),
param.description() == null ? param.name() : param.description(),
param.required()
);
Expand Down Expand Up @@ -215,10 +223,4 @@ private boolean usesInject(@NotNull Class<?> clazz) {
private boolean shouldExclude(@NotNull Class<?> clazz) {
return clazz.isAnnotationPresent(Exclude.class);
}

private @NotNull String lowercaseParameterName(@NotNull String name) {
if (name.toLowerCase().equals(name)) return name;
LOGGER.debug("Converted " + name + " command option to lowercase due to Discord limitation.");
return name.toLowerCase();
}
}
18 changes: 13 additions & 5 deletions src/main/java/gg/flyte/neptune/command/CommandMapping.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.thoughtworks.paranamer.CachingParanamer;
import gg.flyte.neptune.annotation.Description;
import gg.flyte.neptune.annotation.Optional;
import gg.flyte.neptune.util.Logger;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand All @@ -29,12 +28,12 @@ public CommandMapping(@NotNull Method method, @NotNull Object classInstance) {

for (int i = 1; i < paramNames.length; i++) {
Parameter param = method.getParameters()[i];
parameters[i - 1] = new NamedParameter(paramNames[i], param.getType(), !param.isAnnotationPresent(Optional.class), obtainDescription(paramNames[i], param));
parameters[i - 1] = new NamedParameter(lowercaseParameterName(paramNames[i]), param.getType(), !param.isAnnotationPresent(Optional.class), obtainDescription(paramNames[i], param));
}
}

record NamedParameter(@NotNull String name, @NotNull Class<?> type, boolean required, @Nullable String description) {

record NamedParameter(@NotNull String name, @NotNull Class<?> type, boolean required,
@Nullable String description) {
}

public @NotNull Method getMethod() {
Expand All @@ -52,8 +51,17 @@ record NamedParameter(@NotNull String name, @NotNull Class<?> type, boolean requ
private @Nullable String obtainDescription(@NotNull String parameterName, @NotNull Parameter parameter) {
if (!parameter.isAnnotationPresent(Description.class)) return null;
String description = parameter.getAnnotation(Description.class).value();
if (description.length() > 100) description = description.substring(0, 100);

if (description.length() <= 100) return description;

description = description.substring(0, 100);
LOGGER.debug("We trimmed your description for argument/option \"" + parameterName + "\" as it was more than 100 characters (a Discord limitation).");
return description;
}

private @NotNull String lowercaseParameterName(@NotNull String name) {
if (name.toLowerCase().equals(name)) return name;
LOGGER.debug("Converted " + name + " command option to lowercase due to Discord limitation.");
return name.toLowerCase();
}
}
1 change: 1 addition & 0 deletions src/test/java/org/example/MessageListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public final class MessageListener extends ListenerAdapter {

@Override
public void onMessageReceived(MessageReceivedEvent event) {
if (event.getAuthor().isBot()) return;
testClass.test();
if (muteRegistry.isMuted(event.getAuthor())) event.getMessage().delete().queueAfter(3L, TimeUnit.SECONDS);
}
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/org/example/MuteCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public final class MuteCommand {
description = "Mutes a user!",
permissions = Permission.MODERATE_MEMBERS
)
public void onMute(SlashCommandInteractionEvent e, @Description("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa") User user, @Optional String reason) {
muteRegistry.mute(user, reason == null ? "No reason given." : reason);
public void onMute(SlashCommandInteractionEvent e, @Description("the user you wish to mute") User user, @Optional @Description("asdasdasd") String theReason) {
muteRegistry.mute(user, theReason == null ? "No reason given." : theReason);
e.reply("Successfully muted " + user.getAsTag() + "!").queue();
}
}