Skip to content

Commit

Permalink
This is as good of a workaround for #162 as I can get
Browse files Browse the repository at this point in the history
  • Loading branch information
Gamebuster19901 committed Mar 10, 2023
1 parent fff8b97 commit bbc92fc
Show file tree
Hide file tree
Showing 25 changed files with 360 additions and 50 deletions.
132 changes: 104 additions & 28 deletions src/main/java/com/gamebuster19901/excite/bot/EventReceiver.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.gamebuster19901.excite.bot;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ExecutionException;

import com.gamebuster19901.excite.Main;
import com.gamebuster19901.excite.bot.command.CommandContext;
import com.gamebuster19901.excite.bot.command.Commands;
import com.gamebuster19901.excite.bot.command.argument.GlobalNode;
import com.gamebuster19901.excite.bot.command.argument.suggestion.MatchingSuggestion;
import com.gamebuster19901.excite.bot.command.interaction.Interactions;
import com.gamebuster19901.excite.bot.user.DiscordUser;
import com.gamebuster19901.excite.util.StacktraceUtil;
Expand All @@ -30,6 +32,8 @@

public class EventReceiver extends ListenerAdapter {

public static final char DATA_ESCAPE = 0x10;

private final Sub sub = new Sub();

@Override
Expand Down Expand Up @@ -100,52 +104,124 @@ public void onGuildReady(GuildReadyEvent e) {
e.getGuild().updateCommands().addCommands(commands).queue();
}


@Override
public void onCommandAutoCompleteInteraction(CommandAutoCompleteInteractionEvent e) {
@SuppressWarnings("unlikely-arg-type")
public void onCommandAutoCompleteInteraction(final CommandAutoCompleteInteractionEvent e) {

String command = e.getName() + " " + e.getFocusedOption().getValue();
final String arguments = e.getFocusedOption().getValue();
String fixedArguments = e.getFocusedOption().getValue();
System.err.println(command + "-----");
final String command = (e.getName() + " " + e.getFocusedOption().getValue());
final List<String> arguments = Commands.getArgs(command);
String lastArg = "";
if(arguments.size() > 0) {
lastArg = arguments.get(arguments.size() - 1);
arguments.remove(arguments.size() - 1);
}

boolean spaceAdded = false;

if(fixedArguments.indexOf(' ') != -1) {
fixedArguments = fixedArguments.substring(0, fixedArguments.lastIndexOf(' '));
}
else {
fixedArguments = "";
}
ParseResults<CommandContext> parseResults = Commands.DISPATCHER.getDispatcher().parse(command, new CommandContext<CommandAutoCompleteInteractionEvent>(e));
List<Suggestion> suggestions;
List<String> returnedSuggestions = new ArrayList<String>();
System.err.println(command + "-----");


CommandContext<CommandAutoCompleteInteractionEvent> context = new CommandContext<>(e);

ParseResults<CommandContext> parseResults = Commands.DISPATCHER.getDispatcher().parse(command, context);
ParseResults<CommandContext> spacedParseResults = Commands.DISPATCHER.getDispatcher().parse(command + " ", context);
List<Suggestion> suggestions = new ArrayList<>();
try {
suggestions = Commands.DISPATCHER.getDispatcher().getCompletionSuggestions(parseResults, command.length()).get().getList();
if(suggestions.size() == 0) {
command = command + " ";
spaceAdded = true;
parseResults = Commands.DISPATCHER.getDispatcher().parse(command, new CommandContext<CommandAutoCompleteInteractionEvent>(e));
suggestions = Commands.DISPATCHER.getDispatcher().getCompletionSuggestions(parseResults, command.length()).get().getList();
List<Suggestion> foundSuggestions = new ArrayList<>();
Suggestion completedSuggestion = null;
for(Suggestion suggestion : Commands.DISPATCHER.getDispatcher().getCompletionSuggestions(parseResults, command.length()).get().getList()) {
if(lastArg.equalsIgnoreCase(suggestion.getText()) || (suggestion instanceof MatchingSuggestion && ((MatchingSuggestion) suggestion).matches(lastArg))) {
completedSuggestion = suggestion;
break;
}
foundSuggestions.add(suggestion);
System.out.println(lastArg + " != " + suggestion.getText());
}
} catch (InterruptedException | ExecutionException ex) {
ex.printStackTrace();
return;
suggestions.addAll(foundSuggestions);
if(completedSuggestion != null) {
System.out.println("EMEPTYASHDFPIOWHAEPHF AWIEUFHOUIWHQEF");
for(Suggestion suggestion : Commands.DISPATCHER.getDispatcher().getCompletionSuggestions(spacedParseResults, command.length() + 1).get().getList()) {
suggestions.add(suggestion);
}
if(suggestions.isEmpty() && !foundSuggestions.isEmpty()) {

}
else {
suggestions.add(completedSuggestion);
}

//suggestions.add(completedSuggestion);
}
} catch (InterruptedException | ExecutionException e1) {
throw new RuntimeException(e1);
}
String completedArgs = getCompletedArgs(arguments);
List<String> returnedSuggestions = new ArrayList<String>();


if(suggestions.size() > 25) {
suggestions = suggestions.subList(0, 25);
}
System.out.println("Arguments:" + arguments);
a:
{
break a;

}

/*
for(int s = 0; s < suggestions.size(); s++) {
Suggestion suggestion = suggestions.get(s);
StringBuilder returnedSuggestionsBuilder = new StringBuilder();
int index = Commands.getMatchingIndex(arguments, suggestion);
if(index > -1) {
for(int i = 0; i < arguments.size() - 1; i++) {
returnedSuggestionsBuilder.append(arguments.get(i));
returnedSuggestionsBuilder.append(' ');
}
returnedSuggestionsBuilder.append(suggestion.getText());
}
else {
for(int i = 0; i < arguments.size(); i++) {
returnedSuggestionsBuilder.append(arguments.get(i));
returnedSuggestionsBuilder.append(' ');
}
returnedSuggestionsBuilder.append(suggestion.getText());
}
returnedSuggestions.add(returnedSuggestionsBuilder.toString());
}
e.replyChoiceStrings(returnedSuggestions).queue();
*/

for(Suggestion suggestion : suggestions) {
if(!spaceAdded) {
returnedSuggestions.add(fixedArguments + " " + suggestion.getText());
if(suggestion instanceof MatchingSuggestion) {
if(((MatchingSuggestion) suggestion).matches(lastArg)) {
System.out.println(suggestion + " matches " + lastArg);
returnedSuggestions.add(completedArgs + " " + suggestion.getText());
}
else {
returnedSuggestions.add(completedArgs);
}
}
else {
returnedSuggestions.add(fixedArguments + arguments + " " + suggestion.getText());
returnedSuggestions.add(completedArgs + " " + suggestion.getText());
}
}

e.replyChoiceStrings(returnedSuggestions).queue();
}

}

private static final String getCompletedArgs(List<String> args) {
StringBuilder ret = new StringBuilder();
Iterator<String> i = args.iterator();
while(i.hasNext()) {
ret = ret.append(i.next());
if(i.hasNext()) {
ret.append(' ');
}
}
return ret.toString();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
public class ArchiveCommand {
@SuppressWarnings("rawtypes")
public static void register(CommandDispatcher<CommandContext> dispatcher) {
dispatcher.register(Commands.literal("archive").then(Commands.argument("channels", StringArgumentType.greedyString()).executes((context) -> {
dispatcher.register(Commands.global("archive").then(Commands.argument("channels", StringArgumentType.greedyString()).executes((context) -> {
return archive(context.getSource(), context.getArgument("channels", String.class));
})));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class BanCommand {

@SuppressWarnings("rawtypes")
public static void register(CommandDispatcher<CommandContext> dispatcher) {
dispatcher.register(Commands.literal("ban")
dispatcher.register(Commands.userGlobal("ban")
.then(Commands.argument("discordUser", new DiscordUserArgumentType())
.executes((context) -> {
return banDiscordUser(context.getSource(), context.getArgument("discordUser", User.class), TimeUtils.FOREVER, parseReason(TimeUtils.FOREVER, null));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class CRCCommand {

public static void register(CommandDispatcher<CommandContext> dispatcher) {
dispatcher.register(
Commands.literal("crc")
Commands.userGlobal("crc")
.then(Commands.literal("test")
.then(Commands.argument("data", StringArgumentType.greedyString())
.executes(context -> {
Expand All @@ -23,8 +23,8 @@ public static void register(CommandDispatcher<CommandContext> dispatcher) {
)
)
.then(Commands.literal("assert")
.then(Commands.argument("expected", StringArgumentType.string())
.then(Commands.argument("data", StringArgumentType.greedyString())
.then(Commands.anyString("expected")
.then(Commands.anyStringGreedy("data")
.executes(context -> {
String expectedString = context.getArgument("expected", String.class);
Integer expected = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class ChangelogCommand {

@SuppressWarnings("rawtypes")
public static void register(CommandDispatcher<CommandContext> dispatcher) {
dispatcher.register(Commands.literal("cl").executes((context) -> {
dispatcher.register(Commands.userGlobal("changelog").executes((context) -> {
return message(context.getSource());
}));
}
Expand Down
67 changes: 65 additions & 2 deletions src/main/java/com/gamebuster19901/excite/bot/command/Commands.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
package com.gamebuster19901.excite.bot.command;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import com.gamebuster19901.excite.Main;
import com.gamebuster19901.excite.bot.audit.CommandAudit;
import com.gamebuster19901.excite.bot.command.argument.GlobalLiteralArgumentBuilder;
import com.gamebuster19901.excite.bot.command.argument.suggestion.AnyStringSuggestionProvider;
import com.gamebuster19901.excite.util.StacktraceUtil;

import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.arguments.ArgumentType;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.suggestion.Suggestion;

@SuppressWarnings("rawtypes")
public class Commands {
Expand Down Expand Up @@ -56,10 +64,27 @@ public void handleCommand(String command) {
}
}

@Deprecated
public static LiteralArgumentBuilder<CommandContext> literal(String name) {
return LiteralArgumentBuilder.literal(name);
}

public static GlobalLiteralArgumentBuilder<CommandContext> global(String name) {
return GlobalLiteralArgumentBuilder.literal(name);
}

public static RequiredArgumentBuilder<CommandContext, String> anyString(String name) {
return argument(name, StringArgumentType.string()).suggests(new AnyStringSuggestionProvider<>(name));
}

public static RequiredArgumentBuilder<CommandContext, String> anyStringGreedy(String name) {
return argument(name, StringArgumentType.greedyString()).suggests(new AnyStringSuggestionProvider<>(name));
}

public static GlobalLiteralArgumentBuilder<CommandContext> userGlobal(String name) {
return GlobalLiteralArgumentBuilder.literal(name, true);
}

public static <T> RequiredArgumentBuilder<CommandContext, T> argument(String name, ArgumentType<T> type) {
return RequiredArgumentBuilder.argument(name, type);
}
Expand Down Expand Up @@ -114,10 +139,48 @@ public static String readQuotedString(StringReader reader) throws CommandSyntaxE
}

public static String lastArgOf(String command) {
if(command.indexOf(' ') > 0) {
return command.substring(command.lastIndexOf(' ') + 1);
List<String> args = getArgs(command);
if(args.size() > 0) {
return args.get(args.size() - 1);
}
return "";
}

public static ArrayList<String> getArgs(String command) {
ArrayList<String> args = new ArrayList<>();
if(command.indexOf(' ') > 0) {
String[] split = command.split(Pattern.quote(" "));
for(int i = 1; i < split.length; i++) {
String arg = split[i];
if(!arg.isBlank()) {
args.add(split[i]);
}
}
}
return args;
}

public static int getMatchingIndex(List<String> arguments, Suggestion suggested) {
return getMatchingIndex(arguments, suggested.getText());
}

public static int getMatchingIndex(List<String> arguments, String suggested) {
if(arguments.size() > 0) {
String arg = arguments.get(arguments.size() - 1);
String suggestion = suggested;
if(!(arg.isBlank() || suggestion.isBlank())) {
if(arg.charAt(0) == suggestion.charAt(0)) {
int i = 1;
for(; i < arg.length() && i < suggestion.length(); i++) {
if(arg.charAt(i) != suggestion.charAt(i)) {
break;
}
}
return i;
}
}
}
return -1;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class Debug {
public static void register(CommandDispatcher<CommandContext> dispatcher) {

dispatcher.register(
Commands.literal("debug")
Commands.userGlobal("debug")
.then(Commands.literal("out")
/*.executes(
(context ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class GameDataCommand {

@SuppressWarnings("rawtypes")
public static void register(CommandDispatcher<CommandContext> dispatcher) {
dispatcher.register(Commands.literal("gameData").executes((context) -> {
dispatcher.register(Commands.userGlobal("game").then(Commands.literal("data")).executes((context) -> {
return getData(context.getSource());
}));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class HelpCommand {

@SuppressWarnings("rawtypes")
public static void register(CommandDispatcher<CommandContext> dispatcher) {
dispatcher.register(Commands.literal("help").executes((context) -> {
dispatcher.register(Commands.userGlobal("help").executes((context) -> {
return sendHelpInfo(context.getSource());
}));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class IconDumpCommand {

@SuppressWarnings("rawtypes")
public static void register(CommandDispatcher<CommandContext> dispatcher) {
dispatcher.register(Commands.literal("icondump").executes((context) -> {
dispatcher.register(Commands.userGlobal("icondump").executes((context) -> {
context.getSource().sendMessage("Provide a server id");
return 0;
}).then(Commands.argument("server", LongArgumentType.longArg()).executes((command) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
public class InsertCommand {

public static void register(CommandDispatcher<CommandContext> dispatcher) {
dispatcher.register(Commands.literal("insert").then(Commands.literal("profile")
dispatcher.register(Commands.userGlobal("insert").then(Commands.literal("profile")
.then(Commands.argument("pid", IntegerArgumentType.integer(1, 999999999))
.then(Commands.argument("fc", StringArgumentType.string())
.then(Commands.argument("name", StringArgumentType.greedyString())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class NotifyCommand {

@SuppressWarnings("rawtypes")
public static void register(CommandDispatcher<CommandContext> dispatcher) {
dispatcher.register(Commands.literal("notify").then(Commands.literal("threshold").then(Commands.argument("amount", IntegerArgumentType.integer()).executes((context) -> {
dispatcher.register(Commands.userGlobal("notify").then(Commands.literal("threshold").then(Commands.argument("amount", IntegerArgumentType.integer()).executes((context) -> {
return setThreshold(context.getSource(), context.getArgument("amount", Integer.class));
})))
.then(Commands.literal("frequency").then(Commands.argument("amount", IntegerArgumentType.integer()).then(Commands.argument("timeUnit", StringArgumentType.word()).executes((context) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
public class OnlineCommand {

public static void register(CommandDispatcher<CommandContext> dispatcher) {
LiteralArgumentBuilder<CommandContext> builder = Commands.literal("online").executes((command) -> {
LiteralArgumentBuilder<CommandContext> builder = Commands.userGlobal("online").executes((command) -> {
return sendResponse(command.getSource(), command);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class PardonCommand {
@SuppressWarnings("rawtypes")
public static void register(CommandDispatcher<CommandContext> dispatcher) {

dispatcher.register(Commands.literal("pardon")
dispatcher.register(Commands.userGlobal("pardon")

.then(Commands.argument("discord", new DiscordUserArgumentType())
.executes((context) -> {
Expand Down
Loading

0 comments on commit bbc92fc

Please sign in to comment.