Skip to content
This repository was archived by the owner on Feb 22, 2021. It is now read-only.
Open
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
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ repositories.jcenter()

dependencies {
api 'org.apache.logging.log4j:log4j-api:2.13.0'

implementation 'me.xdrop:fuzzywuzzy:1.2.0'

compileOnly 'org.javacord:javacord-api:3.0.5'
compileOnly 'org.javacord:javacord-core:3.0.5'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.comroid.javacord.util.model.command.SelfBotOwnerIgnorable;
import org.comroid.javacord.util.model.command.SelfCommandChannelable;
import org.comroid.javacord.util.model.command.SelfCustomPrefixable;
import org.comroid.javacord.util.model.command.SelfFuzzyComparable;
import org.comroid.javacord.util.model.command.SelfMultiCommandRegisterable;
import org.comroid.javacord.util.model.command.SelfUnknownCommandRespondable;
import org.comroid.javacord.util.ui.embed.DefaultEmbedFactory;
Expand All @@ -36,6 +37,8 @@
import org.comroid.javacord.util.ui.messages.paging.PagedMessage;
import org.comroid.javacord.util.ui.reactions.InfoReaction;

import me.xdrop.fuzzywuzzy.FuzzySearch;
import me.xdrop.fuzzywuzzy.model.ExtractedResult;
import org.apache.logging.log4j.Logger;
import org.javacord.api.DiscordApi;
import org.javacord.api.entity.DiscordEntity;
Expand Down Expand Up @@ -68,6 +71,7 @@ public final class CommandHandler implements
SelfCommandChannelable<CommandHandler>,
SelfCustomPrefixable<CommandHandler>,
SelfBotOwnerIgnorable<CommandHandler>,
SelfFuzzyComparable<CommandHandler>,
SelfUnknownCommandRespondable<CommandHandler> {
private static final Logger logger = LoggerUtil.getLogger(CommandHandler.class);
static final String NO_GROUP = "@NoGroup#";
Expand All @@ -85,6 +89,7 @@ public final class CommandHandler implements
private long[] serverBlacklist;
private boolean ignoreBotOwnerPermissions;
private boolean respondToUnknownCommand;
private @Nullable Function<Long, Integer> fuzzyMatchingThresholdFunction;

public CommandHandler(DiscordApi api) {
this(api, false);
Expand Down Expand Up @@ -328,6 +333,18 @@ public boolean doesRespondToUnknownCommands() {
return respondToUnknownCommand;
}

@Override
public CommandHandler withFuzzyMatchingThreshold(@Nullable Function<Long, Integer> fuzzyMatchingThresholdFunction) {
this.fuzzyMatchingThresholdFunction = fuzzyMatchingThresholdFunction;

return this;
}

@Override
public Optional<? extends Function<Long, Integer>> getFuzzyMatchingThreshold() {
return Optional.ofNullable(fuzzyMatchingThresholdFunction);
}

private void extractCommandRep(@Nullable Object invocationTarget, Method... methods) {
for (Method method : methods) {
Command cmd = method.getAnnotation(Command.class);
Expand Down Expand Up @@ -462,27 +479,21 @@ private void handleCommand(final Message message, final TextChannel channel, fin
CommandRepresentation cmd;
String[] split = splitContent(content);
final String[] commandName = new String[1];

String[] args;
Optional<Server> serverOptional = commandParams.getServer();
if (usedPrefix.matches("^(.*\\s.*)+$")) {
cmd = commands.entrySet()
.stream()
.filter(entry -> entry.getKey()
.toLowerCase()
.equals((commandName[0] = split[1]).substring(usedPrefix.length()).toLowerCase()))
.findFirst()
.map(Map.Entry::getValue)
.orElse(null);
final String cmdQuery = (commandName[0] = split[1]).substring(usedPrefix.length()).toLowerCase();

cmd = findCommand(serverOptional.orElse(null), cmdQuery);

args = new String[split.length - 2];
arraycopy(split, 2, args, 0, args.length);
} else {
cmd = commands.entrySet()
.stream()
.filter(entry -> entry.getKey()
.toLowerCase()
.equals((commandName[0] = split[0]).substring(usedPrefix.length()).toLowerCase()))
.findFirst()
.map(Map.Entry::getValue)
.orElse(null);
final String cmdQuery = (commandName[0] = split[0]).substring(usedPrefix.length()).toLowerCase();

cmd = findCommand(serverOptional.orElse(null), cmdQuery);

args = new String[split.length - 1];
arraycopy(split, 1, args, 0, args.length);
}
Expand Down Expand Up @@ -557,6 +568,32 @@ else if (!message.isPrivateMessage() && !cmd.enableServerChat)
else doInvoke(cmd, commandParams, channel, message);
}

private @Nullable CommandRepresentation findCommand(@Nullable Server server, String cmdQuery) {
CommandRepresentation yield = null;

if (fuzzyMatchingThresholdFunction != null && server != null) {
final List<ExtractedResult> extractedResults = FuzzySearch.extractTop(
cmdQuery,
commands.keySet(),
fuzzyMatchingThresholdFunction.apply(server.getId())
);

if (extractedResults.size() == 1)
yield = commands.get(extractedResults.get(0).getString());
} else {
yield = commands.entrySet()
.stream()
.filter(entry -> entry.getKey()
.toLowerCase()
.equals(cmdQuery))
.findFirst()
.map(Map.Entry::getValue)
.orElse(null);
}

return yield;
}

private @Nullable String extractUsedPrefix(final Message message) {
String content = message.getContent();
int usedPrefix = -1;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.comroid.javacord.util.model.command;

import java.util.Optional;
import java.util.function.Function;

import org.jetbrains.annotations.Nullable;

public interface SelfFuzzyComparable<Self extends SelfFuzzyComparable<Self>> {
Self withFuzzyMatchingThreshold(@Nullable Function<Long, Integer> fuzzyMatchingThresholdFunction);

Optional<? extends Function<Long, Integer>> getFuzzyMatchingThreshold();

default Self removeFuzzyMatchingThreshold() {
return withFuzzyMatchingThreshold(null);
}
}