unfinished commit

help is in a half written state and command manager may be unstable due
to unfinished checks with optional wildcard arguments, rewrite pending

Signed-off-by: deepCurse <leverplays@gmail.com>
This commit is contained in:
deepCurse 2021-12-07 01:37:20 -04:00
parent a5027a1790
commit addfa80197
No known key found for this signature in database
GPG key ID: EEBCBB60C9DFC782
6 changed files with 239 additions and 115 deletions

View file

@ -24,7 +24,6 @@ public class Prefix implements GuildCommandInterface {
public void runGuildCommand(GuildCommandBlob blob, HashMap<String, Argument> argumentList) throws Exception {
if (argumentList.get("prefix") != null) {
// System.out.println(argumentList.get("prefix").getWildCardString());
Guild.Prefix.setPrefix(
blob.getEvent().getGuild().getIdLong(), argumentList.get("prefix").getWildCardString());
blob.getEvent().getChannel().sendMessage("Set prefix to " + argumentList.get("prefix").getWildCardString()).queue();

View file

@ -0,0 +1,31 @@
package pkg.deepCurse.nopalmo.command.commands.general;
import java.util.HashMap;
import pkg.deepCurse.nopalmo.command.CommandInterface.GuildCommandInterface;
import pkg.deepCurse.nopalmo.manager.Argument;
import pkg.deepCurse.nopalmo.manager.GuildCommandBlob;
public class Test implements GuildCommandInterface {
@Override
public String[] getCommandCalls() {
return new String[] {"test"};
}
@Override
public HelpPage getHelpPage() {
return HelpPage.TESTING;
}
@Override
public String getHelp() {
return "A command used to test various things";
}
@Override
public void runGuildCommand(GuildCommandBlob blob, HashMap<String, Argument> argumentList) throws Exception {
blob.getEvent().getChannel().sendMessage("Tested").queue();
}
}

View file

@ -1,14 +1,15 @@
package pkg.deepCurse.nopalmo.command.commands.info;
import java.time.Instant;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.List;
import net.dv8tion.jda.api.EmbedBuilder;
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.CommandBlob;
import pkg.deepCurse.nopalmo.manager.GuildCommandBlob;
import pkg.deepCurse.nopalmo.manager.GuildCommandManager;
@ -26,13 +27,35 @@ public class Help implements GuildCommandInterface {
if (argumentMap.isEmpty()) {
EmbedBuilder embed = new EmbedBuilder().setTitle("Commands:");
HashMap<HelpPage, String> commandHash = new HashMap<HelpPage, String>();
HashMap<HelpPage, List<String>> commandHash = new HashMap<HelpPage, List<String>>();
for (GuildCommandInterface command : manager.getGuildCommands()) {
List<String> commandNameList = commandHash.get(command.getHelpPage());
if (commandNameList == null) {
commandNameList = new ArrayList<String>();
commandHash.put(command.getHelpPage(),
commandHash.get(command.getHelpPage()) + command.getCommandName());
}
commandNameList.add(command.getCommandName());
commandHash.put(command.getHelpPage(), commandNameList);
}
for (HelpPage i : HelpPage.values()) {
if (commandHash.get(i) != null) {
StringBuilder sB = new StringBuilder();
int count = 0;
for (String j : commandHash.get(i)) {
if (++count > 3) {
sB.append("\n");
count = 0;
}
sB.append("`" + j + "` ");
}
embed.addField(i.toString(), sB.toString(), true);
}
}
StringBuilder sB = new StringBuilder();
@ -47,6 +70,11 @@ public class Help implements GuildCommandInterface {
sB.append("`" + help.getUsage() + "`\n");
}
GuildCommandInterface prefix = blob.getCommandManager().getCommand("prefix");
if (prefix != null) {
sB.append("`" + prefix.getUsage() + "`\n");
}
embed.addField("Information:", "Commands to take note of:\n" + sB, false);
// embed.addField("Commands : ", "`"+sB.toString()+"`\n", true);
@ -60,20 +88,12 @@ public class Help implements GuildCommandInterface {
blob.getEvent().getMember().getUser().getEffectiveAvatarUrl());
embed.setTimestamp(Instant.now());
embed.setColor(0);
if (embed.isValidLength()) {
blob.getEvent().getChannel().sendMessageEmbeds(embed.build()).queue();
} else {
blob.getEvent().getChannel()
.sendMessage(
"Critical error!\nEmbed max size exceeded, please report this to the devs immediately")
.queue();
}
if (new Random().nextLong() == 69420l) { // i wonder who will find this, also, if you read the source to
// find this, shhhhhhhh - deepCurse
blob.getEvent().getChannel().sendMessage("we will rise above you humans")
.queue(msg -> msg.delete().queueAfter(300, TimeUnit.MILLISECONDS));
}
blob.getEvent().getChannel().sendMessageEmbeds(embed.build()).queue();
return;
} else {
}
// ##########################################################################################################################
@ -85,64 +105,64 @@ public class Help implements GuildCommandInterface {
// ##########################################################################################################################
// ##########################################################################################################################
try {
GuildCommandInterface command = manager.getCommand(String.join("", blob.getArgs()));
// event.getChannel().sendMessage("Command help for `" + command.commandName() +
// "`:\n\tUsage: "+ command.usageString() + "\n" +
// command.helpString()).queue();
if (command.getHelpPage() != HelpPage.EGG) {
EmbedBuilder eB = new EmbedBuilder();
eB.setTitle("Help results for: " + command.getCommandName());
if (command.getHelp() != null) {
eB.addField("Help info:", command.getHelp(), false);
}
eB.addField("Usage:", command.getUsage(), false);
eB.setFooter("Page: " + command.getHelpPage().toString());
String alias = "`";
for (int i = 1; i < command.getCommandCalls().length; i++) {
if (i == 1) {
alias += command.getCommandCalls()[i];
} else {
alias += ", " + command.getCommandCalls()[i];
}
}
alias += "`";
String endAilias = "";
if (!alias.contentEquals("``")) {
endAilias = "Aliases: " + alias + "\n";
} else {
endAilias = "Aliases: none\n";
}
eB.setColor(0);
StringBuilder sB = new StringBuilder();
sB.append(endAilias);
try {
sB.append("Required Permission: " + command.getRequiredPermission().getName() + "\n");
} catch (NullPointerException e) {
}
if (command.getTimeout() > 0) {
sB.append("Usage Timeout: " + command.getTimeout() + "\n");
}
sB.append("Premium: " + command.isPremium() + "\n");
eB.addField("Misc", sB.toString(), false);
blob.getEvent().getChannel().sendMessageEmbeds(eB.build()).queue();
} else {
throw new NullPointerException("Invalid input");
}
} catch (java.lang.NullPointerException e) {
e.printStackTrace();
blob.getEvent().getChannel()
.sendMessage("The command `" + String.join("", blob.getArgs()) + "` does not exist!\n" + "Use `"
+ Global.prefix + getCommandCalls()[0] + "` for a list of all my commands!")
.queue();
return;
}
// try {
// GuildCommandInterface command = manager.getCommand(String.join("", blob.getArgs()));
//
// // event.getChannel().sendMessage("Command help for `" + command.commandName() +
// // "`:\n\tUsage: "+ command.usageString() + "\n" +
// // command.helpString()).queue();
// if (command.getHelpPage() != HelpPage.EGG) {
// EmbedBuilder eB = new EmbedBuilder();
// eB.setTitle("Help results for: " + command.getCommandName());
// if (command.getHelp() != null) {
// eB.addField("Help info:", command.getHelp(), false);
// }
// eB.addField("Usage:", command.getUsage(), false);
// eB.setFooter("Page: " + command.getHelpPage().toString());
// String alias = "`";
// for (int i = 1; i < command.getCommandCalls().length; i++) {
//
// if (i == 1) {
// alias += command.getCommandCalls()[i];
// } else {
// alias += ", " + command.getCommandCalls()[i];
// }
// }
// alias += "`";
//
// String endAilias = "";
//
// if (!alias.contentEquals("``")) {
// endAilias = "Aliases: " + alias + "\n";
// } else {
// endAilias = "Aliases: none\n";
// }
// eB.setColor(0);
// StringBuilder sB = new StringBuilder();
// sB.append(endAilias);
// try {
// sB.append("Required Permission: " + command.getRequiredPermission().getName() + "\n");
// } catch (NullPointerException e) {
// }
// if (command.getTimeout() > 0) {
// sB.append("Usage Timeout: " + command.getTimeout() + "\n");
// }
// sB.append("Premium: " + command.isPremium() + "\n");
// eB.addField("Misc", sB.toString(), false);
// blob.getEvent().getChannel().sendMessageEmbeds(eB.build()).queue();
// } else {
// throw new NullPointerException("Invalid input");
// }
//
// } catch (java.lang.NullPointerException e) {
// e.printStackTrace();
// blob.getEvent().getChannel()
// .sendMessage("The command `" + String.join("", blob.getArgs()) + "` does not exist!\n" + "Use `"
// + Global.prefix + getCommandCalls()[0] + "` for a list of all my commands!")
// .queue();
// return;
//
// }
// }
// https://download.java.net/java/GA/jdk16/7863447f0ab643c585b9bdebf67c69db/36/GPL/openjdk-16_linux-x64_bin.tar.gz
@ -170,4 +190,16 @@ public class Help implements GuildCommandInterface {
return Global.prefix + getCommandCalls()[0] + " [Command name]";
}
@Override
public HashMap<String, Argument> getArguments() {
HashMap<String, Argument> args = new HashMap<String, Argument>();
args.put("commandName", new Argument("commandName").setPosition(0).setIsWildcard(true));
args.put("dev", new Argument("dev", (CommandBlob blob) -> {
blob.getChannel().sendMessage("dev").queue();
}).setPrefixRequirement(true).setAutoStartRunnable(true));
return args;
}
}

View file

@ -1,7 +1,5 @@
package pkg.deepCurse.nopalmo.manager;
import pkg.deepCurse.nopalmo.command.CommandInterface;
public class Argument {
// README

View file

@ -13,9 +13,10 @@ import java.util.regex.Pattern;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
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.core.Boot;
import pkg.deepCurse.nopalmo.database.DatabaseTools;
import pkg.deepCurse.nopalmo.database.DatabaseTools.Tools.Global;
import pkg.deepCurse.nopalmo.global.Tools;
public class DirectCommandManager extends CommandManager {
@ -30,6 +31,8 @@ public class DirectCommandManager extends CommandManager {
public void init() {
addCommand(new Ping());
addCommand(new Git());
// addCommand(new Help(this));
}
@ -53,12 +56,24 @@ public class DirectCommandManager extends CommandManager {
return null;
}
public void startCommand(MessageReceivedEvent messageReceivedEvent) {
public void startCommand(MessageReceivedEvent messageReceivedEvent) { // TODO SPLIT UP AND INHERIT
final String message = messageReceivedEvent.getMessage().getContentRaw();
String prefix = Global.prefix;
String pingPrefix = "<@!" + messageReceivedEvent.getJDA().getSelfUser().getIdLong() + ">";
final String[] split = message.replaceFirst("(?i)" + Pattern.quote(DatabaseTools.Tools.Global.prefix), "")
.split("\\s+");
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)) {
@ -68,6 +83,9 @@ public class DirectCommandManager extends CommandManager {
long commandStartTime = System.currentTimeMillis();
try {
// ArrayList<String> newArguments = new ArrayList<String>();
// ArrayList<String> commandFlags = new ArrayList<String>();
DirectCommandBlob commandBlob = new DirectCommandBlob(messageReceivedEvent);
DirectCommandInterface directCommand = directCommandMap.get(command);
HashMap<String, Argument> argumentList = new HashMap<String, Argument>();
@ -75,49 +93,93 @@ public class DirectCommandManager extends CommandManager {
boolean printTime = false;
byte argSkipCount = 0;
boolean remainsValid = true;
// int id = 0;
for (String x : args) {
HashMap<Integer, Argument> positionalArgs = new HashMap<Integer, Argument>();
if (directCommand.getArguments() != null) {
for (Argument i : directCommand.getArguments().values()) {
if (i.getPosition() >= 0) {
positionalArgs.put(i.getPosition(), i);
}
if (i.isSkipOriginalTaskOnRunnable()) {
remainsValid = false;
}
}
}
for (int i = 0; i < args.size(); i++) {
String x = args.get(i);
x = x.toLowerCase();
switch (x) {
case "\\time":
printTime = true;
break;
case "\\perm":
// commandFlags.add("user:380045419381784576");
commandBlob.setUserID(380045419381784576L);
break;
case "\\del":
messageReceivedEvent.getMessage().delete().queue();
break;
default: // in the rewrite process
default:
if (argSkipCount <= 0) {
if (directCommand.getArguments() != null) {
if (x.startsWith(Argument.argumentPrefix)) {
String pre = x.substring(Argument.argumentPrefix.length());
if (directCommand.getArguments().keySet().contains(pre)) {
argumentList.put(pre, directCommand.getArguments().get(pre));
if (positionalArgs.get(i) == null) {
if (x.startsWith(Argument.argumentPrefix)) {
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()
.run(new CommandBlob(commandBlob));
}
} else {
Tools.wrongUsage(messageReceivedEvent.getChannel(), directCommand);
remainsValid = false;
}
} else {
Tools.wrongUsage(messageReceivedEvent.getChannel(), directCommand);
remainsValid = false;
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 (directCommand.getArguments().get(x).getPrefixRequirement()) {
Tools.wrongUsage(messageReceivedEvent.getChannel(), directCommand);
remainsValid = false;
} else {
argumentList.put(x, directCommand.getArguments().get(x));
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));
}
}
}
}
}
}
commandBlob.setCommandManager(this);
if (remainsValid) {
directCommand.runDirectCommand(commandBlob, argumentList);
}

View file

@ -14,6 +14,7 @@ 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.Ping;
@ -36,6 +37,7 @@ public class GuildCommandManager extends CommandManager {
addCommand(new Ping());
addCommand(new Git());
addCommand(new Prefix());
addCommand(new Test());
}
private void addCommand(GuildCommandInterface c) {
@ -64,13 +66,6 @@ public class GuildCommandManager extends CommandManager {
String prefix = DatabaseTools.Tools.Guild.Prefix.getPrefix(guildMessageEvent.getGuild().getIdLong());
String pingPrefix = "<@!" + guildMessageEvent.getJDA().getSelfUser().getIdLong() + ">";
// String splicer = null;
// String[] prefixArray = new String[] { DatabaseTools.Tools.Guild.Prefix.getPrefix(guildMessageEvent.getGuild().getIdLong()),
// "<@!" + guildMessageEvent.getJDA().getSelfUser().getIdLong() + ">" }; // FIXME BROKEN PING PREFIX
// for () {
//
// }
String splicer = null;
if (message.startsWith(pingPrefix + " ")) {
splicer = pingPrefix + " ";
@ -173,13 +168,20 @@ public class GuildCommandManager extends CommandManager {
}
}
} 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));
if (guildCommand.getArguments().get(x) == null) {
if (positionalArgs.get(i).getIsWildcard()) {
argumentList.put(positionalArgs.get(i).getArgName(),
positionalArgs.get(i).setWildCardString(x));
}
if (positionalArgs.get(i).isAutoStartRunnable()
&& positionalArgs.get(i).getRunnableArg() != null) {
positionalArgs.get(i).getRunnableArg()
.run(new CommandBlob(commandBlob));
}
} else {
// argumentList.put(x, guildCommand.getArguments().get(x));
// i--;
}
}
}