small changes before the storm

Signed-off-by: deepCurse <leverplays@gmail.com>
This commit is contained in:
deepCurse 2021-12-08 09:26:55 -04:00
parent 0fddf1e458
commit a063691657
No known key found for this signature in database
GPG key ID: EEBCBB60C9DFC782
5 changed files with 151 additions and 72 deletions

4
TODO.txt Normal file
View file

@ -0,0 +1,4 @@
mix command managers
finish loop
add socket server for better management / integration
implement pheonix in a smarter way than last time

View file

@ -33,7 +33,6 @@ public class DirectMessageReceivedListener extends ListenerAdapter {
}
String[] prefixArray = new String[] { Global.prefix, "<@! " + event.getJDA().getSelfUser().getIdLong() + ">" };
// FIXME BROKEN PING PREFIX
boolean shouldReturn = true;
for (String i : prefixArray) { // TODO switch to [] to skip for loop?

View file

@ -2,26 +2,39 @@ package pkg.deepCurse.nopalmo.manager;
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;
public class DirectCommandBlob {
private DirectCommandManager commandManager = null;
private ArrayList<String> args = null;
private ArrayList<String> args = null;
private MessageReceivedEvent event = null;
private JDA bot = null;
private long userID = 0;
private long channelID = 0;
public DirectCommandBlob(MessageReceivedEvent event) {
setUserID(event.getAuthor().getIdLong());
setChannelID(event.getChannel().getIdLong());this.event = event;
setChannelID(event.getChannel().getIdLong());
this.event = event;
this.bot = event.getJDA();
}
public ArrayList<String> getArgs() {
return args;
}
public TextChannel getChannel() {
TextChannel textChannel = bot.getTextChannelById(channelID);
if (textChannel != null) {
return textChannel;
}
return null;
}
public DirectCommandBlob setArgs(ArrayList<String> newArguments) {
this.args = newArguments;
return this;

View file

@ -2,6 +2,7 @@ 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;
@ -12,11 +13,13 @@ 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.commands.info.Git;
import pkg.deepCurse.nopalmo.command.commands.info.Ping;
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 {
@ -30,10 +33,17 @@ public class DirectCommandManager extends CommandManager {
}
public void init() {
addCommand(new Ping());
addCommand(new Git());
// addCommand(new Help(this));
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) {
@ -56,11 +66,11 @@ public class DirectCommandManager extends CommandManager {
return null;
}
public void startCommand(MessageReceivedEvent messageReceivedEvent) { // TODO SPLIT UP AND INHERIT
public void startCommand(MessageReceivedEvent directMessageEvent) {
final String message = messageReceivedEvent.getMessage().getContentRaw();
final String message = directMessageEvent.getMessage().getContentRaw();
String prefix = Global.prefix;
String pingPrefix = "<@!" + messageReceivedEvent.getJDA().getSelfUser().getIdLong() + ">";
String pingPrefix = "<@!" + directMessageEvent.getJDA().getSelfUser().getIdLong() + ">";
String splicer = null;
if (message.startsWith(pingPrefix + " ")) {
@ -86,30 +96,27 @@ public class DirectCommandManager extends CommandManager {
// ArrayList<String> newArguments = new ArrayList<String>();
// ArrayList<String> commandFlags = new ArrayList<String>();
DirectCommandBlob commandBlob = new DirectCommandBlob(messageReceivedEvent);
DirectCommandInterface directCommand = directCommandMap.get(command);
DirectCommandBlob commandBlob = new DirectCommandBlob(directMessageEvent);
DirectCommandInterface guildCommand = directCommandMap.get(command);
HashMap<String, Argument> argumentList = new HashMap<String, Argument>();
boolean printTime = false;
byte argSkipCount = 0;
boolean remainsValid = true;
// int id = 0;
HashMap<Integer, Argument> positionalArgs = new HashMap<Integer, Argument>();
if (directCommand.getArguments() != null) {
for (Argument i : directCommand.getArguments().values()) {
if (guildCommand.getArguments() != null) {
for (Argument i : guildCommand.getArguments().values()) {
if (i.getPosition() >= 0) {
positionalArgs.put(i.getPosition(), i);
}
if (i.isSkipOriginalTaskOnRunnable()) {
remainsValid = false;
}
}
}
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();
@ -121,95 +128,151 @@ public class DirectCommandManager extends CommandManager {
commandBlob.setUserID(380045419381784576L);
break;
case "\\del":
messageReceivedEvent.getMessage().delete().queue();
directMessageEvent.getMessage().delete().queue();
break;
default:
if (argSkipCount <= 0) {
if (directCommand.getArguments() != null) {
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 (positionalArgs.get(i) == null) {
if (x.startsWith(Argument.argumentPrefix)) {
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;
}
String pre = x.substring(Argument.argumentPrefix.length());
if (directCommand.getArguments().keySet().contains(pre)) {
argumentList.put(pre, directCommand.getArguments().get(pre));
if (directCommand.getArguments().get(pre).isAutoStartRunnable()
&& directCommand.getArguments().get(pre)
.getRunnableArg() != null) {
directCommand.getArguments().get(pre).getRunnableArg()
} 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.wrongUsage(messageReceivedEvent.getChannel(), directCommand);
Tools.invalidPermissions(directMessageEvent.getChannel(), guildCommand);
remainsValid = false;
}
} else {
if (directCommand.getArguments().get(x) != null) {
if (directCommand.getArguments().get(x).getPrefixRequirement()) {
Tools.wrongUsage(messageReceivedEvent.getChannel(), directCommand);
remainsValid = false;
} else {
argumentList.put(x, directCommand.getArguments().get(x));
if (directCommand.getArguments().get(x).isAutoStartRunnable()
&& directCommand.getArguments().get(x)
.getRunnableArg() != null) {
directCommand.getArguments().get(x).getRunnableArg()
.run(new CommandBlob(commandBlob));
}
}
} else {
Tools.wrongUsage(messageReceivedEvent.getChannel(), directCommand);
remainsValid = false;
}
}
} else {
if (positionalArgs.get(i).getIsWildcard()) {
argumentList.put(positionalArgs.get(i).getArgName(),
positionalArgs.get(i).setWildCardString(x));
}
if (positionalArgs.get(i).isAutoStartRunnable()
&& positionalArgs.get(i).getRunnableArg() != null) {
positionalArgs.get(i).getRunnableArg().run(new CommandBlob(commandBlob));
}
} else
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) {
directCommand.runDirectCommand(commandBlob, argumentList);
guildCommand.runDirectCommand(commandBlob, argumentList);
}
if (printTime) {
messageReceivedEvent.getChannel()
directMessageEvent.getChannel()
.sendMessage("Time to run: " + (System.currentTimeMillis() - commandStartTime) + "ms")
.queue();
}
} catch (Exception e) {
if (Boot.isProd) {
messageReceivedEvent.getChannel().sendMessage("```properties\n" + e + "```").queue();
directMessageEvent.getChannel().sendMessage("```properties\n" + e + "```").queue();
} else {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
messageReceivedEvent.getChannel().sendMessage("```properties\n" + sw.toString() + "```").queue();
directMessageEvent.getChannel().sendMessage("```properties\n" + sw.toString() + "```").queue();
System.err.println("Exception caught in: " + e.toString());
e.printStackTrace();
}
} catch (Throwable t) {
if (Boot.isProd) {
messageReceivedEvent.getChannel().sendMessage("```mathematica\n" + t + "```").queue();
directMessageEvent.getChannel().sendMessage("```mathematica\n" + t + "```").queue();
} else {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
t.printStackTrace(pw);
messageReceivedEvent.getChannel().sendMessage("```mathematica\n" + sw.toString() + "```").queue();
directMessageEvent.getChannel().sendMessage("```mathematica\n" + sw.toString() + "```").queue();
System.err.println("Error caught in: " + t.toString());
t.printStackTrace();
}

View file

@ -64,7 +64,7 @@ public class GuildCommandManager extends CommandManager {
return null;
}
public void startCommand(GuildMessageReceivedEvent guildMessageEvent) { // TODO SPLIT UP AND INHERIT
public void startCommand(GuildMessageReceivedEvent guildMessageEvent) {
final String message = guildMessageEvent.getMessage().getContentRaw();
String prefix = DatabaseTools.Tools.Guild.Prefix.getPrefix(guildMessageEvent.getGuild().getIdLong());