some shit to do with the command manager, again

Signed-off-by: deepCurse <leverplays@gmail.com>
This commit is contained in:
lever1209 2021-12-06 23:35:33 -04:00
commit cda31cfaa0
No known key found for this signature in database
GPG key ID: EEBCBB60C9DFC782
9 changed files with 237 additions and 108 deletions

View file

@ -13,10 +13,10 @@ import java.util.regex.Pattern;
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
import pkg.deepCurse.nopalmo.command.CommandInterface.GuildCommandInterface;
import pkg.deepCurse.nopalmo.command.commands.general.Prefix;
import pkg.deepCurse.nopalmo.command.commands.info.Git;
import pkg.deepCurse.nopalmo.command.commands.info.Help;
import pkg.deepCurse.nopalmo.command.commands.info.Ping;
import pkg.deepCurse.nopalmo.command.commands.info.Prefix;
import pkg.deepCurse.nopalmo.core.Boot;
import pkg.deepCurse.nopalmo.database.DatabaseTools;
import pkg.deepCurse.nopalmo.global.Tools;
@ -58,12 +58,31 @@ public class GuildCommandManager extends CommandManager {
return null;
}
public void startCommand(GuildMessageReceivedEvent guildMessage) {
public void startCommand(GuildMessageReceivedEvent guildMessageEvent) { // TODO SPLIT UP AND INHERIT
final String message = guildMessage.getMessage().getContentRaw();
final String message = guildMessageEvent.getMessage().getContentRaw();
String prefix = DatabaseTools.Tools.Guild.Prefix.getPrefix(guildMessageEvent.getGuild().getIdLong());
String pingPrefix = "<@!" + guildMessageEvent.getJDA().getSelfUser().getIdLong() + ">";
final String[] split = message.replaceFirst("(?i)" + Pattern.quote(DatabaseTools.Tools.Global.prefix), "")
.split("\\s+");
// String splicer = null;
// String[] prefixArray = new String[] { DatabaseTools.Tools.Guild.Prefix.getPrefix(guildMessageEvent.getGuild().getIdLong()),
// "<@!" + guildMessageEvent.getJDA().getSelfUser().getIdLong() + ">" }; // FIXME BROKEN PING PREFIX
// for () {
//
// }
String splicer = null;
if (message.startsWith(pingPrefix + " ")) {
splicer = pingPrefix + " ";
} else if (message.startsWith(prefix)) {
splicer = prefix;
} else if (message.startsWith(pingPrefix)) {
splicer = pingPrefix;
} else {
return;
}
final String[] split = message.replaceFirst("(?i)" + Pattern.quote(splicer), "").split("\\s+");
final String command = split[0].toLowerCase();
if (guildCommandMap.containsKey(command)) {
@ -76,55 +95,90 @@ public class GuildCommandManager extends CommandManager {
// ArrayList<String> newArguments = new ArrayList<String>();
// ArrayList<String> commandFlags = new ArrayList<String>();
GuildCommandBlob commandBlob = new GuildCommandBlob(guildMessage);
GuildCommandBlob commandBlob = new GuildCommandBlob(guildMessageEvent);
GuildCommandInterface guildCommand = guildCommandMap.get(command);
HashMap<String, Argument> argumentList = new HashMap<String, Argument>();
boolean printTime = false;
byte argSkipCount = 0;
boolean remainsValid = true;
boolean isWildCard = false;
// int id = 0;
for (String x : args) {
boolean taken = false;
HashMap<Integer, Argument> positionalArgs = new HashMap<Integer, Argument>();
for (Argument i : guildCommand.getArguments().values()) {
if (i.getPosition() >= 0) {
positionalArgs.put(i.getPosition(), i);
}
if (i.isSkipOriginalTaskOnRunnable()) {
remainsValid = false;
}
}
for (int i = 0; i < args.size(); i++) {
String x = args.get(i);
x = x.toLowerCase();
switch (x) {
case "\\time":
printTime = true;
break;
case "\\perm":
// commandFlags.add("user:380045419381784576");
commandBlob.setUserID(380045419381784576L);
break;
case "\\del":
guildMessage.getMessage().delete().queue();
guildMessageEvent.getMessage().delete().queue();
break;
default: // in the rewrite process
default:
if (argSkipCount <= 0) {
if (guildCommand.getArguments() != null) {
if (x.startsWith(Argument.argumentPrefix)) {
String pre = x.substring(Argument.argumentPrefix.length());
if (guildCommand.getArguments().keySet().contains(pre)) {
argumentList.put(pre, guildCommand.getArguments().get(pre));
taken = true;
if (positionalArgs.get(i) == null) {
if (x.startsWith(Argument.argumentPrefix)) {
String pre = x.substring(Argument.argumentPrefix.length());
if (guildCommand.getArguments().keySet().contains(pre)) {
argumentList.put(pre, guildCommand.getArguments().get(pre));
if (guildCommand.getArguments().get(pre).isAutoStartRunnable()
&& guildCommand.getArguments().get(pre)
.getRunnableArg() != null) {
guildCommand.getArguments().get(pre).getRunnableArg()
.run(new CommandBlob(commandBlob));
}
} else {
Tools.wrongUsage(guildMessageEvent.getChannel(), guildCommand);
remainsValid = false;
}
} else {
Tools.wrongUsage(guildMessage.getChannel(), guildCommand);
remainsValid = false;
if (guildCommand.getArguments().get(x) != null) {
if (guildCommand.getArguments().get(x).getPrefixRequirement()) {
Tools.wrongUsage(guildMessageEvent.getChannel(), guildCommand);
remainsValid = false;
} else {
argumentList.put(x, guildCommand.getArguments().get(x));
if (guildCommand.getArguments().get(x).isAutoStartRunnable()
&& guildCommand.getArguments().get(x)
.getRunnableArg() != null) {
guildCommand.getArguments().get(x).getRunnableArg()
.run(new CommandBlob(commandBlob));
}
}
} else {
Tools.wrongUsage(guildMessageEvent.getChannel(), guildCommand);
remainsValid = false;
}
}
} else {
// if (!guildCommand.getArguments().get(x).getIsWildcard()) {
if (guildCommand.getArguments().get(x).getPrefixRequirement()) {
Tools.wrongUsage(guildMessage.getChannel(), guildCommand);
remainsValid = false;
} else {
argumentList.put(x, guildCommand.getArguments().get(x));
taken = true;
if (positionalArgs.get(i).getIsWildcard()) {
argumentList.put(positionalArgs.get(i).getArgName(),
positionalArgs.get(i).setWildCardString(x));
}
if (positionalArgs.get(i).isAutoStartRunnable()
&& positionalArgs.get(i).getRunnableArg() != null) {
positionalArgs.get(i).getRunnableArg().run(new CommandBlob(commandBlob));
}
// } else {
// argumentList.put(x, guildCommand.getArguments().get(x));
// }
}
}
}
@ -138,31 +192,31 @@ public class GuildCommandManager extends CommandManager {
}
if (printTime) {
guildMessage.getChannel()
guildMessageEvent.getChannel()
.sendMessage("Time to run: " + (System.currentTimeMillis() - commandStartTime) + "ms")
.queue();
}
} catch (Exception e) {
if (Boot.isProd) {
guildMessage.getChannel().sendMessage("```properties\n" + e + "```").queue();
guildMessageEvent.getChannel().sendMessage("```properties\n" + e + "```").queue();
} else {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
guildMessage.getChannel().sendMessage("```properties\n" + sw.toString() + "```").queue();
guildMessageEvent.getChannel().sendMessage("```properties\n" + sw.toString() + "```").queue();
System.err.println("Exception caught in: " + e.toString());
e.printStackTrace();
}
} catch (Throwable t) {
if (Boot.isProd) {
guildMessage.getChannel().sendMessage("```mathematica\n" + t + "```").queue();
guildMessageEvent.getChannel().sendMessage("```mathematica\n" + t + "```").queue();
} else {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
t.printStackTrace(pw);
guildMessage.getChannel().sendMessage("```mathematica\n" + sw.toString() + "```").queue();
guildMessageEvent.getChannel().sendMessage("```mathematica\n" + sw.toString() + "```").queue();
System.err.println("Error caught in: " + t.toString());
t.printStackTrace();
}