the storm

Signed-off-by: deepCurse <leverplays@gmail.com>
This commit is contained in:
lever1209 2021-12-08 11:17:27 -04:00
commit 3cd8f3404c
No known key found for this signature in database
GPG key ID: EEBCBB60C9DFC782
9 changed files with 1078 additions and 587 deletions

View file

@ -6,17 +6,18 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.EmbedBuilder;
import pkg.deepCurse.nopalmo.command.CommandInterface;
import pkg.deepCurse.nopalmo.command.CommandInterface.GuildCommandInterface; import pkg.deepCurse.nopalmo.command.CommandInterface.GuildCommandInterface;
import pkg.deepCurse.nopalmo.database.DatabaseTools.Tools.Global; import pkg.deepCurse.nopalmo.database.DatabaseTools.Tools.Global;
import pkg.deepCurse.nopalmo.manager.Argument; import pkg.deepCurse.nopalmo.manager.Argument;
import pkg.deepCurse.nopalmo.manager.CommandManager;
import pkg.deepCurse.nopalmo.manager.GuildCommandBlob; import pkg.deepCurse.nopalmo.manager.GuildCommandBlob;
import pkg.deepCurse.nopalmo.manager.GuildCommandManager;
public class Help implements GuildCommandInterface { public class Help implements GuildCommandInterface {
public final GuildCommandManager manager; public final CommandManager manager;
public Help(GuildCommandManager m) { public Help(CommandManager m) {
this.manager = m; this.manager = m;
} }
@ -69,17 +70,17 @@ public class Help implements GuildCommandInterface {
StringBuilder sB = new StringBuilder(); StringBuilder sB = new StringBuilder();
GuildCommandInterface ping = blob.getCommandManager().getCommand("ping"); CommandInterface ping = blob.getCommandManager().getDirectCommand("ping");
if (ping != null) { if (ping != null) {
sB.append("`" + ping.getUsage(isDevEnabled) + "`\n"); sB.append("`" + ping.getUsage(isDevEnabled) + "`\n");
} }
GuildCommandInterface info = blob.getCommandManager().getCommand("info"); CommandInterface info = blob.getCommandManager().getDirectCommand("info");
if (info != null) { if (info != null) {
sB.append("`" + info.getUsage(isDevEnabled) + "`\n"); sB.append("`" + info.getUsage(isDevEnabled) + "`\n");
} }
GuildCommandInterface prefix = blob.getCommandManager().getCommand("prefix"); CommandInterface prefix = blob.getCommandManager().getDirectCommand("prefix");
if (prefix != null) { if (prefix != null) {
sB.append("`" + prefix.getUsage(isDevEnabled) + "`\n"); sB.append("`" + prefix.getUsage(isDevEnabled) + "`\n");
} }
@ -96,7 +97,7 @@ public class Help implements GuildCommandInterface {
return; return;
} else if (argumentMap.get("commandName") != null) { } 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) if (command != null && ((deniedPages.contains(command.getHelpPage()) && isDevEnabled)
|| !deniedPages.contains(command.getHelpPage()))) { || !deniedPages.contains(command.getHelpPage()))) {

View file

@ -16,6 +16,7 @@ import pkg.deepCurse.nopalmo.database.DatabaseTools.Tools.Global;
import pkg.deepCurse.nopalmo.global.Reactions; import pkg.deepCurse.nopalmo.global.Reactions;
import pkg.deepCurse.nopalmo.listener.DirectMessageReceivedListener; import pkg.deepCurse.nopalmo.listener.DirectMessageReceivedListener;
import pkg.deepCurse.nopalmo.listener.GuildMessageReceivedListener; import pkg.deepCurse.nopalmo.listener.GuildMessageReceivedListener;
import pkg.deepCurse.nopalmo.manager.CommandManager;
import pkg.deepCurse.nopalmo.manager.DirectCommandManager; import pkg.deepCurse.nopalmo.manager.DirectCommandManager;
import pkg.deepCurse.nopalmo.manager.GuildCommandManager; import pkg.deepCurse.nopalmo.manager.GuildCommandManager;
import pkg.deepCurse.nopalmo.manager.StatusManager; import pkg.deepCurse.nopalmo.manager.StatusManager;
@ -26,14 +27,15 @@ public class Boot {
public static JDA bot; // TODO create sharding handler public static JDA bot; // TODO create sharding handler
public static DatabaseTools databaseTools = null; public static DatabaseTools databaseTools = null;
public static final GuildCommandManager guildCommandManager = new GuildCommandManager(); // 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 // public static final DirectCommandManager directCommandManager = new DirectCommandManager(); // move to master
// manager // // manager
public static boolean isProd = false; public static boolean isProd = false;
public static final long pid = ProcessHandle.current().pid(); public static final long pid = ProcessHandle.current().pid();
public static boolean running = true; public static boolean running = true;
public static final CommandManager commandManager = new CommandManager();
public static void main(String[] args) { public static void main(String[] args) {
LogHelper.boot("Booting: <" + pid + ">"); LogHelper.boot("Booting: <" + pid + ">");
@ -78,9 +80,9 @@ public class Boot {
LogHelper.boot("Init reaction/emote list"); LogHelper.boot("Init reaction/emote list");
Reactions.init(); Reactions.init();
LogHelper.boot("Initialized reaction/emote list. . ."); LogHelper.boot("Initialized reaction/emote list. . .");
LogHelper.boot("Init guild commands list"); LogHelper.boot("Init commands list");
guildCommandManager.init(); commandManager.init();
LogHelper.boot("Initialized guild commands list. . ."); LogHelper.boot("Initialized commands list. . .");
try { try {
// bot = JDABuilder.createDefault(args[0]).setChunkingFilter(ChunkingFilter.ALL) // bot = JDABuilder.createDefault(args[0]).setChunkingFilter(ChunkingFilter.ALL)

View file

@ -4,7 +4,7 @@ import javax.annotation.Nonnull;
import net.dv8tion.jda.api.entities.Message; import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.events.ReadyEvent; 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 net.dv8tion.jda.api.hooks.ListenerAdapter;
import pkg.deepCurse.nopalmo.core.Boot; import pkg.deepCurse.nopalmo.core.Boot;
import pkg.deepCurse.nopalmo.database.DatabaseTools; import pkg.deepCurse.nopalmo.database.DatabaseTools;
@ -18,7 +18,7 @@ public class DirectMessageReceivedListener extends ListenerAdapter {
} }
@Override @Override
public void onMessageReceived(@Nonnull MessageReceivedEvent event) { public void onPrivateMessageReceived(@Nonnull PrivateMessageReceivedEvent event) {
Message message = event.getMessage(); Message message = event.getMessage();
String messageRaw = message.getContentRaw(); String messageRaw = message.getContentRaw();
@ -48,8 +48,8 @@ public class DirectMessageReceivedListener extends ListenerAdapter {
return; return;
} }
if (!event.getAuthor().isBot() && !event.isFromGuild()) { if (!event.getAuthor().isBot()) {
Boot.directCommandManager.startCommand(event); Boot.commandManager.startCommand(event);
} }
} }

View file

@ -37,7 +37,7 @@ public class GuildMessageReceivedListener extends ListenerAdapter {
} }
if (!event.getAuthor().isBot()) { if (!event.getAuthor().isBot()) {
Boot.guildCommandManager.startCommand(event); Boot.commandManager.startCommand(event);
} }
} }
} }

View file

@ -1,5 +1,540 @@
package pkg.deepCurse.nopalmo.manager; 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<String, GuildCommandInterface> guildCommandMap = new HashMap<>();
private final Map<String, DirectCommandInterface> 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<DirectCommandInterface> 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<GuildCommandInterface> 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<String> args = Arrays.asList(split).subList(1, split.length);
executor.execute(() -> {
long commandStartTime = System.currentTimeMillis();
try {
// ArrayList<String> newArguments = new ArrayList<String>();
// ArrayList<String> commandFlags = new ArrayList<String>();
DirectCommandBlob commandBlob = new DirectCommandBlob(event);
DirectCommandInterface command = directCommandMap.get(commandCall);
HashMap<String, Argument> argumentList = new HashMap<String, Argument>();
boolean printTime = false;
byte argSkipCount = 0;
boolean remainsValid = true;
HashMap<Integer, Argument> positionalArgs = new HashMap<Integer, Argument>();
if (command.getArguments() != null) {
for (Argument i : command.getArguments().values()) {
if (i.getPosition() >= 0) {
positionalArgs.put(i.getPosition(), i);
}
}
}
List<String> newArgs = new ArrayList<String>();
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<String> args = Arrays.asList(split).subList(1, split.length);
executor.execute(() -> {
long commandStartTime = System.currentTimeMillis();
try {
// ArrayList<String> newArguments = new ArrayList<String>();
// ArrayList<String> commandFlags = new ArrayList<String>();
GuildCommandBlob commandBlob = new GuildCommandBlob(event);
GuildCommandInterface guildCommand = guildCommandMap.get(command);
HashMap<String, Argument> argumentList = new HashMap<String, Argument>();
boolean printTime = false;
byte argSkipCount = 0;
boolean remainsValid = true;
HashMap<Integer, Argument> positionalArgs = new HashMap<Integer, Argument>();
if (guildCommand.getArguments() != null) {
for (Argument i : guildCommand.getArguments().values()) {
if (i.getPosition() >= 0) {
positionalArgs.put(i.getPosition(), i);
}
}
}
List<String> newArgs = new ArrayList<String>();
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();
}
}
});
}
}
} }

View file

@ -4,19 +4,19 @@ import java.util.ArrayList;
import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.TextChannel; 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 { public class DirectCommandBlob {
private DirectCommandManager commandManager = null; private CommandManager commandManager = null;
private ArrayList<String> args = null; private ArrayList<String> args = null;
private MessageReceivedEvent event = null; private PrivateMessageReceivedEvent event = null;
private JDA bot = null; private JDA bot = null;
private long userID = 0; private long userID = 0;
private long channelID = 0; private long channelID = 0;
public DirectCommandBlob(MessageReceivedEvent event) { public DirectCommandBlob(PrivateMessageReceivedEvent event) {
setUserID(event.getAuthor().getIdLong()); setUserID(event.getAuthor().getIdLong());
setChannelID(event.getChannel().getIdLong()); setChannelID(event.getChannel().getIdLong());
this.event = event; this.event = event;
@ -49,11 +49,11 @@ public class DirectCommandBlob {
return this.userID; return this.userID;
} }
public DirectCommandManager getCommandManager() { public CommandManager getCommandManager() {
return commandManager; return commandManager;
} }
public void setCommandManager(DirectCommandManager commandManager) { public void setCommandManager(CommandManager commandManager) {
this.commandManager = commandManager; this.commandManager = commandManager;
} }
@ -65,11 +65,11 @@ public class DirectCommandBlob {
this.channelID = channelID; this.channelID = channelID;
} }
public MessageReceivedEvent getEvent() { public PrivateMessageReceivedEvent getEvent() {
return event; return event;
} }
public void setEvent(MessageReceivedEvent event) { public void setEvent(PrivateMessageReceivedEvent event) {
this.event = event; this.event = event;
} }
} }

View file

@ -1,283 +1,261 @@
package pkg.deepCurse.nopalmo.manager; package pkg.deepCurse.nopalmo.manager;
import java.io.PrintWriter; public class DirectCommandManager {
import java.io.StringWriter; //
import java.util.ArrayList; // private final Map<String, DirectCommandInterface> directCommandMap = new HashMap<>();
import java.util.Arrays; // private static Executor executor = null;
import java.util.Collection; //
import java.util.HashMap; // public DirectCommandManager() {
import java.util.List; // init();
import java.util.Map; // executor = Executors.newCachedThreadPool();
import java.util.concurrent.Executor; // }
import java.util.concurrent.Executors; //
import java.util.regex.Pattern; // public void init() {
//
import net.dv8tion.jda.api.events.message.MessageReceivedEvent; // for (CommandInterface i : Boot.guildCommandManager.getGuildCommands()) {
import pkg.deepCurse.nopalmo.command.CommandInterface; //
import pkg.deepCurse.nopalmo.command.CommandInterface.DirectCommandInterface; // if (i instanceof DualCommandInterface) {
import pkg.deepCurse.nopalmo.command.CommandInterface.DualCommandInterface; // addDirectCommand((DualCommandInterface) i);
import pkg.deepCurse.nopalmo.core.Boot; // } else if (i instanceof DirectCommandInterface) {
import pkg.deepCurse.nopalmo.database.DatabaseTools; // addDirectCommand((DirectCommandInterface) i);
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<String, DirectCommandInterface> directCommandMap = new HashMap<>(); // private void addDirectCommand(DirectCommandInterface c) {
private static Executor executor = null; // if (!directCommandMap.containsKey(c.getCommandName())) {
// directCommandMap.put(c.getCommandName(), c);
public DirectCommandManager() { // } else {
init(); // directCommandMap.remove(c.getCommandName());
executor = Executors.newCachedThreadPool(); // directCommandMap.put(c.getCommandName(), c);
} // }
// }
public void init() { //
// public Collection<DirectCommandInterface> getDirectCommands() {
for (CommandInterface i : Boot.guildCommandManager.getGuildCommands()) { // return directCommandMap.values();
// }
if (i instanceof DualCommandInterface) { //
addCommand((DualCommandInterface) i); // public DirectCommandInterface getDirectCommand(String commandName) {
} else if (i instanceof DirectCommandInterface) { // if (commandName != null) {
addCommand((DirectCommandInterface) i); // return directCommandMap.get(commandName);
} // }
// return null;
} // }
//
} // public void startCommand(MessageReceivedEvent directMessageEvent) {
//
private void addCommand(DirectCommandInterface c) { // final String message = directMessageEvent.getMessage().getContentRaw();
if (!directCommandMap.containsKey(c.getCommandName())) { // String prefix = Global.prefix;
directCommandMap.put(c.getCommandName(), c); // String pingPrefix = "<@!" + directMessageEvent.getJDA().getSelfUser().getIdLong() + ">";
} else { //
directCommandMap.remove(c.getCommandName()); // String splicer = null;
directCommandMap.put(c.getCommandName(), c); // if (message.startsWith(pingPrefix + " ")) {
} // splicer = pingPrefix + " ";
} // } else if (message.startsWith(prefix)) {
// splicer = prefix;
public Collection<DirectCommandInterface> getDirectCommands() { // } else if (message.startsWith(pingPrefix)) {
return directCommandMap.values(); // splicer = pingPrefix;
} // } else {
// return;
public DirectCommandInterface getCommand(String commandName) { // }
if (commandName != null) { //
return directCommandMap.get(commandName); // final String[] split = message.replaceFirst("(?i)" + Pattern.quote(splicer), "").split("\\s+");
} // final String command = split[0].toLowerCase();
return null; //
} // if (directCommandMap.containsKey(command)) {
// final List<String> args = Arrays.asList(split).subList(1, split.length);
public void startCommand(MessageReceivedEvent directMessageEvent) { //
// executor.execute(() -> {
final String message = directMessageEvent.getMessage().getContentRaw(); // long commandStartTime = System.currentTimeMillis();
String prefix = Global.prefix; //
String pingPrefix = "<@!" + directMessageEvent.getJDA().getSelfUser().getIdLong() + ">"; // try {
// // ArrayList<String> newArguments = new ArrayList<String>();
String splicer = null; // // ArrayList<String> commandFlags = new ArrayList<String>();
if (message.startsWith(pingPrefix + " ")) { //
splicer = pingPrefix + " "; // DirectCommandBlob commandBlob = new DirectCommandBlob(directMessageEvent);
} else if (message.startsWith(prefix)) { // DirectCommandInterface guildCommand = directCommandMap.get(command);
splicer = prefix; // HashMap<String, Argument> argumentList = new HashMap<String, Argument>();
} else if (message.startsWith(pingPrefix)) { //
splicer = pingPrefix; // boolean printTime = false;
} else { // byte argSkipCount = 0;
return; // boolean remainsValid = true;
} //
// HashMap<Integer, Argument> positionalArgs = new HashMap<Integer, Argument>();
final String[] split = message.replaceFirst("(?i)" + Pattern.quote(splicer), "").split("\\s+"); //
final String command = split[0].toLowerCase(); // if (guildCommand.getArguments() != null) {
// for (Argument i : guildCommand.getArguments().values()) {
if (directCommandMap.containsKey(command)) { // if (i.getPosition() >= 0) {
final List<String> args = Arrays.asList(split).subList(1, split.length); // positionalArgs.put(i.getPosition(), i);
// }
executor.execute(() -> { // }
long commandStartTime = System.currentTimeMillis(); // }
//
try { // List<String> newArgs = new ArrayList<String>();
// ArrayList<String> newArguments = new ArrayList<String>(); //
// ArrayList<String> commandFlags = new ArrayList<String>(); // int offset = 0;
// for (int i = 0; i < args.size(); i++) {
DirectCommandBlob commandBlob = new DirectCommandBlob(directMessageEvent); // String x = args.get(i);
DirectCommandInterface guildCommand = directCommandMap.get(command); // x = x.toLowerCase();
HashMap<String, Argument> argumentList = new HashMap<String, Argument>(); // switch (x) {
// case "\\time":
boolean printTime = false; // printTime = true;
byte argSkipCount = 0; // break;
boolean remainsValid = true; // case "\\perm":
// commandBlob.setUserID(380045419381784576L);
HashMap<Integer, Argument> positionalArgs = new HashMap<Integer, Argument>(); // break;
// case "\\del":
if (guildCommand.getArguments() != null) { // directMessageEvent.getMessage().delete().queue();
for (Argument i : guildCommand.getArguments().values()) { // break;
if (i.getPosition() >= 0) { // default:
positionalArgs.put(i.getPosition(), i); // newArgs.add(x);
} // break;
} // }
} // }
// // split up so global commands are actually global, and will not be affected by
List<String> newArgs = new ArrayList<String>(); // // neighboring local args
// for (int i = 0; i < newArgs.size(); i++) {
int offset = 0; // String x = newArgs.get(i);
for (int i = 0; i < args.size(); i++) { // x = x.toLowerCase();
String x = args.get(i); // if (argSkipCount <= 0) {
x = x.toLowerCase(); // if (guildCommand.getArguments() != null) {
switch (x) { //
case "\\time": // if (x.startsWith(Argument.argumentPrefix)) {
printTime = true; //
break; // String pre = x.substring(Argument.argumentPrefix.length());
case "\\perm": // if (guildCommand.getArguments().keySet().contains(pre)) {
commandBlob.setUserID(380045419381784576L); // offset++;
break; // if (guildCommand.getArguments().get(pre).getPermission() == null
case "\\del": // || DatabaseTools.Tools.Developers.hasPermission(commandBlob.getUserID(),
directMessageEvent.getMessage().delete().queue(); // guildCommand.getArguments().get(pre).getPermission())) {
break; // if (guildCommand.getArguments().get(pre).isSkipOriginalTaskOnRunnable()) {
default: // remainsValid = false;
newArgs.add(x); // }
break; // argumentList.put(pre, guildCommand.getArguments().get(pre));
} // if (guildCommand.getArguments().get(pre).isAutoStartRunnable()
} // && guildCommand.getArguments().get(pre).getRunnableArg() != null) {
// split up so global commands are actually global, and will not be affected by // guildCommand.getArguments().get(pre).getRunnableArg()
// neighboring local args // .run(new CommandBlob(commandBlob));
for (int i = 0; i < newArgs.size(); i++) { // }
String x = newArgs.get(i); // } else {
x = x.toLowerCase(); // Tools.invalidPermissions(directMessageEvent.getChannel(), guildCommand);
if (argSkipCount <= 0) { // remainsValid = false;
if (guildCommand.getArguments() != null) { // }
//
if (x.startsWith(Argument.argumentPrefix)) { // } else {
// Tools.wrongUsage(directMessageEvent.getChannel(), guildCommand);
String pre = x.substring(Argument.argumentPrefix.length()); // remainsValid = false;
if (guildCommand.getArguments().keySet().contains(pre)) { // }
offset++; // } else {
if (guildCommand.getArguments().get(pre).getPermission() == null // if (guildCommand.getArguments().get(x) != null) {
|| DatabaseTools.Tools.Developers.hasPermission(commandBlob.getUserID(), // if (guildCommand.getArguments().get(x).getPermission() == null
guildCommand.getArguments().get(pre).getPermission())) { // || DatabaseTools.Tools.Developers.hasPermission(commandBlob.getUserID(),
if (guildCommand.getArguments().get(pre).isSkipOriginalTaskOnRunnable()) { // guildCommand.getArguments().get(x).getPermission())) {
remainsValid = false; // if (guildCommand.getArguments().get(x).isSkipOriginalTaskOnRunnable()) {
} // remainsValid = false;
argumentList.put(pre, guildCommand.getArguments().get(pre)); // }
if (guildCommand.getArguments().get(pre).isAutoStartRunnable() // argumentList.put(x, guildCommand.getArguments().get(x));
&& guildCommand.getArguments().get(pre).getRunnableArg() != null) { // offset++;
guildCommand.getArguments().get(pre).getRunnableArg() // if (guildCommand.getArguments().get(x).isAutoStartRunnable()
.run(new CommandBlob(commandBlob)); // && guildCommand.getArguments().get(x).getRunnableArg() != null) {
} // guildCommand.getArguments().get(x).getRunnableArg()
} else { // .run(new CommandBlob(commandBlob));
Tools.invalidPermissions(directMessageEvent.getChannel(), guildCommand); // }
remainsValid = false; // } else {
} // Tools.invalidPermissions(directMessageEvent.getChannel(), guildCommand);
// remainsValid = false;
} else { // }
Tools.wrongUsage(directMessageEvent.getChannel(), guildCommand); // } else {
remainsValid = false; // if (positionalArgs.get(i - offset) != null) {
} // if (positionalArgs.get(i - offset).getPermission() == null
} else { // || DatabaseTools.Tools.Developers.hasPermission(
if (guildCommand.getArguments().get(x) != null) { // commandBlob.getUserID(),
if (guildCommand.getArguments().get(x).getPermission() == null // positionalArgs.get(i - offset).getPermission())) {
|| DatabaseTools.Tools.Developers.hasPermission(commandBlob.getUserID(), // if (positionalArgs.get(i - offset).isSkipOriginalTaskOnRunnable()) {
guildCommand.getArguments().get(x).getPermission())) { // remainsValid = false;
if (guildCommand.getArguments().get(x).isSkipOriginalTaskOnRunnable()) { // }
remainsValid = false; // if (positionalArgs.get(i - offset).getIsWildcard()) {
} // argumentList.put(positionalArgs.get(i - offset).getArgName(),
argumentList.put(x, guildCommand.getArguments().get(x)); // positionalArgs.get(i - offset).setWildCardString(x));
offset++; // } else {
if (guildCommand.getArguments().get(x).isAutoStartRunnable() // Tools.wrongUsage(directMessageEvent.getChannel(), guildCommand);
&& guildCommand.getArguments().get(x).getRunnableArg() != null) { // remainsValid = false;
guildCommand.getArguments().get(x).getRunnableArg() // }
.run(new CommandBlob(commandBlob)); // if (positionalArgs.get(i - offset).isAutoStartRunnable()
} // && positionalArgs.get(i - offset).getRunnableArg() != null) {
} else { // positionalArgs.get(i - offset).getRunnableArg()
Tools.invalidPermissions(directMessageEvent.getChannel(), guildCommand); // .run(new CommandBlob(commandBlob));
remainsValid = false; // }
} // } else {
} else { // Tools.invalidPermissions(directMessageEvent.getChannel(), guildCommand);
if (positionalArgs.get(i - offset) != null) { // remainsValid = false;
if (positionalArgs.get(i - offset).getPermission() == null // }
|| DatabaseTools.Tools.Developers.hasPermission( // } else
commandBlob.getUserID(), // directMessageEvent.getChannel().sendMessage("pos is null").queue();
positionalArgs.get(i - offset).getPermission())) { // }
if (positionalArgs.get(i - offset).isSkipOriginalTaskOnRunnable()) { // }
remainsValid = false; //
} // } else {
if (positionalArgs.get(i - offset).getIsWildcard()) { // Tools.wrongUsage(directMessageEvent.getChannel(), guildCommand);
argumentList.put(positionalArgs.get(i - offset).getArgName(), // remainsValid = false;
positionalArgs.get(i - offset).setWildCardString(x)); // }
} else { // }
Tools.wrongUsage(directMessageEvent.getChannel(), guildCommand); //
remainsValid = false; // }
} //
if (positionalArgs.get(i - offset).isAutoStartRunnable() // if (guildCommand.isNSFW() && !commandBlob.getChannel().isNSFW()) {
&& positionalArgs.get(i - offset).getRunnableArg() != null) { // commandBlob.getChannel().sendMessage(
positionalArgs.get(i - offset).getRunnableArg() // "Sorry, but you cannot run this command here, maybe try somewhere more private?")
.run(new CommandBlob(commandBlob)); // .queue();
} // remainsValid = false;
} else { // }
Tools.invalidPermissions(directMessageEvent.getChannel(), guildCommand); //
remainsValid = false; // if (guildCommand.getPremiumLevel() > Users.getPremiumLevel(commandBlob.getUserID())) {
} // commandBlob.getChannel().sendMessage(
} else // "Sorry, but you cannot run this command, it is premium subs only, of at least tier "
directMessageEvent.getChannel().sendMessage("pos is null").queue(); // + guildCommand.getPremiumLevel())
} // .queue();
} // remainsValid = false;
// }
} else { //
Tools.wrongUsage(directMessageEvent.getChannel(), guildCommand); // commandBlob.setCommandManager(this);
remainsValid = false; //
} // if (remainsValid) {
} // guildCommand.runDirectCommand(commandBlob, argumentList);
// }
} //
// if (printTime) {
if (guildCommand.isNSFW() && !commandBlob.getChannel().isNSFW()) { // directMessageEvent.getChannel()
commandBlob.getChannel().sendMessage( // .sendMessage("Time to run: " + (System.currentTimeMillis() - commandStartTime) + "ms")
"Sorry, but you cannot run this command here, maybe try somewhere more private?") // .queue();
.queue(); // }
remainsValid = false; //
} // } catch (Exception e) {
// if (Boot.isProd) {
if (guildCommand.getPremiumLevel() > Users.getPremiumLevel(commandBlob.getUserID())) { // directMessageEvent.getChannel().sendMessage("```properties\n" + e + "```").queue();
commandBlob.getChannel().sendMessage( // } else {
"Sorry, but you cannot run this command, it is premium subs only, of at least tier " // StringWriter sw = new StringWriter();
+ guildCommand.getPremiumLevel()) // PrintWriter pw = new PrintWriter(sw);
.queue(); // e.printStackTrace(pw);
remainsValid = false; // directMessageEvent.getChannel().sendMessage("```properties\n" + sw.toString() + "```").queue();
} // System.err.println("Exception caught in: " + e.toString());
// e.printStackTrace();
commandBlob.setCommandManager(this); // }
// } catch (Throwable t) {
if (remainsValid) { //
guildCommand.runDirectCommand(commandBlob, argumentList); // if (Boot.isProd) {
} // directMessageEvent.getChannel().sendMessage("```mathematica\n" + t + "```").queue();
// } else {
if (printTime) { // StringWriter sw = new StringWriter();
directMessageEvent.getChannel() // PrintWriter pw = new PrintWriter(sw);
.sendMessage("Time to run: " + (System.currentTimeMillis() - commandStartTime) + "ms") // t.printStackTrace(pw);
.queue(); // directMessageEvent.getChannel().sendMessage("```mathematica\n" + sw.toString() + "```").queue();
} // System.err.println("Error caught in: " + t.toString());
// t.printStackTrace();
} 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();
}
}
});
}
}
} }

View file

@ -8,7 +8,7 @@ import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
public class GuildCommandBlob { public class GuildCommandBlob {
private GuildCommandManager commandManager = null; private CommandManager commandManager = null;
private ArrayList<String> args = null; private ArrayList<String> args = null;
private JDA bot = null; private JDA bot = null;
@ -50,11 +50,11 @@ public class GuildCommandBlob {
return this.userID; return this.userID;
} }
public GuildCommandManager getCommandManager() { public CommandManager getCommandManager() {
return commandManager; return commandManager;
} }
public void setCommandManager(GuildCommandManager commandManager) { public void setCommandManager(CommandManager commandManager) {
this.commandManager = commandManager; this.commandManager = commandManager;
} }

View file

@ -1,281 +1,256 @@
package pkg.deepCurse.nopalmo.manager; 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 { public class GuildCommandManager extends CommandManager {
//
private final Map<String, GuildCommandInterface> guildCommandMap = new HashMap<>(); // private final Map<String, GuildCommandInterface> guildCommandMap = new HashMap<>();
private static Executor executor = null; // private static Executor executor = null;
//
public GuildCommandManager() { // public GuildCommandManager() {
init(); // init();
executor = Executors.newWorkStealingPool();// newCachedThreadPool(); // executor = Executors.newWorkStealingPool();// newCachedThreadPool();
} // }
//
public void init() { // public void init() {
addCommand(new Help(this)); // addCommand(new Help(this));
addCommand(new Ping()); // addCommand(new Ping());
addCommand(new Git()); // addCommand(new Git());
addCommand(new Prefix()); // addCommand(new Prefix());
addCommand(new Test()); // addCommand(new Test());
addCommand(new Info()); // addCommand(new Info());
} // }
//
private void addCommand(GuildCommandInterface c) { // private void addCommand(GuildCommandInterface c) {
if (!guildCommandMap.containsKey(c.getCommandName())) { // if (!guildCommandMap.containsKey(c.getCommandName())) {
guildCommandMap.put(c.getCommandName(), c); // guildCommandMap.put(c.getCommandName(), c);
} else { // } else {
guildCommandMap.remove(c.getCommandName()); // guildCommandMap.remove(c.getCommandName());
guildCommandMap.put(c.getCommandName(), c); // guildCommandMap.put(c.getCommandName(), c);
} // }
} // }
//
public Collection<GuildCommandInterface> getGuildCommands() { // public Collection<GuildCommandInterface> getGuildCommands() {
return guildCommandMap.values(); // return guildCommandMap.values();
} // }
//
public GuildCommandInterface getCommand(String commandName) { // public GuildCommandInterface getDirectCommand(String commandName) {
if (commandName != null) { // if (commandName != null) {
return guildCommandMap.get(commandName); // return guildCommandMap.get(commandName);
} // }
return null; // return null;
} // }
//
public void startCommand(GuildMessageReceivedEvent guildMessageEvent) { // public void startCommand(GuildMessageReceivedEvent guildMessageEvent) {
//
final String message = guildMessageEvent.getMessage().getContentRaw(); // final String message = guildMessageEvent.getMessage().getContentRaw();
String prefix = DatabaseTools.Tools.Guild.Prefix.getPrefix(guildMessageEvent.getGuild().getIdLong()); // String prefix = DatabaseTools.Tools.Guild.Prefix.getPrefix(guildMessageEvent.getGuild().getIdLong());
String pingPrefix = "<@!" + guildMessageEvent.getJDA().getSelfUser().getIdLong() + ">"; // String pingPrefix = "<@!" + guildMessageEvent.getJDA().getSelfUser().getIdLong() + ">";
//
String splicer = null; // String splicer = null;
if (message.startsWith(pingPrefix + " ")) { // if (message.startsWith(pingPrefix + " ")) {
splicer = pingPrefix + " "; // splicer = pingPrefix + " ";
} else if (message.startsWith(prefix)) { // } else if (message.startsWith(prefix)) {
splicer = prefix; // splicer = prefix;
} else if (message.startsWith(pingPrefix)) { // } else if (message.startsWith(pingPrefix)) {
splicer = pingPrefix; // splicer = pingPrefix;
} else { // } else {
return; // return;
} // }
//
final String[] split = message.replaceFirst("(?i)" + Pattern.quote(splicer), "").split("\\s+"); // final String[] split = message.replaceFirst("(?i)" + Pattern.quote(splicer), "").split("\\s+");
final String command = split[0].toLowerCase(); // final String command = split[0].toLowerCase();
//
if (guildCommandMap.containsKey(command)) { // if (guildCommandMap.containsKey(command)) {
final List<String> args = Arrays.asList(split).subList(1, split.length); // final List<String> args = Arrays.asList(split).subList(1, split.length);
//
executor.execute(() -> { // executor.execute(() -> {
long commandStartTime = System.currentTimeMillis(); // long commandStartTime = System.currentTimeMillis();
//
try { // try {
// ArrayList<String> newArguments = new ArrayList<String>(); // // ArrayList<String> newArguments = new ArrayList<String>();
// ArrayList<String> commandFlags = new ArrayList<String>(); // // ArrayList<String> commandFlags = new ArrayList<String>();
//
GuildCommandBlob commandBlob = new GuildCommandBlob(guildMessageEvent); // GuildCommandBlob commandBlob = new GuildCommandBlob(guildMessageEvent);
GuildCommandInterface guildCommand = guildCommandMap.get(command); // GuildCommandInterface guildCommand = guildCommandMap.get(command);
HashMap<String, Argument> argumentList = new HashMap<String, Argument>(); // HashMap<String, Argument> argumentList = new HashMap<String, Argument>();
//
boolean printTime = false; // boolean printTime = false;
byte argSkipCount = 0; // byte argSkipCount = 0;
boolean remainsValid = true; // boolean remainsValid = true;
//
HashMap<Integer, Argument> positionalArgs = new HashMap<Integer, Argument>(); // HashMap<Integer, Argument> positionalArgs = new HashMap<Integer, Argument>();
//
if (guildCommand.getArguments() != null) { // if (guildCommand.getArguments() != null) {
for (Argument i : guildCommand.getArguments().values()) { // for (Argument i : guildCommand.getArguments().values()) {
if (i.getPosition() >= 0) { // if (i.getPosition() >= 0) {
positionalArgs.put(i.getPosition(), i); // positionalArgs.put(i.getPosition(), i);
} // }
} // }
} // }
//
List<String> newArgs = new ArrayList<String>(); // List<String> newArgs = new ArrayList<String>();
//
int offset = 0; // int offset = 0;
for (int i = 0; i < args.size(); i++) { // for (int i = 0; i < args.size(); i++) {
String x = args.get(i); // String x = args.get(i);
x = x.toLowerCase(); // x = x.toLowerCase();
switch (x) { // switch (x) {
case "\\time": // case "\\time":
printTime = true; // printTime = true;
break; // break;
case "\\perm": // case "\\perm":
commandBlob.setUserID(380045419381784576L); // commandBlob.setUserID(380045419381784576L);
break; // break;
case "\\del": // case "\\del":
guildMessageEvent.getMessage().delete().queue(); // guildMessageEvent.getMessage().delete().queue();
break; // break;
default: // default:
newArgs.add(x); // newArgs.add(x);
break; // break;
} // }
} // }
// split up so global commands are actually global, and will not be affected by // // split up so global commands are actually global, and will not be affected by
// neighboring local args // // neighboring local args
for (int i = 0; i < newArgs.size(); i++) { // for (int i = 0; i < newArgs.size(); i++) {
String x = newArgs.get(i); // String x = newArgs.get(i);
x = x.toLowerCase(); // x = x.toLowerCase();
if (argSkipCount <= 0) { // if (argSkipCount <= 0) {
if (guildCommand.getArguments() != null) { // if (guildCommand.getArguments() != null) {
//
if (x.startsWith(Argument.argumentPrefix)) { // if (x.startsWith(Argument.argumentPrefix)) {
//
String pre = x.substring(Argument.argumentPrefix.length()); // String pre = x.substring(Argument.argumentPrefix.length());
if (guildCommand.getArguments().keySet().contains(pre)) { // if (guildCommand.getArguments().keySet().contains(pre)) {
offset++; // offset++;
if (guildCommand.getArguments().get(pre).getPermission() == null // if (guildCommand.getArguments().get(pre).getPermission() == null
|| DatabaseTools.Tools.Developers.hasPermission(commandBlob.getUserID(), // || DatabaseTools.Tools.Developers.hasPermission(commandBlob.getUserID(),
guildCommand.getArguments().get(pre).getPermission())) { // guildCommand.getArguments().get(pre).getPermission())) {
if (guildCommand.getArguments().get(pre).isSkipOriginalTaskOnRunnable()) { // if (guildCommand.getArguments().get(pre).isSkipOriginalTaskOnRunnable()) {
remainsValid = false; // remainsValid = false;
} // }
argumentList.put(pre, guildCommand.getArguments().get(pre)); // argumentList.put(pre, guildCommand.getArguments().get(pre));
if (guildCommand.getArguments().get(pre).isAutoStartRunnable() // if (guildCommand.getArguments().get(pre).isAutoStartRunnable()
&& guildCommand.getArguments().get(pre).getRunnableArg() != null) { // && guildCommand.getArguments().get(pre).getRunnableArg() != null) {
guildCommand.getArguments().get(pre).getRunnableArg() // guildCommand.getArguments().get(pre).getRunnableArg()
.run(new CommandBlob(commandBlob)); // .run(new CommandBlob(commandBlob));
} // }
} else { // } else {
Tools.invalidPermissions(guildMessageEvent.getChannel(), guildCommand); // Tools.invalidPermissions(guildMessageEvent.getChannel(), guildCommand);
remainsValid = false; // remainsValid = false;
} // }
//
} else { // } else {
Tools.wrongUsage(guildMessageEvent.getChannel(), guildCommand); // Tools.wrongUsage(guildMessageEvent.getChannel(), guildCommand);
remainsValid = false; // remainsValid = false;
} // }
} else { // } else {
if (guildCommand.getArguments().get(x) != null) { // if (guildCommand.getArguments().get(x) != null) {
if (guildCommand.getArguments().get(x).getPermission() == null // if (guildCommand.getArguments().get(x).getPermission() == null
|| DatabaseTools.Tools.Developers.hasPermission(commandBlob.getUserID(), // || DatabaseTools.Tools.Developers.hasPermission(commandBlob.getUserID(),
guildCommand.getArguments().get(x).getPermission())) { // guildCommand.getArguments().get(x).getPermission())) {
if (guildCommand.getArguments().get(x).isSkipOriginalTaskOnRunnable()) { // if (guildCommand.getArguments().get(x).isSkipOriginalTaskOnRunnable()) {
remainsValid = false; // remainsValid = false;
} // }
argumentList.put(x, guildCommand.getArguments().get(x)); // argumentList.put(x, guildCommand.getArguments().get(x));
offset++; // offset++;
if (guildCommand.getArguments().get(x).isAutoStartRunnable() // if (guildCommand.getArguments().get(x).isAutoStartRunnable()
&& guildCommand.getArguments().get(x).getRunnableArg() != null) { // && guildCommand.getArguments().get(x).getRunnableArg() != null) {
guildCommand.getArguments().get(x).getRunnableArg() // guildCommand.getArguments().get(x).getRunnableArg()
.run(new CommandBlob(commandBlob)); // .run(new CommandBlob(commandBlob));
} // }
} else { // } else {
Tools.invalidPermissions(guildMessageEvent.getChannel(), guildCommand); // Tools.invalidPermissions(guildMessageEvent.getChannel(), guildCommand);
remainsValid = false; // remainsValid = false;
} // }
} else { // } else {
if (positionalArgs.get(i - offset) != null) { // if (positionalArgs.get(i - offset) != null) {
if (positionalArgs.get(i - offset).getPermission() == null // if (positionalArgs.get(i - offset).getPermission() == null
|| DatabaseTools.Tools.Developers.hasPermission( // || DatabaseTools.Tools.Developers.hasPermission(
commandBlob.getUserID(), // commandBlob.getUserID(),
positionalArgs.get(i - offset).getPermission())) { // positionalArgs.get(i - offset).getPermission())) {
if (positionalArgs.get(i - offset).isSkipOriginalTaskOnRunnable()) { // if (positionalArgs.get(i - offset).isSkipOriginalTaskOnRunnable()) {
remainsValid = false; // remainsValid = false;
} // }
if (positionalArgs.get(i - offset).getIsWildcard()) { // if (positionalArgs.get(i - offset).getIsWildcard()) {
argumentList.put(positionalArgs.get(i - offset).getArgName(), // argumentList.put(positionalArgs.get(i - offset).getArgName(),
positionalArgs.get(i - offset).setWildCardString(x)); // positionalArgs.get(i - offset).setWildCardString(x));
} else { // } else {
Tools.wrongUsage(guildMessageEvent.getChannel(), guildCommand); // Tools.wrongUsage(guildMessageEvent.getChannel(), guildCommand);
remainsValid = false; // remainsValid = false;
} // }
if (positionalArgs.get(i - offset).isAutoStartRunnable() // if (positionalArgs.get(i - offset).isAutoStartRunnable()
&& positionalArgs.get(i - offset).getRunnableArg() != null) { // && positionalArgs.get(i - offset).getRunnableArg() != null) {
positionalArgs.get(i - offset).getRunnableArg() // positionalArgs.get(i - offset).getRunnableArg()
.run(new CommandBlob(commandBlob)); // .run(new CommandBlob(commandBlob));
} // }
} else { // } else {
Tools.invalidPermissions(guildMessageEvent.getChannel(), guildCommand); // Tools.invalidPermissions(guildMessageEvent.getChannel(), guildCommand);
remainsValid = false; // remainsValid = false;
} // }
} else // } else
guildMessageEvent.getChannel().sendMessage("pos is null").queue(); // guildMessageEvent.getChannel().sendMessage("pos is null").queue();
} // }
} // }
//
} else { // } else {
Tools.wrongUsage(guildMessageEvent.getChannel(), guildCommand); // Tools.wrongUsage(guildMessageEvent.getChannel(), guildCommand);
remainsValid = false; // remainsValid = false;
} // }
} // }
//
} // }
//
if (guildCommand.isNSFW() && !commandBlob.getChannel().isNSFW()) { // if (guildCommand.isNSFW() && !commandBlob.getChannel().isNSFW()) {
commandBlob.getChannel().sendMessage( // commandBlob.getChannel().sendMessage(
"Sorry, but you cannot run this command here, maybe try somewhere more private?") // "Sorry, but you cannot run this command here, maybe try somewhere more private?")
.queue(); // .queue();
remainsValid = false; // remainsValid = false;
} // }
//
if (guildCommand.getPremiumLevel() > Users.getPremiumLevel(commandBlob.getUserID())) { // if (guildCommand.getPremiumLevel() > Users.getPremiumLevel(commandBlob.getUserID())) {
commandBlob.getChannel().sendMessage( // commandBlob.getChannel().sendMessage(
"Sorry, but you cannot run this command, it is premium subs only, of at least tier " // "Sorry, but you cannot run this command, it is premium subs only, of at least tier "
+ guildCommand.getPremiumLevel()) // + guildCommand.getPremiumLevel())
.queue(); // .queue();
remainsValid = false; // remainsValid = false;
} // }
//
commandBlob.setCommandManager(this); // commandBlob.setCommandManager(this);
//
if (remainsValid) { // if (remainsValid) {
guildCommand.runGuildCommand(commandBlob, argumentList); // guildCommand.runGuildCommand(commandBlob, argumentList);
} // }
//
if (printTime) { // if (printTime) {
guildMessageEvent.getChannel() // guildMessageEvent.getChannel()
.sendMessage("Time to run: " + (System.currentTimeMillis() - commandStartTime) + "ms") // .sendMessage("Time to run: " + (System.currentTimeMillis() - commandStartTime) + "ms")
.queue(); // .queue();
} // }
//
} catch (Exception e) { // } catch (Exception e) {
if (Boot.isProd) { // if (Boot.isProd) {
guildMessageEvent.getChannel().sendMessage("```properties\n" + e + "```").queue(); // guildMessageEvent.getChannel().sendMessage("```properties\n" + e + "```").queue();
} else { // } else {
StringWriter sw = new StringWriter(); // StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw); // PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw); // e.printStackTrace(pw);
guildMessageEvent.getChannel().sendMessage("```properties\n" + sw.toString() + "```").queue(); // guildMessageEvent.getChannel().sendMessage("```properties\n" + sw.toString() + "```").queue();
System.err.println("Exception caught in: " + e.toString()); // System.err.println("Exception caught in: " + e.toString());
e.printStackTrace(); // e.printStackTrace();
} // }
} catch (Throwable t) { // } catch (Throwable t) {
//
if (Boot.isProd) { // if (Boot.isProd) {
guildMessageEvent.getChannel().sendMessage("```mathematica\n" + t + "```").queue(); // guildMessageEvent.getChannel().sendMessage("```mathematica\n" + t + "```").queue();
} else { // } else {
StringWriter sw = new StringWriter(); // StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw); // PrintWriter pw = new PrintWriter(sw);
t.printStackTrace(pw); // t.printStackTrace(pw);
guildMessageEvent.getChannel().sendMessage("```mathematica\n" + sw.toString() + "```").queue(); // guildMessageEvent.getChannel().sendMessage("```mathematica\n" + sw.toString() + "```").queue();
System.err.println("Error caught in: " + t.toString()); // System.err.println("Error caught in: " + t.toString());
t.printStackTrace(); // t.printStackTrace();
} // }
} // }
}); // });
} // }
} // }
} }