cuts out legacy command manager

fully joins both message and command handlers

Signed-off-by: deepCurse <leverplays@gmail.com>
This commit is contained in:
lever1209 2021-12-08 18:36:15 -04:00
commit b64a3639f3
No known key found for this signature in database
GPG key ID: EEBCBB60C9DFC782
22 changed files with 275 additions and 1193 deletions

View file

@ -12,12 +12,13 @@ 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 net.dv8tion.jda.api.entities.TextChannel;
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.command.CommandInterface.GuildCommandInterface;
import pkg.deepCurse.nopalmo.command.CommandInterface.PrivateCommandInterface;
import pkg.deepCurse.nopalmo.command.commands.general.Example;
import pkg.deepCurse.nopalmo.command.commands.general.Prefix;
import pkg.deepCurse.nopalmo.command.commands.general.Test;
import pkg.deepCurse.nopalmo.command.commands.info.Git;
@ -32,8 +33,9 @@ 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 final Map<String, CommandInterface> commandMap = new HashMap<>();
// private final Map<String, DirectCommandInterface> directCommandMap = new
// HashMap<>();
private static Executor executor = null;
public CommandManager() {
@ -42,75 +44,41 @@ public class CommandManager {
}
public void init() {
addCommand(new Help(this));
addCommand(new Ping());
addCommand(new Git());
addCommand(new Prefix());
addCommand(new Test());
addCommand(new Info());
addCommand(new Help(this));// guild
addCommand(new Ping()); // dual
addCommand(new Git()); // dual
addCommand(new Prefix()); // guild
addCommand(new Test()); // guild
addCommand(new Info()); // guild direct
addCommand(new Example()); // dual
}
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);
if (!commandMap.containsKey(c.getCommandName())) {
commandMap.put(c.getCommandName(), c);
} else {
directCommandMap.remove(c.getCommandName());
directCommandMap.put(c.getCommandName(), c);
commandMap.remove(c.getCommandName());
commandMap.put(c.getCommandName(), c);
}
}
public Collection<DirectCommandInterface> getDirectCommands() {
return directCommandMap.values();
public CommandInterface getCommand(String commandName) {
return commandMap.get(commandName);
}
public DirectCommandInterface getDirectCommand(String commandName) {
if (commandName != null) {
return directCommandMap.get(commandName);
}
return null;
public Collection<CommandInterface> getCommands() {
return commandMap.values();
}
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 void startCommand(MessageReceivedEvent event) { // TODO split up more
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 prefix = null;
if (event.isFromGuild()) {
prefix = DatabaseTools.Tools.Guild.Prefix.getPrefix(event.getGuild().getIdLong());
} else {
prefix = Global.prefix;
}
String pingPrefix = "<@!" + event.getJDA().getSelfUser().getIdLong() + ">";
String splicer = null;
@ -127,18 +95,15 @@ public class CommandManager {
final String[] split = message.replaceFirst("(?i)" + Pattern.quote(splicer), "").split("\\s+");
final String commandCall = split[0].toLowerCase();
if (guildCommandMap.containsKey(commandCall)) {
if (commandMap.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);
CommandBlob commandBlob = new CommandBlob(event, this);
CommandInterface command = commandMap.get(commandCall);
HashMap<String, Argument> argumentList = new HashMap<String, Argument>();
boolean printTime = false;
@ -166,7 +131,7 @@ public class CommandManager {
printTime = true;
break;
case "\\perm":
commandBlob.setUserID(380045419381784576L);
commandBlob.setAuthorID(380045419381784576L);
break;
case "\\del":
event.getMessage().delete().queue();
@ -190,7 +155,8 @@ public class CommandManager {
if (command.getArguments().keySet().contains(pre)) {
offset++;
if (command.getArguments().get(pre).getPermission() == null
|| DatabaseTools.Tools.Developers.hasPermission(commandBlob.getUserID(),
|| DatabaseTools.Tools.Developers.hasPermission(
commandBlob.getAuthorID(),
command.getArguments().get(pre).getPermission())) {
if (command.getArguments().get(pre).isSkipOriginalTaskOnRunnable()) {
remainsValid = false;
@ -198,8 +164,7 @@ public class CommandManager {
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));
command.getArguments().get(pre).getRunnableArg().run(commandBlob);
}
} else {
Tools.invalidPermissions(event.getChannel(), command);
@ -213,7 +178,8 @@ public class CommandManager {
} else {
if (command.getArguments().get(x) != null) {
if (command.getArguments().get(x).getPermission() == null
|| DatabaseTools.Tools.Developers.hasPermission(commandBlob.getUserID(),
|| DatabaseTools.Tools.Developers.hasPermission(
commandBlob.getAuthorID(),
command.getArguments().get(x).getPermission())) {
if (command.getArguments().get(x).isSkipOriginalTaskOnRunnable()) {
remainsValid = false;
@ -222,8 +188,7 @@ public class CommandManager {
offset++;
if (command.getArguments().get(x).isAutoStartRunnable()
&& command.getArguments().get(x).getRunnableArg() != null) {
command.getArguments().get(x).getRunnableArg()
.run(new CommandBlob(commandBlob));
command.getArguments().get(x).getRunnableArg().run(commandBlob);
}
} else {
Tools.invalidPermissions(event.getChannel(), command);
@ -233,7 +198,7 @@ public class CommandManager {
if (positionalArgs.get(i - offset) != null) {
if (positionalArgs.get(i - offset).getPermission() == null
|| DatabaseTools.Tools.Developers.hasPermission(
commandBlob.getUserID(),
commandBlob.getAuthorID(),
positionalArgs.get(i - offset).getPermission())) {
if (positionalArgs.get(i - offset).isSkipOriginalTaskOnRunnable()) {
remainsValid = false;
@ -247,8 +212,7 @@ public class CommandManager {
}
if (positionalArgs.get(i - offset).isAutoStartRunnable()
&& positionalArgs.get(i - offset).getRunnableArg() != null) {
positionalArgs.get(i - offset).getRunnableArg()
.run(new CommandBlob(commandBlob));
positionalArgs.get(i - offset).getRunnableArg().run(commandBlob);
}
} else {
Tools.invalidPermissions(event.getChannel(), command);
@ -267,14 +231,7 @@ public class CommandManager {
}
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())) {
if (command.getPremiumLevel() > Users.getPremiumLevel(commandBlob.getAuthorID())) {
commandBlob.getChannel().sendMessage(
"Sorry, but you cannot run this command, it is premium subs only, of at least tier "
+ command.getPremiumLevel())
@ -284,8 +241,23 @@ public class CommandManager {
commandBlob.setCommandManager(this);
if (event.isFromGuild()) {
if (command.isNSFW() && !((TextChannel) commandBlob.getChannel()).isNSFW()) {
commandBlob.getChannel().sendMessage(
"Sorry, but you cannot run this command here, maybe try somewhere more private?")
.queue();
remainsValid = false;
}
}
if (remainsValid) {
command.runDirectCommand(commandBlob, argumentList);
if (command instanceof DualCommandInterface) {
((DualCommandInterface) command).runDualCommand(commandBlob, argumentList);
} else if (command instanceof GuildCommandInterface && event.isFromGuild()) {
((GuildCommandInterface) command).runGuildCommand(commandBlob, argumentList);
} else if (command instanceof PrivateCommandInterface && !event.isFromGuild()) {
((PrivateCommandInterface) command).runDirectCommand(commandBlob, argumentList);
}
}
if (printTime) {
@ -321,220 +293,4 @@ public class CommandManager {
});
}
}
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();
}
}
});
}
}
}