From 3cd8f3404c1ab778522f3045dd9c336eb84fb1cd Mon Sep 17 00:00:00 2001 From: deepCurse Date: Wed, 8 Dec 2021 11:17:27 -0400 Subject: [PATCH] the storm Signed-off-by: deepCurse --- .../nopalmo/command/commands/info/Help.java | 15 +- src/pkg/deepCurse/nopalmo/core/Boot.java | 14 +- .../DirectMessageReceivedListener.java | 8 +- .../GuildMessageReceivedListener.java | 2 +- .../nopalmo/manager/CommandManager.java | 537 ++++++++++++++++- .../nopalmo/manager/DirectCommandBlob.java | 16 +- .../nopalmo/manager/DirectCommandManager.java | 538 +++++++++--------- .../nopalmo/manager/GuildCommandBlob.java | 6 +- .../nopalmo/manager/GuildCommandManager.java | 529 ++++++++--------- 9 files changed, 1078 insertions(+), 587 deletions(-) diff --git a/src/pkg/deepCurse/nopalmo/command/commands/info/Help.java b/src/pkg/deepCurse/nopalmo/command/commands/info/Help.java index 6d7759c..077f643 100644 --- a/src/pkg/deepCurse/nopalmo/command/commands/info/Help.java +++ b/src/pkg/deepCurse/nopalmo/command/commands/info/Help.java @@ -6,17 +6,18 @@ import java.util.HashMap; import java.util.List; import net.dv8tion.jda.api.EmbedBuilder; +import pkg.deepCurse.nopalmo.command.CommandInterface; import pkg.deepCurse.nopalmo.command.CommandInterface.GuildCommandInterface; import pkg.deepCurse.nopalmo.database.DatabaseTools.Tools.Global; import pkg.deepCurse.nopalmo.manager.Argument; +import pkg.deepCurse.nopalmo.manager.CommandManager; import pkg.deepCurse.nopalmo.manager.GuildCommandBlob; -import pkg.deepCurse.nopalmo.manager.GuildCommandManager; public class Help implements GuildCommandInterface { - public final GuildCommandManager manager; + public final CommandManager manager; - public Help(GuildCommandManager m) { + public Help(CommandManager m) { this.manager = m; } @@ -69,17 +70,17 @@ public class Help implements GuildCommandInterface { StringBuilder sB = new StringBuilder(); - GuildCommandInterface ping = blob.getCommandManager().getCommand("ping"); + CommandInterface ping = blob.getCommandManager().getDirectCommand("ping"); if (ping != null) { sB.append("`" + ping.getUsage(isDevEnabled) + "`\n"); } - GuildCommandInterface info = blob.getCommandManager().getCommand("info"); + CommandInterface info = blob.getCommandManager().getDirectCommand("info"); if (info != null) { sB.append("`" + info.getUsage(isDevEnabled) + "`\n"); } - GuildCommandInterface prefix = blob.getCommandManager().getCommand("prefix"); + CommandInterface prefix = blob.getCommandManager().getDirectCommand("prefix"); if (prefix != null) { sB.append("`" + prefix.getUsage(isDevEnabled) + "`\n"); } @@ -96,7 +97,7 @@ public class Help implements GuildCommandInterface { return; } else if (argumentMap.get("commandName") != null) { - GuildCommandInterface command = manager.getCommand(argumentMap.get("commandName").getWildCardString()); + GuildCommandInterface command = manager.getGuildCommand(argumentMap.get("commandName").getWildCardString()); if (command != null && ((deniedPages.contains(command.getHelpPage()) && isDevEnabled) || !deniedPages.contains(command.getHelpPage()))) { diff --git a/src/pkg/deepCurse/nopalmo/core/Boot.java b/src/pkg/deepCurse/nopalmo/core/Boot.java index 7d1128b..5330c53 100644 --- a/src/pkg/deepCurse/nopalmo/core/Boot.java +++ b/src/pkg/deepCurse/nopalmo/core/Boot.java @@ -16,6 +16,7 @@ import pkg.deepCurse.nopalmo.database.DatabaseTools.Tools.Global; import pkg.deepCurse.nopalmo.global.Reactions; import pkg.deepCurse.nopalmo.listener.DirectMessageReceivedListener; import pkg.deepCurse.nopalmo.listener.GuildMessageReceivedListener; +import pkg.deepCurse.nopalmo.manager.CommandManager; import pkg.deepCurse.nopalmo.manager.DirectCommandManager; import pkg.deepCurse.nopalmo.manager.GuildCommandManager; import pkg.deepCurse.nopalmo.manager.StatusManager; @@ -26,14 +27,15 @@ public class Boot { public static JDA bot; // TODO create sharding handler public static DatabaseTools databaseTools = null; - public static final GuildCommandManager guildCommandManager = new GuildCommandManager(); // move to master manager - public static final DirectCommandManager directCommandManager = new DirectCommandManager(); // move to master - // manager +// public static final GuildCommandManager guildCommandManager = new GuildCommandManager(); // move to master manager +// public static final DirectCommandManager directCommandManager = new DirectCommandManager(); // move to master +// // manager public static boolean isProd = false; public static final long pid = ProcessHandle.current().pid(); public static boolean running = true; + public static final CommandManager commandManager = new CommandManager(); public static void main(String[] args) { LogHelper.boot("Booting: <" + pid + ">"); @@ -78,9 +80,9 @@ public class Boot { LogHelper.boot("Init reaction/emote list"); Reactions.init(); LogHelper.boot("Initialized reaction/emote list. . ."); - LogHelper.boot("Init guild commands list"); - guildCommandManager.init(); - LogHelper.boot("Initialized guild commands list. . ."); + LogHelper.boot("Init commands list"); + commandManager.init(); + LogHelper.boot("Initialized commands list. . ."); try { // bot = JDABuilder.createDefault(args[0]).setChunkingFilter(ChunkingFilter.ALL) diff --git a/src/pkg/deepCurse/nopalmo/listener/DirectMessageReceivedListener.java b/src/pkg/deepCurse/nopalmo/listener/DirectMessageReceivedListener.java index 698b67b..dd9a6e9 100644 --- a/src/pkg/deepCurse/nopalmo/listener/DirectMessageReceivedListener.java +++ b/src/pkg/deepCurse/nopalmo/listener/DirectMessageReceivedListener.java @@ -4,7 +4,7 @@ import javax.annotation.Nonnull; import net.dv8tion.jda.api.entities.Message; import net.dv8tion.jda.api.events.ReadyEvent; -import net.dv8tion.jda.api.events.message.MessageReceivedEvent; +import net.dv8tion.jda.api.events.message.priv.PrivateMessageReceivedEvent; import net.dv8tion.jda.api.hooks.ListenerAdapter; import pkg.deepCurse.nopalmo.core.Boot; import pkg.deepCurse.nopalmo.database.DatabaseTools; @@ -18,7 +18,7 @@ public class DirectMessageReceivedListener extends ListenerAdapter { } @Override - public void onMessageReceived(@Nonnull MessageReceivedEvent event) { + public void onPrivateMessageReceived(@Nonnull PrivateMessageReceivedEvent event) { Message message = event.getMessage(); String messageRaw = message.getContentRaw(); @@ -48,8 +48,8 @@ public class DirectMessageReceivedListener extends ListenerAdapter { return; } - if (!event.getAuthor().isBot() && !event.isFromGuild()) { - Boot.directCommandManager.startCommand(event); + if (!event.getAuthor().isBot()) { + Boot.commandManager.startCommand(event); } } diff --git a/src/pkg/deepCurse/nopalmo/listener/GuildMessageReceivedListener.java b/src/pkg/deepCurse/nopalmo/listener/GuildMessageReceivedListener.java index effa6be..e17a5d5 100644 --- a/src/pkg/deepCurse/nopalmo/listener/GuildMessageReceivedListener.java +++ b/src/pkg/deepCurse/nopalmo/listener/GuildMessageReceivedListener.java @@ -37,7 +37,7 @@ public class GuildMessageReceivedListener extends ListenerAdapter { } if (!event.getAuthor().isBot()) { - Boot.guildCommandManager.startCommand(event); + Boot.commandManager.startCommand(event); } } } diff --git a/src/pkg/deepCurse/nopalmo/manager/CommandManager.java b/src/pkg/deepCurse/nopalmo/manager/CommandManager.java index 40c9f27..619520b 100644 --- a/src/pkg/deepCurse/nopalmo/manager/CommandManager.java +++ b/src/pkg/deepCurse/nopalmo/manager/CommandManager.java @@ -1,5 +1,540 @@ package pkg.deepCurse.nopalmo.manager; -public abstract class CommandManager { +import java.io.PrintWriter; +import java.io.StringWriter; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.Executor; +import java.util.concurrent.Executors; +import java.util.regex.Pattern; +import net.dv8tion.jda.api.events.Event; +import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent; +import net.dv8tion.jda.api.events.message.priv.PrivateMessageReceivedEvent; +import pkg.deepCurse.nopalmo.command.CommandInterface; +import pkg.deepCurse.nopalmo.command.CommandInterface.DirectCommandInterface; +import pkg.deepCurse.nopalmo.command.CommandInterface.GuildCommandInterface; +import pkg.deepCurse.nopalmo.command.commands.general.Prefix; +import pkg.deepCurse.nopalmo.command.commands.general.Test; +import pkg.deepCurse.nopalmo.command.commands.info.Git; +import pkg.deepCurse.nopalmo.command.commands.info.Help; +import pkg.deepCurse.nopalmo.command.commands.info.Info; +import pkg.deepCurse.nopalmo.command.commands.info.Ping; +import pkg.deepCurse.nopalmo.core.Boot; +import pkg.deepCurse.nopalmo.database.DatabaseTools; +import pkg.deepCurse.nopalmo.database.DatabaseTools.Tools.Global; +import pkg.deepCurse.nopalmo.database.DatabaseTools.Tools.Users; +import pkg.deepCurse.nopalmo.global.Tools; + +public class CommandManager { + + private final Map guildCommandMap = new HashMap<>(); + private final Map directCommandMap = new HashMap<>(); + private static Executor executor = null; + + public CommandManager() { + init(); + executor = Executors.newWorkStealingPool(); + } + + public void init() { + addCommand(new Help(this)); + addCommand(new Ping()); + addCommand(new Git()); + addCommand(new Prefix()); + addCommand(new Test()); + addCommand(new Info()); + } + + private void addCommand(CommandInterface c) { + if (c instanceof DirectCommandInterface) { + addDirectCommand((DirectCommandInterface) c); + } + if (c instanceof GuildCommandInterface) { + addGuildCommand((GuildCommandInterface) c); + } + } + + private void addDirectCommand(DirectCommandInterface c) { + if (!directCommandMap.containsKey(c.getCommandName())) { + directCommandMap.put(c.getCommandName(), c); + } else { + directCommandMap.remove(c.getCommandName()); + directCommandMap.put(c.getCommandName(), c); + } + } + + public Collection getDirectCommands() { + return directCommandMap.values(); + } + + public DirectCommandInterface getDirectCommand(String commandName) { + if (commandName != null) { + return directCommandMap.get(commandName); + } + return null; + } + + private void addGuildCommand(GuildCommandInterface c) { + if (!guildCommandMap.containsKey(c.getCommandName())) { + guildCommandMap.put(c.getCommandName(), c); + } else { + guildCommandMap.remove(c.getCommandName()); + guildCommandMap.put(c.getCommandName(), c); + } + } + + public Collection getGuildCommands() { + return guildCommandMap.values(); + } + + public GuildCommandInterface getGuildCommand(String commandName) { + if (commandName != null) { + return guildCommandMap.get(commandName); + } + return null; + } + + public void startCommand(Event event) { + if (event instanceof GuildMessageReceivedEvent) { + startGuildCommand((GuildMessageReceivedEvent) event); + } else if (event instanceof PrivateMessageReceivedEvent) { + startDirectCommand((PrivateMessageReceivedEvent)event); + } else throw new IllegalArgumentException("Invalid type"); + } + + public void startDirectCommand(PrivateMessageReceivedEvent event){ + + final String message = event.getMessage().getContentRaw(); + String prefix = Global.prefix; + String pingPrefix = "<@!" + event.getJDA().getSelfUser().getIdLong() + ">"; + + 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 commandCall = split[0].toLowerCase(); + + if (guildCommandMap.containsKey(commandCall)) { + final List args = Arrays.asList(split).subList(1, split.length); + + executor.execute(() -> { + long commandStartTime = System.currentTimeMillis(); + + try { + // ArrayList newArguments = new ArrayList(); + // ArrayList commandFlags = new ArrayList(); + + DirectCommandBlob commandBlob = new DirectCommandBlob(event); + DirectCommandInterface command = directCommandMap.get(commandCall); + HashMap argumentList = new HashMap(); + + boolean printTime = false; + byte argSkipCount = 0; + boolean remainsValid = true; + + HashMap positionalArgs = new HashMap(); + + if (command.getArguments() != null) { + for (Argument i : command.getArguments().values()) { + if (i.getPosition() >= 0) { + positionalArgs.put(i.getPosition(), i); + } + } + } + + List newArgs = new ArrayList(); + + int offset = 0; + 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": + commandBlob.setUserID(380045419381784576L); + break; + case "\\del": + event.getMessage().delete().queue(); + break; + default: + newArgs.add(x); + break; + } + } + // split up so global commands are actually global, and will not be affected by + // neighboring local args + for (int i = 0; i < newArgs.size(); i++) { + String x = newArgs.get(i); + x = x.toLowerCase(); + if (argSkipCount <= 0) { + if (command.getArguments() != null) { + + if (x.startsWith(Argument.argumentPrefix)) { + + String pre = x.substring(Argument.argumentPrefix.length()); + if (command.getArguments().keySet().contains(pre)) { + offset++; + if (command.getArguments().get(pre).getPermission() == null + || DatabaseTools.Tools.Developers.hasPermission(commandBlob.getUserID(), + command.getArguments().get(pre).getPermission())) { + if (command.getArguments().get(pre).isSkipOriginalTaskOnRunnable()) { + remainsValid = false; + } + argumentList.put(pre, command.getArguments().get(pre)); + if (command.getArguments().get(pre).isAutoStartRunnable() + && command.getArguments().get(pre).getRunnableArg() != null) { + command.getArguments().get(pre).getRunnableArg() + .run(new CommandBlob(commandBlob)); + } + } else { + Tools.invalidPermissions(event.getChannel(), command); + remainsValid = false; + } + + } else { + Tools.wrongUsage(event.getChannel(), command); + remainsValid = false; + } + } else { + if (command.getArguments().get(x) != null) { + if (command.getArguments().get(x).getPermission() == null + || DatabaseTools.Tools.Developers.hasPermission(commandBlob.getUserID(), + command.getArguments().get(x).getPermission())) { + if (command.getArguments().get(x).isSkipOriginalTaskOnRunnable()) { + remainsValid = false; + } + argumentList.put(x, command.getArguments().get(x)); + offset++; + if (command.getArguments().get(x).isAutoStartRunnable() + && command.getArguments().get(x).getRunnableArg() != null) { + command.getArguments().get(x).getRunnableArg() + .run(new CommandBlob(commandBlob)); + } + } else { + Tools.invalidPermissions(event.getChannel(), command); + remainsValid = false; + } + } else { + if (positionalArgs.get(i - offset) != null) { + if (positionalArgs.get(i - offset).getPermission() == null + || DatabaseTools.Tools.Developers.hasPermission( + commandBlob.getUserID(), + positionalArgs.get(i - offset).getPermission())) { + if (positionalArgs.get(i - offset).isSkipOriginalTaskOnRunnable()) { + remainsValid = false; + } + if (positionalArgs.get(i - offset).getIsWildcard()) { + argumentList.put(positionalArgs.get(i - offset).getArgName(), + positionalArgs.get(i - offset).setWildCardString(x)); + } else { + Tools.wrongUsage(event.getChannel(), command); + remainsValid = false; + } + if (positionalArgs.get(i - offset).isAutoStartRunnable() + && positionalArgs.get(i - offset).getRunnableArg() != null) { + positionalArgs.get(i - offset).getRunnableArg() + .run(new CommandBlob(commandBlob)); + } + } else { + Tools.invalidPermissions(event.getChannel(), command); + remainsValid = false; + } + } else + event.getChannel().sendMessage("pos is null").queue(); + } + } + + } else { + Tools.wrongUsage(event.getChannel(), command); + remainsValid = false; + } + } + + } + + if (command.isNSFW() && !commandBlob.getChannel().isNSFW()) { + commandBlob.getChannel().sendMessage( + "Sorry, but you cannot run this command here, maybe try somewhere more private?") + .queue(); + remainsValid = false; + } + + if (command.getPremiumLevel() > Users.getPremiumLevel(commandBlob.getUserID())) { + commandBlob.getChannel().sendMessage( + "Sorry, but you cannot run this command, it is premium subs only, of at least tier " + + command.getPremiumLevel()) + .queue(); + remainsValid = false; + } + + commandBlob.setCommandManager(this); + + if (remainsValid) { + command.runDirectCommand(commandBlob, argumentList); + } + + if (printTime) { + event.getChannel() + .sendMessage("Time to run: " + (System.currentTimeMillis() - commandStartTime) + "ms") + .queue(); + } + + } catch (Exception e) { + if (Boot.isProd) { + event.getChannel().sendMessage("```properties\n" + e + "```").queue(); + } else { + StringWriter sw = new StringWriter(); + PrintWriter pw = new PrintWriter(sw); + e.printStackTrace(pw); + event.getChannel().sendMessage("```properties\n" + sw.toString() + "```").queue(); + System.err.println("Exception caught in: " + e.toString()); + e.printStackTrace(); + } + } catch (Throwable t) { + + if (Boot.isProd) { + event.getChannel().sendMessage("```mathematica\n" + t + "```").queue(); + } else { + StringWriter sw = new StringWriter(); + PrintWriter pw = new PrintWriter(sw); + t.printStackTrace(pw); + event.getChannel().sendMessage("```mathematica\n" + sw.toString() + "```").queue(); + System.err.println("Error caught in: " + t.toString()); + t.printStackTrace(); + } + } + }); + } + } + + public void startGuildCommand(GuildMessageReceivedEvent event) { + + final String message = event.getMessage().getContentRaw(); + String prefix = DatabaseTools.Tools.Guild.Prefix.getPrefix(event.getGuild().getIdLong()); + String pingPrefix = "<@!" + event.getJDA().getSelfUser().getIdLong() + ">"; + + 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)) { + final List args = Arrays.asList(split).subList(1, split.length); + + executor.execute(() -> { + long commandStartTime = System.currentTimeMillis(); + + try { + // ArrayList newArguments = new ArrayList(); + // ArrayList commandFlags = new ArrayList(); + + GuildCommandBlob commandBlob = new GuildCommandBlob(event); + GuildCommandInterface guildCommand = guildCommandMap.get(command); + HashMap argumentList = new HashMap(); + + boolean printTime = false; + byte argSkipCount = 0; + boolean remainsValid = true; + + HashMap positionalArgs = new HashMap(); + + if (guildCommand.getArguments() != null) { + for (Argument i : guildCommand.getArguments().values()) { + if (i.getPosition() >= 0) { + positionalArgs.put(i.getPosition(), i); + } + } + } + + List newArgs = new ArrayList(); + + int offset = 0; + 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": + commandBlob.setUserID(380045419381784576L); + break; + case "\\del": + event.getMessage().delete().queue(); + break; + default: + newArgs.add(x); + break; + } + } + // split up so global commands are actually global, and will not be affected by + // neighboring local args + for (int i = 0; i < newArgs.size(); i++) { + String x = newArgs.get(i); + x = x.toLowerCase(); + 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)) { + offset++; + if (guildCommand.getArguments().get(pre).getPermission() == null + || DatabaseTools.Tools.Developers.hasPermission(commandBlob.getUserID(), + guildCommand.getArguments().get(pre).getPermission())) { + if (guildCommand.getArguments().get(pre).isSkipOriginalTaskOnRunnable()) { + remainsValid = false; + } + 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.invalidPermissions(event.getChannel(), guildCommand); + remainsValid = false; + } + + } else { + Tools.wrongUsage(event.getChannel(), guildCommand); + remainsValid = false; + } + } else { + if (guildCommand.getArguments().get(x) != null) { + if (guildCommand.getArguments().get(x).getPermission() == null + || DatabaseTools.Tools.Developers.hasPermission(commandBlob.getUserID(), + guildCommand.getArguments().get(x).getPermission())) { + if (guildCommand.getArguments().get(x).isSkipOriginalTaskOnRunnable()) { + remainsValid = false; + } + argumentList.put(x, guildCommand.getArguments().get(x)); + offset++; + if (guildCommand.getArguments().get(x).isAutoStartRunnable() + && guildCommand.getArguments().get(x).getRunnableArg() != null) { + guildCommand.getArguments().get(x).getRunnableArg() + .run(new CommandBlob(commandBlob)); + } + } else { + Tools.invalidPermissions(event.getChannel(), guildCommand); + remainsValid = false; + } + } else { + if (positionalArgs.get(i - offset) != null) { + if (positionalArgs.get(i - offset).getPermission() == null + || DatabaseTools.Tools.Developers.hasPermission( + commandBlob.getUserID(), + positionalArgs.get(i - offset).getPermission())) { + if (positionalArgs.get(i - offset).isSkipOriginalTaskOnRunnable()) { + remainsValid = false; + } + if (positionalArgs.get(i - offset).getIsWildcard()) { + argumentList.put(positionalArgs.get(i - offset).getArgName(), + positionalArgs.get(i - offset).setWildCardString(x)); + } else { + Tools.wrongUsage(event.getChannel(), guildCommand); + remainsValid = false; + } + if (positionalArgs.get(i - offset).isAutoStartRunnable() + && positionalArgs.get(i - offset).getRunnableArg() != null) { + positionalArgs.get(i - offset).getRunnableArg() + .run(new CommandBlob(commandBlob)); + } + } else { + Tools.invalidPermissions(event.getChannel(), guildCommand); + remainsValid = false; + } + } else + event.getChannel().sendMessage("pos is null").queue(); + } + } + + } else { + Tools.wrongUsage(event.getChannel(), guildCommand); + remainsValid = false; + } + } + + } + + if (guildCommand.isNSFW() && !commandBlob.getChannel().isNSFW()) { + commandBlob.getChannel().sendMessage( + "Sorry, but you cannot run this command here, maybe try somewhere more private?") + .queue(); + remainsValid = false; + } + + if (guildCommand.getPremiumLevel() > Users.getPremiumLevel(commandBlob.getUserID())) { + commandBlob.getChannel().sendMessage( + "Sorry, but you cannot run this command, it is premium subs only, of at least tier " + + guildCommand.getPremiumLevel()) + .queue(); + remainsValid = false; + } + + commandBlob.setCommandManager(this); + + if (remainsValid) { + guildCommand.runGuildCommand(commandBlob, argumentList); + } + + if (printTime) { + event.getChannel() + .sendMessage("Time to run: " + (System.currentTimeMillis() - commandStartTime) + "ms") + .queue(); + } + + } catch (Exception e) { + if (Boot.isProd) { + event.getChannel().sendMessage("```properties\n" + e + "```").queue(); + } else { + StringWriter sw = new StringWriter(); + PrintWriter pw = new PrintWriter(sw); + e.printStackTrace(pw); + event.getChannel().sendMessage("```properties\n" + sw.toString() + "```").queue(); + System.err.println("Exception caught in: " + e.toString()); + e.printStackTrace(); + } + } catch (Throwable t) { + + if (Boot.isProd) { + event.getChannel().sendMessage("```mathematica\n" + t + "```").queue(); + } else { + StringWriter sw = new StringWriter(); + PrintWriter pw = new PrintWriter(sw); + t.printStackTrace(pw); + event.getChannel().sendMessage("```mathematica\n" + sw.toString() + "```").queue(); + System.err.println("Error caught in: " + t.toString()); + t.printStackTrace(); + } + } + }); + } + } + } diff --git a/src/pkg/deepCurse/nopalmo/manager/DirectCommandBlob.java b/src/pkg/deepCurse/nopalmo/manager/DirectCommandBlob.java index 1f5ebc5..968ee01 100644 --- a/src/pkg/deepCurse/nopalmo/manager/DirectCommandBlob.java +++ b/src/pkg/deepCurse/nopalmo/manager/DirectCommandBlob.java @@ -4,19 +4,19 @@ import java.util.ArrayList; import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.entities.TextChannel; -import net.dv8tion.jda.api.events.message.MessageReceivedEvent; +import net.dv8tion.jda.api.events.message.priv.PrivateMessageReceivedEvent; public class DirectCommandBlob { - private DirectCommandManager commandManager = null; + private CommandManager commandManager = null; private ArrayList args = null; - private MessageReceivedEvent event = null; + private PrivateMessageReceivedEvent event = null; private JDA bot = null; private long userID = 0; private long channelID = 0; - public DirectCommandBlob(MessageReceivedEvent event) { + public DirectCommandBlob(PrivateMessageReceivedEvent event) { setUserID(event.getAuthor().getIdLong()); setChannelID(event.getChannel().getIdLong()); this.event = event; @@ -49,11 +49,11 @@ public class DirectCommandBlob { return this.userID; } - public DirectCommandManager getCommandManager() { + public CommandManager getCommandManager() { return commandManager; } - public void setCommandManager(DirectCommandManager commandManager) { + public void setCommandManager(CommandManager commandManager) { this.commandManager = commandManager; } @@ -65,11 +65,11 @@ public class DirectCommandBlob { this.channelID = channelID; } - public MessageReceivedEvent getEvent() { + public PrivateMessageReceivedEvent getEvent() { return event; } - public void setEvent(MessageReceivedEvent event) { + public void setEvent(PrivateMessageReceivedEvent event) { this.event = event; } } diff --git a/src/pkg/deepCurse/nopalmo/manager/DirectCommandManager.java b/src/pkg/deepCurse/nopalmo/manager/DirectCommandManager.java index bbdecc5..15a3e39 100644 --- a/src/pkg/deepCurse/nopalmo/manager/DirectCommandManager.java +++ b/src/pkg/deepCurse/nopalmo/manager/DirectCommandManager.java @@ -1,283 +1,261 @@ package pkg.deepCurse.nopalmo.manager; -import java.io.PrintWriter; -import java.io.StringWriter; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.concurrent.Executor; -import java.util.concurrent.Executors; -import java.util.regex.Pattern; - -import net.dv8tion.jda.api.events.message.MessageReceivedEvent; -import pkg.deepCurse.nopalmo.command.CommandInterface; -import pkg.deepCurse.nopalmo.command.CommandInterface.DirectCommandInterface; -import pkg.deepCurse.nopalmo.command.CommandInterface.DualCommandInterface; -import pkg.deepCurse.nopalmo.core.Boot; -import pkg.deepCurse.nopalmo.database.DatabaseTools; -import pkg.deepCurse.nopalmo.database.DatabaseTools.Tools.Global; -import pkg.deepCurse.nopalmo.database.DatabaseTools.Tools.Users; -import pkg.deepCurse.nopalmo.global.Tools; - -public class DirectCommandManager extends CommandManager { - - private final Map directCommandMap = new HashMap<>(); - private static Executor executor = null; - - public DirectCommandManager() { - init(); - executor = Executors.newCachedThreadPool(); - } - - public void init() { - - for (CommandInterface i : Boot.guildCommandManager.getGuildCommands()) { - - if (i instanceof DualCommandInterface) { - addCommand((DualCommandInterface) i); - } else if (i instanceof DirectCommandInterface) { - addCommand((DirectCommandInterface) i); - } - - } - - } - - private void addCommand(DirectCommandInterface c) { - if (!directCommandMap.containsKey(c.getCommandName())) { - directCommandMap.put(c.getCommandName(), c); - } else { - directCommandMap.remove(c.getCommandName()); - directCommandMap.put(c.getCommandName(), c); - } - } - - public Collection getDirectCommands() { - return directCommandMap.values(); - } - - public DirectCommandInterface getCommand(String commandName) { - if (commandName != null) { - return directCommandMap.get(commandName); - } - return null; - } - - public void startCommand(MessageReceivedEvent directMessageEvent) { - - final String message = directMessageEvent.getMessage().getContentRaw(); - String prefix = Global.prefix; - String pingPrefix = "<@!" + directMessageEvent.getJDA().getSelfUser().getIdLong() + ">"; - - 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 (directCommandMap.containsKey(command)) { - final List args = Arrays.asList(split).subList(1, split.length); - - executor.execute(() -> { - long commandStartTime = System.currentTimeMillis(); - - try { - // ArrayList newArguments = new ArrayList(); - // ArrayList commandFlags = new ArrayList(); - - DirectCommandBlob commandBlob = new DirectCommandBlob(directMessageEvent); - DirectCommandInterface guildCommand = directCommandMap.get(command); - HashMap argumentList = new HashMap(); - - boolean printTime = false; - byte argSkipCount = 0; - boolean remainsValid = true; - - HashMap positionalArgs = new HashMap(); - - if (guildCommand.getArguments() != null) { - for (Argument i : guildCommand.getArguments().values()) { - if (i.getPosition() >= 0) { - positionalArgs.put(i.getPosition(), i); - } - } - } - - List newArgs = new ArrayList(); - - int offset = 0; - 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": - commandBlob.setUserID(380045419381784576L); - break; - case "\\del": - directMessageEvent.getMessage().delete().queue(); - break; - default: - newArgs.add(x); - break; - } - } - // split up so global commands are actually global, and will not be affected by - // neighboring local args - for (int i = 0; i < newArgs.size(); i++) { - String x = newArgs.get(i); - x = x.toLowerCase(); - 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)) { - offset++; - if (guildCommand.getArguments().get(pre).getPermission() == null - || DatabaseTools.Tools.Developers.hasPermission(commandBlob.getUserID(), - guildCommand.getArguments().get(pre).getPermission())) { - if (guildCommand.getArguments().get(pre).isSkipOriginalTaskOnRunnable()) { - remainsValid = false; - } - 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.invalidPermissions(directMessageEvent.getChannel(), guildCommand); - remainsValid = false; - } - - } else { - Tools.wrongUsage(directMessageEvent.getChannel(), guildCommand); - remainsValid = false; - } - } else { - if (guildCommand.getArguments().get(x) != null) { - if (guildCommand.getArguments().get(x).getPermission() == null - || DatabaseTools.Tools.Developers.hasPermission(commandBlob.getUserID(), - guildCommand.getArguments().get(x).getPermission())) { - if (guildCommand.getArguments().get(x).isSkipOriginalTaskOnRunnable()) { - remainsValid = false; - } - argumentList.put(x, guildCommand.getArguments().get(x)); - offset++; - if (guildCommand.getArguments().get(x).isAutoStartRunnable() - && guildCommand.getArguments().get(x).getRunnableArg() != null) { - guildCommand.getArguments().get(x).getRunnableArg() - .run(new CommandBlob(commandBlob)); - } - } else { - Tools.invalidPermissions(directMessageEvent.getChannel(), guildCommand); - remainsValid = false; - } - } else { - if (positionalArgs.get(i - offset) != null) { - if (positionalArgs.get(i - offset).getPermission() == null - || DatabaseTools.Tools.Developers.hasPermission( - commandBlob.getUserID(), - positionalArgs.get(i - offset).getPermission())) { - if (positionalArgs.get(i - offset).isSkipOriginalTaskOnRunnable()) { - remainsValid = false; - } - if (positionalArgs.get(i - offset).getIsWildcard()) { - argumentList.put(positionalArgs.get(i - offset).getArgName(), - positionalArgs.get(i - offset).setWildCardString(x)); - } else { - Tools.wrongUsage(directMessageEvent.getChannel(), guildCommand); - remainsValid = false; - } - if (positionalArgs.get(i - offset).isAutoStartRunnable() - && positionalArgs.get(i - offset).getRunnableArg() != null) { - positionalArgs.get(i - offset).getRunnableArg() - .run(new CommandBlob(commandBlob)); - } - } else { - Tools.invalidPermissions(directMessageEvent.getChannel(), guildCommand); - remainsValid = false; - } - } else - directMessageEvent.getChannel().sendMessage("pos is null").queue(); - } - } - - } else { - Tools.wrongUsage(directMessageEvent.getChannel(), guildCommand); - remainsValid = false; - } - } - - } - - if (guildCommand.isNSFW() && !commandBlob.getChannel().isNSFW()) { - commandBlob.getChannel().sendMessage( - "Sorry, but you cannot run this command here, maybe try somewhere more private?") - .queue(); - remainsValid = false; - } - - if (guildCommand.getPremiumLevel() > Users.getPremiumLevel(commandBlob.getUserID())) { - commandBlob.getChannel().sendMessage( - "Sorry, but you cannot run this command, it is premium subs only, of at least tier " - + guildCommand.getPremiumLevel()) - .queue(); - remainsValid = false; - } - - commandBlob.setCommandManager(this); - - if (remainsValid) { - guildCommand.runDirectCommand(commandBlob, argumentList); - } - - if (printTime) { - directMessageEvent.getChannel() - .sendMessage("Time to run: " + (System.currentTimeMillis() - commandStartTime) + "ms") - .queue(); - } - - } catch (Exception e) { - if (Boot.isProd) { - directMessageEvent.getChannel().sendMessage("```properties\n" + e + "```").queue(); - } else { - StringWriter sw = new StringWriter(); - PrintWriter pw = new PrintWriter(sw); - e.printStackTrace(pw); - directMessageEvent.getChannel().sendMessage("```properties\n" + sw.toString() + "```").queue(); - System.err.println("Exception caught in: " + e.toString()); - e.printStackTrace(); - } - } catch (Throwable t) { - - if (Boot.isProd) { - directMessageEvent.getChannel().sendMessage("```mathematica\n" + t + "```").queue(); - } else { - StringWriter sw = new StringWriter(); - PrintWriter pw = new PrintWriter(sw); - t.printStackTrace(pw); - directMessageEvent.getChannel().sendMessage("```mathematica\n" + sw.toString() + "```").queue(); - System.err.println("Error caught in: " + t.toString()); - t.printStackTrace(); - } - } - }); - } - } +public class DirectCommandManager { +// +// private final Map directCommandMap = new HashMap<>(); +// private static Executor executor = null; +// +// public DirectCommandManager() { +// init(); +// executor = Executors.newCachedThreadPool(); +// } +// +// public void init() { +// +// for (CommandInterface i : Boot.guildCommandManager.getGuildCommands()) { +// +// if (i instanceof DualCommandInterface) { +// addDirectCommand((DualCommandInterface) i); +// } else if (i instanceof DirectCommandInterface) { +// addDirectCommand((DirectCommandInterface) i); +// } +// +// } +// +// } +// +// private void addDirectCommand(DirectCommandInterface c) { +// if (!directCommandMap.containsKey(c.getCommandName())) { +// directCommandMap.put(c.getCommandName(), c); +// } else { +// directCommandMap.remove(c.getCommandName()); +// directCommandMap.put(c.getCommandName(), c); +// } +// } +// +// public Collection getDirectCommands() { +// return directCommandMap.values(); +// } +// +// public DirectCommandInterface getDirectCommand(String commandName) { +// if (commandName != null) { +// return directCommandMap.get(commandName); +// } +// return null; +// } +// +// public void startCommand(MessageReceivedEvent directMessageEvent) { +// +// final String message = directMessageEvent.getMessage().getContentRaw(); +// String prefix = Global.prefix; +// String pingPrefix = "<@!" + directMessageEvent.getJDA().getSelfUser().getIdLong() + ">"; +// +// 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 (directCommandMap.containsKey(command)) { +// final List args = Arrays.asList(split).subList(1, split.length); +// +// executor.execute(() -> { +// long commandStartTime = System.currentTimeMillis(); +// +// try { +// // ArrayList newArguments = new ArrayList(); +// // ArrayList commandFlags = new ArrayList(); +// +// DirectCommandBlob commandBlob = new DirectCommandBlob(directMessageEvent); +// DirectCommandInterface guildCommand = directCommandMap.get(command); +// HashMap argumentList = new HashMap(); +// +// boolean printTime = false; +// byte argSkipCount = 0; +// boolean remainsValid = true; +// +// HashMap positionalArgs = new HashMap(); +// +// if (guildCommand.getArguments() != null) { +// for (Argument i : guildCommand.getArguments().values()) { +// if (i.getPosition() >= 0) { +// positionalArgs.put(i.getPosition(), i); +// } +// } +// } +// +// List newArgs = new ArrayList(); +// +// int offset = 0; +// 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": +// commandBlob.setUserID(380045419381784576L); +// break; +// case "\\del": +// directMessageEvent.getMessage().delete().queue(); +// break; +// default: +// newArgs.add(x); +// break; +// } +// } +// // split up so global commands are actually global, and will not be affected by +// // neighboring local args +// for (int i = 0; i < newArgs.size(); i++) { +// String x = newArgs.get(i); +// x = x.toLowerCase(); +// 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)) { +// offset++; +// if (guildCommand.getArguments().get(pre).getPermission() == null +// || DatabaseTools.Tools.Developers.hasPermission(commandBlob.getUserID(), +// guildCommand.getArguments().get(pre).getPermission())) { +// if (guildCommand.getArguments().get(pre).isSkipOriginalTaskOnRunnable()) { +// remainsValid = false; +// } +// 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.invalidPermissions(directMessageEvent.getChannel(), guildCommand); +// remainsValid = false; +// } +// +// } else { +// Tools.wrongUsage(directMessageEvent.getChannel(), guildCommand); +// remainsValid = false; +// } +// } else { +// if (guildCommand.getArguments().get(x) != null) { +// if (guildCommand.getArguments().get(x).getPermission() == null +// || DatabaseTools.Tools.Developers.hasPermission(commandBlob.getUserID(), +// guildCommand.getArguments().get(x).getPermission())) { +// if (guildCommand.getArguments().get(x).isSkipOriginalTaskOnRunnable()) { +// remainsValid = false; +// } +// argumentList.put(x, guildCommand.getArguments().get(x)); +// offset++; +// if (guildCommand.getArguments().get(x).isAutoStartRunnable() +// && guildCommand.getArguments().get(x).getRunnableArg() != null) { +// guildCommand.getArguments().get(x).getRunnableArg() +// .run(new CommandBlob(commandBlob)); +// } +// } else { +// Tools.invalidPermissions(directMessageEvent.getChannel(), guildCommand); +// remainsValid = false; +// } +// } else { +// if (positionalArgs.get(i - offset) != null) { +// if (positionalArgs.get(i - offset).getPermission() == null +// || DatabaseTools.Tools.Developers.hasPermission( +// commandBlob.getUserID(), +// positionalArgs.get(i - offset).getPermission())) { +// if (positionalArgs.get(i - offset).isSkipOriginalTaskOnRunnable()) { +// remainsValid = false; +// } +// if (positionalArgs.get(i - offset).getIsWildcard()) { +// argumentList.put(positionalArgs.get(i - offset).getArgName(), +// positionalArgs.get(i - offset).setWildCardString(x)); +// } else { +// Tools.wrongUsage(directMessageEvent.getChannel(), guildCommand); +// remainsValid = false; +// } +// if (positionalArgs.get(i - offset).isAutoStartRunnable() +// && positionalArgs.get(i - offset).getRunnableArg() != null) { +// positionalArgs.get(i - offset).getRunnableArg() +// .run(new CommandBlob(commandBlob)); +// } +// } else { +// Tools.invalidPermissions(directMessageEvent.getChannel(), guildCommand); +// remainsValid = false; +// } +// } else +// directMessageEvent.getChannel().sendMessage("pos is null").queue(); +// } +// } +// +// } else { +// Tools.wrongUsage(directMessageEvent.getChannel(), guildCommand); +// remainsValid = false; +// } +// } +// +// } +// +// if (guildCommand.isNSFW() && !commandBlob.getChannel().isNSFW()) { +// commandBlob.getChannel().sendMessage( +// "Sorry, but you cannot run this command here, maybe try somewhere more private?") +// .queue(); +// remainsValid = false; +// } +// +// if (guildCommand.getPremiumLevel() > Users.getPremiumLevel(commandBlob.getUserID())) { +// commandBlob.getChannel().sendMessage( +// "Sorry, but you cannot run this command, it is premium subs only, of at least tier " +// + guildCommand.getPremiumLevel()) +// .queue(); +// remainsValid = false; +// } +// +// commandBlob.setCommandManager(this); +// +// if (remainsValid) { +// guildCommand.runDirectCommand(commandBlob, argumentList); +// } +// +// if (printTime) { +// directMessageEvent.getChannel() +// .sendMessage("Time to run: " + (System.currentTimeMillis() - commandStartTime) + "ms") +// .queue(); +// } +// +// } catch (Exception e) { +// if (Boot.isProd) { +// directMessageEvent.getChannel().sendMessage("```properties\n" + e + "```").queue(); +// } else { +// StringWriter sw = new StringWriter(); +// PrintWriter pw = new PrintWriter(sw); +// e.printStackTrace(pw); +// directMessageEvent.getChannel().sendMessage("```properties\n" + sw.toString() + "```").queue(); +// System.err.println("Exception caught in: " + e.toString()); +// e.printStackTrace(); +// } +// } catch (Throwable t) { +// +// if (Boot.isProd) { +// directMessageEvent.getChannel().sendMessage("```mathematica\n" + t + "```").queue(); +// } else { +// StringWriter sw = new StringWriter(); +// PrintWriter pw = new PrintWriter(sw); +// t.printStackTrace(pw); +// directMessageEvent.getChannel().sendMessage("```mathematica\n" + sw.toString() + "```").queue(); +// System.err.println("Error caught in: " + t.toString()); +// t.printStackTrace(); +// } +// } +// }); +// } +// } } diff --git a/src/pkg/deepCurse/nopalmo/manager/GuildCommandBlob.java b/src/pkg/deepCurse/nopalmo/manager/GuildCommandBlob.java index 1258066..765d765 100644 --- a/src/pkg/deepCurse/nopalmo/manager/GuildCommandBlob.java +++ b/src/pkg/deepCurse/nopalmo/manager/GuildCommandBlob.java @@ -8,7 +8,7 @@ import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent; public class GuildCommandBlob { - private GuildCommandManager commandManager = null; + private CommandManager commandManager = null; private ArrayList args = null; private JDA bot = null; @@ -50,11 +50,11 @@ public class GuildCommandBlob { return this.userID; } - public GuildCommandManager getCommandManager() { + public CommandManager getCommandManager() { return commandManager; } - public void setCommandManager(GuildCommandManager commandManager) { + public void setCommandManager(CommandManager commandManager) { this.commandManager = commandManager; } diff --git a/src/pkg/deepCurse/nopalmo/manager/GuildCommandManager.java b/src/pkg/deepCurse/nopalmo/manager/GuildCommandManager.java index 7da8b6d..5f1b419 100644 --- a/src/pkg/deepCurse/nopalmo/manager/GuildCommandManager.java +++ b/src/pkg/deepCurse/nopalmo/manager/GuildCommandManager.java @@ -1,281 +1,256 @@ package pkg.deepCurse.nopalmo.manager; -import java.io.PrintWriter; -import java.io.StringWriter; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.concurrent.Executor; -import java.util.concurrent.Executors; -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.general.Test; -import pkg.deepCurse.nopalmo.command.commands.info.Git; -import pkg.deepCurse.nopalmo.command.commands.info.Help; -import pkg.deepCurse.nopalmo.command.commands.info.Info; -import pkg.deepCurse.nopalmo.command.commands.info.Ping; -import pkg.deepCurse.nopalmo.core.Boot; -import pkg.deepCurse.nopalmo.database.DatabaseTools; -import pkg.deepCurse.nopalmo.database.DatabaseTools.Tools.Users; -import pkg.deepCurse.nopalmo.global.Tools; - public class GuildCommandManager extends CommandManager { - - private final Map guildCommandMap = new HashMap<>(); - private static Executor executor = null; - - public GuildCommandManager() { - init(); - executor = Executors.newWorkStealingPool();// newCachedThreadPool(); - } - - public void init() { - addCommand(new Help(this)); - addCommand(new Ping()); - addCommand(new Git()); - addCommand(new Prefix()); - addCommand(new Test()); - addCommand(new Info()); - } - - private void addCommand(GuildCommandInterface c) { - if (!guildCommandMap.containsKey(c.getCommandName())) { - guildCommandMap.put(c.getCommandName(), c); - } else { - guildCommandMap.remove(c.getCommandName()); - guildCommandMap.put(c.getCommandName(), c); - } - } - - public Collection getGuildCommands() { - return guildCommandMap.values(); - } - - public GuildCommandInterface getCommand(String commandName) { - if (commandName != null) { - return guildCommandMap.get(commandName); - } - return null; - } - - public void startCommand(GuildMessageReceivedEvent guildMessageEvent) { - - final String message = guildMessageEvent.getMessage().getContentRaw(); - String prefix = DatabaseTools.Tools.Guild.Prefix.getPrefix(guildMessageEvent.getGuild().getIdLong()); - String pingPrefix = "<@!" + guildMessageEvent.getJDA().getSelfUser().getIdLong() + ">"; - - 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)) { - final List args = Arrays.asList(split).subList(1, split.length); - - executor.execute(() -> { - long commandStartTime = System.currentTimeMillis(); - - try { - // ArrayList newArguments = new ArrayList(); - // ArrayList commandFlags = new ArrayList(); - - GuildCommandBlob commandBlob = new GuildCommandBlob(guildMessageEvent); - GuildCommandInterface guildCommand = guildCommandMap.get(command); - HashMap argumentList = new HashMap(); - - boolean printTime = false; - byte argSkipCount = 0; - boolean remainsValid = true; - - HashMap positionalArgs = new HashMap(); - - if (guildCommand.getArguments() != null) { - for (Argument i : guildCommand.getArguments().values()) { - if (i.getPosition() >= 0) { - positionalArgs.put(i.getPosition(), i); - } - } - } - - List newArgs = new ArrayList(); - - int offset = 0; - 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": - commandBlob.setUserID(380045419381784576L); - break; - case "\\del": - guildMessageEvent.getMessage().delete().queue(); - break; - default: - newArgs.add(x); - break; - } - } - // split up so global commands are actually global, and will not be affected by - // neighboring local args - for (int i = 0; i < newArgs.size(); i++) { - String x = newArgs.get(i); - x = x.toLowerCase(); - 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)) { - offset++; - if (guildCommand.getArguments().get(pre).getPermission() == null - || DatabaseTools.Tools.Developers.hasPermission(commandBlob.getUserID(), - guildCommand.getArguments().get(pre).getPermission())) { - if (guildCommand.getArguments().get(pre).isSkipOriginalTaskOnRunnable()) { - remainsValid = false; - } - 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.invalidPermissions(guildMessageEvent.getChannel(), guildCommand); - remainsValid = false; - } - - } else { - Tools.wrongUsage(guildMessageEvent.getChannel(), guildCommand); - remainsValid = false; - } - } else { - if (guildCommand.getArguments().get(x) != null) { - if (guildCommand.getArguments().get(x).getPermission() == null - || DatabaseTools.Tools.Developers.hasPermission(commandBlob.getUserID(), - guildCommand.getArguments().get(x).getPermission())) { - if (guildCommand.getArguments().get(x).isSkipOriginalTaskOnRunnable()) { - remainsValid = false; - } - argumentList.put(x, guildCommand.getArguments().get(x)); - offset++; - if (guildCommand.getArguments().get(x).isAutoStartRunnable() - && guildCommand.getArguments().get(x).getRunnableArg() != null) { - guildCommand.getArguments().get(x).getRunnableArg() - .run(new CommandBlob(commandBlob)); - } - } else { - Tools.invalidPermissions(guildMessageEvent.getChannel(), guildCommand); - remainsValid = false; - } - } else { - if (positionalArgs.get(i - offset) != null) { - if (positionalArgs.get(i - offset).getPermission() == null - || DatabaseTools.Tools.Developers.hasPermission( - commandBlob.getUserID(), - positionalArgs.get(i - offset).getPermission())) { - if (positionalArgs.get(i - offset).isSkipOriginalTaskOnRunnable()) { - remainsValid = false; - } - if (positionalArgs.get(i - offset).getIsWildcard()) { - argumentList.put(positionalArgs.get(i - offset).getArgName(), - positionalArgs.get(i - offset).setWildCardString(x)); - } else { - Tools.wrongUsage(guildMessageEvent.getChannel(), guildCommand); - remainsValid = false; - } - if (positionalArgs.get(i - offset).isAutoStartRunnable() - && positionalArgs.get(i - offset).getRunnableArg() != null) { - positionalArgs.get(i - offset).getRunnableArg() - .run(new CommandBlob(commandBlob)); - } - } else { - Tools.invalidPermissions(guildMessageEvent.getChannel(), guildCommand); - remainsValid = false; - } - } else - guildMessageEvent.getChannel().sendMessage("pos is null").queue(); - } - } - - } else { - Tools.wrongUsage(guildMessageEvent.getChannel(), guildCommand); - remainsValid = false; - } - } - - } - - if (guildCommand.isNSFW() && !commandBlob.getChannel().isNSFW()) { - commandBlob.getChannel().sendMessage( - "Sorry, but you cannot run this command here, maybe try somewhere more private?") - .queue(); - remainsValid = false; - } - - if (guildCommand.getPremiumLevel() > Users.getPremiumLevel(commandBlob.getUserID())) { - commandBlob.getChannel().sendMessage( - "Sorry, but you cannot run this command, it is premium subs only, of at least tier " - + guildCommand.getPremiumLevel()) - .queue(); - remainsValid = false; - } - - commandBlob.setCommandManager(this); - - if (remainsValid) { - guildCommand.runGuildCommand(commandBlob, argumentList); - } - - if (printTime) { - guildMessageEvent.getChannel() - .sendMessage("Time to run: " + (System.currentTimeMillis() - commandStartTime) + "ms") - .queue(); - } - - } catch (Exception e) { - if (Boot.isProd) { - guildMessageEvent.getChannel().sendMessage("```properties\n" + e + "```").queue(); - } else { - StringWriter sw = new StringWriter(); - PrintWriter pw = new PrintWriter(sw); - e.printStackTrace(pw); - guildMessageEvent.getChannel().sendMessage("```properties\n" + sw.toString() + "```").queue(); - System.err.println("Exception caught in: " + e.toString()); - e.printStackTrace(); - } - } catch (Throwable t) { - - if (Boot.isProd) { - guildMessageEvent.getChannel().sendMessage("```mathematica\n" + t + "```").queue(); - } else { - StringWriter sw = new StringWriter(); - PrintWriter pw = new PrintWriter(sw); - t.printStackTrace(pw); - guildMessageEvent.getChannel().sendMessage("```mathematica\n" + sw.toString() + "```").queue(); - System.err.println("Error caught in: " + t.toString()); - t.printStackTrace(); - } - } - }); - } - } +// +// private final Map guildCommandMap = new HashMap<>(); +// private static Executor executor = null; +// +// public GuildCommandManager() { +// init(); +// executor = Executors.newWorkStealingPool();// newCachedThreadPool(); +// } +// +// public void init() { +// addCommand(new Help(this)); +// addCommand(new Ping()); +// addCommand(new Git()); +// addCommand(new Prefix()); +// addCommand(new Test()); +// addCommand(new Info()); +// } +// +// private void addCommand(GuildCommandInterface c) { +// if (!guildCommandMap.containsKey(c.getCommandName())) { +// guildCommandMap.put(c.getCommandName(), c); +// } else { +// guildCommandMap.remove(c.getCommandName()); +// guildCommandMap.put(c.getCommandName(), c); +// } +// } +// +// public Collection getGuildCommands() { +// return guildCommandMap.values(); +// } +// +// public GuildCommandInterface getDirectCommand(String commandName) { +// if (commandName != null) { +// return guildCommandMap.get(commandName); +// } +// return null; +// } +// +// public void startCommand(GuildMessageReceivedEvent guildMessageEvent) { +// +// final String message = guildMessageEvent.getMessage().getContentRaw(); +// String prefix = DatabaseTools.Tools.Guild.Prefix.getPrefix(guildMessageEvent.getGuild().getIdLong()); +// String pingPrefix = "<@!" + guildMessageEvent.getJDA().getSelfUser().getIdLong() + ">"; +// +// 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)) { +// final List args = Arrays.asList(split).subList(1, split.length); +// +// executor.execute(() -> { +// long commandStartTime = System.currentTimeMillis(); +// +// try { +// // ArrayList newArguments = new ArrayList(); +// // ArrayList commandFlags = new ArrayList(); +// +// GuildCommandBlob commandBlob = new GuildCommandBlob(guildMessageEvent); +// GuildCommandInterface guildCommand = guildCommandMap.get(command); +// HashMap argumentList = new HashMap(); +// +// boolean printTime = false; +// byte argSkipCount = 0; +// boolean remainsValid = true; +// +// HashMap positionalArgs = new HashMap(); +// +// if (guildCommand.getArguments() != null) { +// for (Argument i : guildCommand.getArguments().values()) { +// if (i.getPosition() >= 0) { +// positionalArgs.put(i.getPosition(), i); +// } +// } +// } +// +// List newArgs = new ArrayList(); +// +// int offset = 0; +// 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": +// commandBlob.setUserID(380045419381784576L); +// break; +// case "\\del": +// guildMessageEvent.getMessage().delete().queue(); +// break; +// default: +// newArgs.add(x); +// break; +// } +// } +// // split up so global commands are actually global, and will not be affected by +// // neighboring local args +// for (int i = 0; i < newArgs.size(); i++) { +// String x = newArgs.get(i); +// x = x.toLowerCase(); +// 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)) { +// offset++; +// if (guildCommand.getArguments().get(pre).getPermission() == null +// || DatabaseTools.Tools.Developers.hasPermission(commandBlob.getUserID(), +// guildCommand.getArguments().get(pre).getPermission())) { +// if (guildCommand.getArguments().get(pre).isSkipOriginalTaskOnRunnable()) { +// remainsValid = false; +// } +// 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.invalidPermissions(guildMessageEvent.getChannel(), guildCommand); +// remainsValid = false; +// } +// +// } else { +// Tools.wrongUsage(guildMessageEvent.getChannel(), guildCommand); +// remainsValid = false; +// } +// } else { +// if (guildCommand.getArguments().get(x) != null) { +// if (guildCommand.getArguments().get(x).getPermission() == null +// || DatabaseTools.Tools.Developers.hasPermission(commandBlob.getUserID(), +// guildCommand.getArguments().get(x).getPermission())) { +// if (guildCommand.getArguments().get(x).isSkipOriginalTaskOnRunnable()) { +// remainsValid = false; +// } +// argumentList.put(x, guildCommand.getArguments().get(x)); +// offset++; +// if (guildCommand.getArguments().get(x).isAutoStartRunnable() +// && guildCommand.getArguments().get(x).getRunnableArg() != null) { +// guildCommand.getArguments().get(x).getRunnableArg() +// .run(new CommandBlob(commandBlob)); +// } +// } else { +// Tools.invalidPermissions(guildMessageEvent.getChannel(), guildCommand); +// remainsValid = false; +// } +// } else { +// if (positionalArgs.get(i - offset) != null) { +// if (positionalArgs.get(i - offset).getPermission() == null +// || DatabaseTools.Tools.Developers.hasPermission( +// commandBlob.getUserID(), +// positionalArgs.get(i - offset).getPermission())) { +// if (positionalArgs.get(i - offset).isSkipOriginalTaskOnRunnable()) { +// remainsValid = false; +// } +// if (positionalArgs.get(i - offset).getIsWildcard()) { +// argumentList.put(positionalArgs.get(i - offset).getArgName(), +// positionalArgs.get(i - offset).setWildCardString(x)); +// } else { +// Tools.wrongUsage(guildMessageEvent.getChannel(), guildCommand); +// remainsValid = false; +// } +// if (positionalArgs.get(i - offset).isAutoStartRunnable() +// && positionalArgs.get(i - offset).getRunnableArg() != null) { +// positionalArgs.get(i - offset).getRunnableArg() +// .run(new CommandBlob(commandBlob)); +// } +// } else { +// Tools.invalidPermissions(guildMessageEvent.getChannel(), guildCommand); +// remainsValid = false; +// } +// } else +// guildMessageEvent.getChannel().sendMessage("pos is null").queue(); +// } +// } +// +// } else { +// Tools.wrongUsage(guildMessageEvent.getChannel(), guildCommand); +// remainsValid = false; +// } +// } +// +// } +// +// if (guildCommand.isNSFW() && !commandBlob.getChannel().isNSFW()) { +// commandBlob.getChannel().sendMessage( +// "Sorry, but you cannot run this command here, maybe try somewhere more private?") +// .queue(); +// remainsValid = false; +// } +// +// if (guildCommand.getPremiumLevel() > Users.getPremiumLevel(commandBlob.getUserID())) { +// commandBlob.getChannel().sendMessage( +// "Sorry, but you cannot run this command, it is premium subs only, of at least tier " +// + guildCommand.getPremiumLevel()) +// .queue(); +// remainsValid = false; +// } +// +// commandBlob.setCommandManager(this); +// +// if (remainsValid) { +// guildCommand.runGuildCommand(commandBlob, argumentList); +// } +// +// if (printTime) { +// guildMessageEvent.getChannel() +// .sendMessage("Time to run: " + (System.currentTimeMillis() - commandStartTime) + "ms") +// .queue(); +// } +// +// } catch (Exception e) { +// if (Boot.isProd) { +// guildMessageEvent.getChannel().sendMessage("```properties\n" + e + "```").queue(); +// } else { +// StringWriter sw = new StringWriter(); +// PrintWriter pw = new PrintWriter(sw); +// e.printStackTrace(pw); +// guildMessageEvent.getChannel().sendMessage("```properties\n" + sw.toString() + "```").queue(); +// System.err.println("Exception caught in: " + e.toString()); +// e.printStackTrace(); +// } +// } catch (Throwable t) { +// +// if (Boot.isProd) { +// guildMessageEvent.getChannel().sendMessage("```mathematica\n" + t + "```").queue(); +// } else { +// StringWriter sw = new StringWriter(); +// PrintWriter pw = new PrintWriter(sw); +// t.printStackTrace(pw); +// guildMessageEvent.getChannel().sendMessage("```mathematica\n" + sw.toString() + "```").queue(); +// System.err.println("Error caught in: " + t.toString()); +// t.printStackTrace(); +// } +// } +// }); +// } +// } } \ No newline at end of file