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:
lever1209 2021-12-07 01:37:20 -04:00
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 { public void runGuildCommand(GuildCommandBlob blob, HashMap<String, Argument> argumentList) throws Exception {
if (argumentList.get("prefix") != null) { if (argumentList.get("prefix") != null) {
// System.out.println(argumentList.get("prefix").getWildCardString());
Guild.Prefix.setPrefix( Guild.Prefix.setPrefix(
blob.getEvent().getGuild().getIdLong(), argumentList.get("prefix").getWildCardString()); blob.getEvent().getGuild().getIdLong(), argumentList.get("prefix").getWildCardString());
blob.getEvent().getChannel().sendMessage("Set prefix to " + argumentList.get("prefix").getWildCardString()).queue(); 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; package pkg.deepCurse.nopalmo.command.commands.info;
import java.time.Instant; import java.time.Instant;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Random; import java.util.List;
import java.util.concurrent.TimeUnit;
import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.EmbedBuilder;
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.CommandBlob;
import pkg.deepCurse.nopalmo.manager.GuildCommandBlob; import pkg.deepCurse.nopalmo.manager.GuildCommandBlob;
import pkg.deepCurse.nopalmo.manager.GuildCommandManager; import pkg.deepCurse.nopalmo.manager.GuildCommandManager;
@ -26,13 +27,35 @@ public class Help implements GuildCommandInterface {
if (argumentMap.isEmpty()) { if (argumentMap.isEmpty()) {
EmbedBuilder embed = new EmbedBuilder().setTitle("Commands:"); 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()) { 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(); StringBuilder sB = new StringBuilder();
@ -47,6 +70,11 @@ public class Help implements GuildCommandInterface {
sB.append("`" + help.getUsage() + "`\n"); 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("Information:", "Commands to take note of:\n" + sB, false);
// embed.addField("Commands : ", "`"+sB.toString()+"`\n", true); // embed.addField("Commands : ", "`"+sB.toString()+"`\n", true);
@ -60,20 +88,12 @@ public class Help implements GuildCommandInterface {
blob.getEvent().getMember().getUser().getEffectiveAvatarUrl()); blob.getEvent().getMember().getUser().getEffectiveAvatarUrl());
embed.setTimestamp(Instant.now()); embed.setTimestamp(Instant.now());
embed.setColor(0); embed.setColor(0);
if (embed.isValidLength()) {
blob.getEvent().getChannel().sendMessageEmbeds(embed.build()).queue(); 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));
}
return; return;
} else {
} }
// ########################################################################################################################## // ##########################################################################################################################
@ -85,64 +105,64 @@ public class Help implements GuildCommandInterface {
// ########################################################################################################################## // ##########################################################################################################################
// ########################################################################################################################## // ##########################################################################################################################
try { // try {
GuildCommandInterface command = manager.getCommand(String.join("", blob.getArgs())); // GuildCommandInterface command = manager.getCommand(String.join("", blob.getArgs()));
//
// event.getChannel().sendMessage("Command help for `" + command.commandName() + // // event.getChannel().sendMessage("Command help for `" + command.commandName() +
// "`:\n\tUsage: "+ command.usageString() + "\n" + // // "`:\n\tUsage: "+ command.usageString() + "\n" +
// command.helpString()).queue(); // // command.helpString()).queue();
if (command.getHelpPage() != HelpPage.EGG) { // if (command.getHelpPage() != HelpPage.EGG) {
EmbedBuilder eB = new EmbedBuilder(); // EmbedBuilder eB = new EmbedBuilder();
eB.setTitle("Help results for: " + command.getCommandName()); // eB.setTitle("Help results for: " + command.getCommandName());
if (command.getHelp() != null) { // if (command.getHelp() != null) {
eB.addField("Help info:", command.getHelp(), false); // eB.addField("Help info:", command.getHelp(), false);
} // }
eB.addField("Usage:", command.getUsage(), false); // eB.addField("Usage:", command.getUsage(), false);
eB.setFooter("Page: " + command.getHelpPage().toString()); // eB.setFooter("Page: " + command.getHelpPage().toString());
String alias = "`"; // String alias = "`";
for (int i = 1; i < command.getCommandCalls().length; i++) { // for (int i = 1; i < command.getCommandCalls().length; i++) {
//
if (i == 1) { // if (i == 1) {
alias += command.getCommandCalls()[i]; // alias += command.getCommandCalls()[i];
} else { // } else {
alias += ", " + command.getCommandCalls()[i]; // alias += ", " + command.getCommandCalls()[i];
} // }
} // }
alias += "`"; // alias += "`";
//
String endAilias = ""; // String endAilias = "";
//
if (!alias.contentEquals("``")) { // if (!alias.contentEquals("``")) {
endAilias = "Aliases: " + alias + "\n"; // endAilias = "Aliases: " + alias + "\n";
} else { // } else {
endAilias = "Aliases: none\n"; // endAilias = "Aliases: none\n";
} // }
eB.setColor(0); // eB.setColor(0);
StringBuilder sB = new StringBuilder(); // StringBuilder sB = new StringBuilder();
sB.append(endAilias); // sB.append(endAilias);
try { // try {
sB.append("Required Permission: " + command.getRequiredPermission().getName() + "\n"); // sB.append("Required Permission: " + command.getRequiredPermission().getName() + "\n");
} catch (NullPointerException e) { // } catch (NullPointerException e) {
} // }
if (command.getTimeout() > 0) { // if (command.getTimeout() > 0) {
sB.append("Usage Timeout: " + command.getTimeout() + "\n"); // sB.append("Usage Timeout: " + command.getTimeout() + "\n");
} // }
sB.append("Premium: " + command.isPremium() + "\n"); // sB.append("Premium: " + command.isPremium() + "\n");
eB.addField("Misc", sB.toString(), false); // eB.addField("Misc", sB.toString(), false);
blob.getEvent().getChannel().sendMessageEmbeds(eB.build()).queue(); // blob.getEvent().getChannel().sendMessageEmbeds(eB.build()).queue();
} else { // } else {
throw new NullPointerException("Invalid input"); // throw new NullPointerException("Invalid input");
} // }
//
} catch (java.lang.NullPointerException e) { // } catch (java.lang.NullPointerException e) {
e.printStackTrace(); // e.printStackTrace();
blob.getEvent().getChannel() // blob.getEvent().getChannel()
.sendMessage("The command `" + String.join("", blob.getArgs()) + "` does not exist!\n" + "Use `" // .sendMessage("The command `" + String.join("", blob.getArgs()) + "` does not exist!\n" + "Use `"
+ Global.prefix + getCommandCalls()[0] + "` for a list of all my commands!") // + Global.prefix + getCommandCalls()[0] + "` for a list of all my commands!")
.queue(); // .queue();
return; // return;
//
} // }
// } // }
// https://download.java.net/java/GA/jdk16/7863447f0ab643c585b9bdebf67c69db/36/GPL/openjdk-16_linux-x64_bin.tar.gz // 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]"; 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; package pkg.deepCurse.nopalmo.manager;
import pkg.deepCurse.nopalmo.command.CommandInterface;
public class Argument { public class Argument {
// README // README

View file

@ -13,9 +13,10 @@ import java.util.regex.Pattern;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent; import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import pkg.deepCurse.nopalmo.command.CommandInterface.DirectCommandInterface; 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.commands.info.Ping;
import pkg.deepCurse.nopalmo.core.Boot; 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; import pkg.deepCurse.nopalmo.global.Tools;
public class DirectCommandManager extends CommandManager { public class DirectCommandManager extends CommandManager {
@ -30,6 +31,8 @@ public class DirectCommandManager extends CommandManager {
public void init() { public void init() {
addCommand(new Ping()); addCommand(new Ping());
addCommand(new Git());
// addCommand(new Help(this));
} }
@ -53,12 +56,24 @@ public class DirectCommandManager extends CommandManager {
return null; return null;
} }
public void startCommand(MessageReceivedEvent messageReceivedEvent) { public void startCommand(MessageReceivedEvent messageReceivedEvent) { // TODO SPLIT UP AND INHERIT
final String message = messageReceivedEvent.getMessage().getContentRaw(); 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), "") String splicer = null;
.split("\\s+"); 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(); final String command = split[0].toLowerCase();
if (directCommandMap.containsKey(command)) { if (directCommandMap.containsKey(command)) {
@ -68,6 +83,9 @@ public class DirectCommandManager extends CommandManager {
long commandStartTime = System.currentTimeMillis(); long commandStartTime = System.currentTimeMillis();
try { try {
// ArrayList<String> newArguments = new ArrayList<String>();
// ArrayList<String> commandFlags = new ArrayList<String>();
DirectCommandBlob commandBlob = new DirectCommandBlob(messageReceivedEvent); DirectCommandBlob commandBlob = new DirectCommandBlob(messageReceivedEvent);
DirectCommandInterface directCommand = directCommandMap.get(command); DirectCommandInterface directCommand = directCommandMap.get(command);
HashMap<String, Argument> argumentList = new HashMap<String, Argument>(); HashMap<String, Argument> argumentList = new HashMap<String, Argument>();
@ -75,49 +93,93 @@ public class DirectCommandManager extends CommandManager {
boolean printTime = false; boolean printTime = false;
byte argSkipCount = 0; byte argSkipCount = 0;
boolean remainsValid = true; 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(); x = x.toLowerCase();
switch (x) { switch (x) {
case "\\time": case "\\time":
printTime = true; printTime = true;
break; break;
case "\\perm": case "\\perm":
// commandFlags.add("user:380045419381784576");
commandBlob.setUserID(380045419381784576L); commandBlob.setUserID(380045419381784576L);
break; break;
case "\\del": case "\\del":
messageReceivedEvent.getMessage().delete().queue(); messageReceivedEvent.getMessage().delete().queue();
break; break;
default: // in the rewrite process default:
if (argSkipCount <= 0) { if (argSkipCount <= 0) {
if (directCommand.getArguments() != null) { if (directCommand.getArguments() != null) {
if (x.startsWith(Argument.argumentPrefix)) {
String pre = x.substring(Argument.argumentPrefix.length()); if (positionalArgs.get(i) == null) {
if (directCommand.getArguments().keySet().contains(pre)) { if (x.startsWith(Argument.argumentPrefix)) {
argumentList.put(pre, directCommand.getArguments().get(pre));
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 { } else {
Tools.wrongUsage(messageReceivedEvent.getChannel(), directCommand); if (directCommand.getArguments().get(x) != null) {
remainsValid = false; 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 { } else {
if (directCommand.getArguments().get(x).getPrefixRequirement()) { if (positionalArgs.get(i).getIsWildcard()) {
Tools.wrongUsage(messageReceivedEvent.getChannel(), directCommand); argumentList.put(positionalArgs.get(i).getArgName(),
remainsValid = false; positionalArgs.get(i).setWildCardString(x));
} else { }
argumentList.put(x, directCommand.getArguments().get(x)); if (positionalArgs.get(i).isAutoStartRunnable()
&& positionalArgs.get(i).getRunnableArg() != null) {
positionalArgs.get(i).getRunnableArg().run(new CommandBlob(commandBlob));
} }
} }
} }
} }
} }
} }
commandBlob.setCommandManager(this); commandBlob.setCommandManager(this);
if (remainsValid) { if (remainsValid) {
directCommand.runDirectCommand(commandBlob, argumentList); 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 net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
import pkg.deepCurse.nopalmo.command.CommandInterface.GuildCommandInterface; import pkg.deepCurse.nopalmo.command.CommandInterface.GuildCommandInterface;
import pkg.deepCurse.nopalmo.command.commands.general.Prefix; 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.Git;
import pkg.deepCurse.nopalmo.command.commands.info.Help; import pkg.deepCurse.nopalmo.command.commands.info.Help;
import pkg.deepCurse.nopalmo.command.commands.info.Ping; import pkg.deepCurse.nopalmo.command.commands.info.Ping;
@ -36,6 +37,7 @@ public class GuildCommandManager extends CommandManager {
addCommand(new Ping()); addCommand(new Ping());
addCommand(new Git()); addCommand(new Git());
addCommand(new Prefix()); addCommand(new Prefix());
addCommand(new Test());
} }
private void addCommand(GuildCommandInterface c) { 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 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[] prefixArray = new String[] { DatabaseTools.Tools.Guild.Prefix.getPrefix(guildMessageEvent.getGuild().getIdLong()),
// "<@!" + guildMessageEvent.getJDA().getSelfUser().getIdLong() + ">" }; // FIXME BROKEN PING PREFIX
// for () {
//
// }
String splicer = null; String splicer = null;
if (message.startsWith(pingPrefix + " ")) { if (message.startsWith(pingPrefix + " ")) {
splicer = pingPrefix + " "; splicer = pingPrefix + " ";
@ -173,13 +168,20 @@ public class GuildCommandManager extends CommandManager {
} }
} }
} else { } else {
if (positionalArgs.get(i).getIsWildcard()) { if (guildCommand.getArguments().get(x) == null) {
argumentList.put(positionalArgs.get(i).getArgName(), if (positionalArgs.get(i).getIsWildcard()) {
positionalArgs.get(i).setWildCardString(x)); argumentList.put(positionalArgs.get(i).getArgName(),
} positionalArgs.get(i).setWildCardString(x));
if (positionalArgs.get(i).isAutoStartRunnable() }
&& positionalArgs.get(i).getRunnableArg() != null) { if (positionalArgs.get(i).isAutoStartRunnable()
positionalArgs.get(i).getRunnableArg().run(new CommandBlob(commandBlob)); && positionalArgs.get(i).getRunnableArg() != null) {
positionalArgs.get(i).getRunnableArg()
.run(new CommandBlob(commandBlob));
}
} else {
// argumentList.put(x, guildCommand.getArguments().get(x));
// i--;
} }
} }
} }