mass code dump

Signed-off-by: deepCurse <leverplays@gmail.com>
This commit is contained in:
lever1209 2021-12-05 17:41:07 -04:00
commit 573443e4b4
No known key found for this signature in database
GPG key ID: EEBCBB60C9DFC782
17 changed files with 438 additions and 289 deletions

View file

@ -0,0 +1,62 @@
package pkg.deepCurse.nopalmo.command;
import java.util.HashMap;
import org.jetbrains.annotations.Nullable;
import pkg.deepCurse.nopalmo.manager.Argument;
import pkg.deepCurse.nopalmo.manager.GuildCommandBlob;
import pkg.deepCurse.nopalmo.manager.GuildCommandManager;
public abstract class Command {
public abstract void runCommand(GuildCommandBlob blob, GuildCommandManager commandManager,
HashMap<String, Argument> argumentList) throws Exception;
public abstract String[] getCommandCalls();
public String getCommandName() {
return getCommandCalls()[0];
}
public boolean isHidden() {
return false;
}
public boolean isNSFW() {
return false;
}
public boolean isPremium() { // im probably never gonna use this, but ill leave it in for those who want to
// see how i would implement it
return false;
}
public abstract HelpPage getHelpPage();
public enum HelpPage {
General, DEV, EGG, Moderation, Fun, Info
}
public String getHelp() {
// TODO Auto-generated method stub
return null;
}
public String getUsage() {
// TODO Auto-generated method stub
return null;
}
public int getTimeout() {
// TODO Auto-generated method stub
return 0;
}
@Nullable
public HashMap<String, Argument> getArguments() {
return null;
}
}

View file

@ -1,60 +1,15 @@
package pkg.deepCurse.nopalmo.command; package pkg.deepCurse.nopalmo.command;
import net.dv8tion.jda.api.Permission; import net.dv8tion.jda.api.Permission;
import pkg.deepCurse.nopalmo.manager.GuildCommandBlob;
import pkg.deepCurse.nopalmo.manager.GuildCommandManager;
public abstract class GuildCommand {
public abstract void run(GuildCommandBlob blob, GuildCommandManager commandManager) throws Exception;
public abstract String[] getCommandCalls();
public String getCommandName() {
return getCommandCalls()[0];
}
public boolean isHidden() {
return false;
}
public boolean isNSFW() {
return false;
}
public abstract class GuildCommand extends Command {
public Permission[] getRequiredPermissions() { public Permission[] getRequiredPermissions() {
return null; return null;
} }
public boolean isPremium() { // im probably never gonna use this, but ill leave it in for those who want to
// see how i would implement it
return false;
}
public abstract HelpPage getHelpPage();
public enum HelpPage {
General, DEV, EGG, Moderation, Fun, Info
}
public String getHelp() {
// TODO Auto-generated method stub
return null;
}
public Permission getRequiredPermission() { public Permission getRequiredPermission() {
// TODO Auto-generated method stub // TODO Auto-generated method stub
return null; return null;
} }
public String getUsage() {
// TODO Auto-generated method stub
return null;
}
public int getTimeout() {
// TODO Auto-generated method stub
return 0;
}
} }

View file

@ -1,13 +1,16 @@
package pkg.deepCurse.nopalmo.command.guildCommand.info; package pkg.deepCurse.nopalmo.command.guildCommand.info;
import java.time.Instant; import java.time.Instant;
import java.util.HashMap;
import java.util.Random; import java.util.Random;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.interactions.commands.Command;
import pkg.deepCurse.nopalmo.command.GuildCommand; import pkg.deepCurse.nopalmo.command.GuildCommand;
import pkg.deepCurse.nopalmo.database.DatabaseTools.Tools.Global; import pkg.deepCurse.nopalmo.database.DatabaseTools.Tools.Global;
import pkg.deepCurse.nopalmo.global.Tools; import pkg.deepCurse.nopalmo.global.Tools;
import pkg.deepCurse.nopalmo.manager.Argument;
import pkg.deepCurse.nopalmo.manager.GuildCommandBlob; import pkg.deepCurse.nopalmo.manager.GuildCommandBlob;
import pkg.deepCurse.nopalmo.manager.GuildCommandManager; import pkg.deepCurse.nopalmo.manager.GuildCommandManager;
@ -20,45 +23,32 @@ public class Help extends GuildCommand {
} }
@Override @Override
public void run(GuildCommandBlob blob, GuildCommandManager commandManager) throws Exception { public void runCommand(GuildCommandBlob blob, GuildCommandManager commandManager,
// TextChannel channel = event.getChannel(); HashMap<String, Argument> argumentMap) throws Exception {
// Member user = event.getMember();
// if (Tools.devIdCheck(null, user.getId(), channel, null)) { if (argumentMap.isEmpty()) {
if (blob.getArgs().size() > 1) {
Tools.wrongUsage(blob.getGuildMessageEvent().getChannel(), this);
return;
}
if (blob.getArgs().isEmpty()) {
EmbedBuilder embed = new EmbedBuilder().setTitle("Commands:"); EmbedBuilder embed = new EmbedBuilder().setTitle("Commands:");
for (HelpPage i : HelpPage.values()) { HashMap<HelpPage, String> commandHash = new HashMap<HelpPage, String>();
if (i != HelpPage.DEV) {
StringBuilder pageData = new StringBuilder();
for (GuildCommand command : manager.getGuildCommands()) { for (GuildCommand command : manager.getGuildCommands()) {
if (!command.isHidden() & command.getHelpPage() != HelpPage.EGG) {
if (command.getHelpPage() == i) {
if (!pageData.toString().contains(command.getCommandName())) { commandHash.put(command.getHelpPage(),
pageData.append("`" + command.getCommandName() + "`\n"); commandHash.get(command.getHelpPage()) + command.getCommandName());
}
}
}
}
if (!pageData.toString().isBlank()) {
embed.addField(i.name() + ": ", pageData.toString(), true);
}
// pageData.delete(0, pageData.length());
}
} }
StringBuilder sB = new StringBuilder(); StringBuilder sB = new StringBuilder();
sB.append("`manual`\n"); GuildCommand ping = commandManager.getCommand("ping");
sB.append("`ping`\n"); if (ping != null) {
sB.append("`support`\n"); sB.append("`"+ping.getUsage()+"`\n");
// sB.append("`"+Global.Prefix+"\n"); }
GuildCommand help = commandManager.getCommand("help");
if (help != null) {
sB.append("`"+help.getUsage()+"`\n");
}
embed.addField("Information:", "Commands to take note of:\n" + sB, false); embed.addField("Information:", "Commands to take note of:\n" + sB, false);
@ -74,7 +64,7 @@ public class Help extends GuildCommand {
embed.setTimestamp(Instant.now()); embed.setTimestamp(Instant.now());
embed.setColor(0); embed.setColor(0);
if (embed.isValidLength()) { if (embed.isValidLength()) {
blob.getGuildMessageEvent().getChannel().sendMessage(embed.build()).queue(); blob.getGuildMessageEvent().getChannel().sendMessageEmbeds(embed.build()).queue();
} else { } else {
blob.getGuildMessageEvent().getChannel() blob.getGuildMessageEvent().getChannel()
.sendMessage( .sendMessage(
@ -88,6 +78,16 @@ public class Help extends GuildCommand {
} }
return; return;
} }
// ##########################################################################################################################
// ##########################################################################################################################
// ##########################################################################################################################
// ##########################################################################################################################
// ##########################################################################################################################
try { try {
GuildCommand command = manager.getCommand(String.join("", blob.getArgs())); GuildCommand command = manager.getCommand(String.join("", blob.getArgs()));
@ -132,15 +132,17 @@ public class Help extends GuildCommand {
} }
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.getGuildMessageEvent().getChannel().sendMessage(eB.build()).queue(); blob.getGuildMessageEvent().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.getGuildMessageEvent().getChannel().sendMessage("The command `" + String.join("", blob.getArgs()) + "` does not exist!\n" + "Use `" blob.getGuildMessageEvent().getChannel()
+ Global.prefix + getCommandCalls()[0] + "` for a list of all my commands!").queue(); .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; return;
} }
@ -176,9 +178,4 @@ public class Help extends GuildCommand {
return Global.prefix + getCommandCalls()[0] + " [Command name]"; return Global.prefix + getCommandCalls()[0] + " [Command name]";
} }
@Override
public int getTimeout() {
// TODO Auto-generated method stub
return 0;
}
} }

View file

@ -1,58 +1,70 @@
package pkg.deepCurse.nopalmo.command.guildCommand.info; package pkg.deepCurse.nopalmo.command.guildCommand.info;
import net.dv8tion.jda.api.entities.TextChannel; import java.util.HashMap;
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent; import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
import pkg.deepCurse.nopalmo.command.GuildCommand; import pkg.deepCurse.nopalmo.command.GuildCommand;
import pkg.deepCurse.nopalmo.core.Boot; import pkg.deepCurse.nopalmo.database.DatabaseTools.Tools.Global;
import pkg.deepCurse.nopalmo.global.Tools; import pkg.deepCurse.nopalmo.manager.Argument;
import pkg.deepCurse.nopalmo.manager.GuildCommandBlob; import pkg.deepCurse.nopalmo.manager.GuildCommandBlob;
import pkg.deepCurse.nopalmo.manager.GuildCommandManager; import pkg.deepCurse.nopalmo.manager.GuildCommandManager;
public class Ping extends GuildCommand { public class Ping extends GuildCommand {
@Override @Override
public void run(GuildCommandBlob blob, GuildCommandManager commandManager) public void runCommand(GuildCommandBlob blob, GuildCommandManager commandManager,
throws Exception { HashMap<String, Argument> argumentMap) throws Exception {
GuildMessageReceivedEvent event = blob.getGuildMessageEvent(); GuildMessageReceivedEvent event = blob.getGuildMessageEvent();
TextChannel channel = event.getChannel(); if (argumentMap.isEmpty()) {
event.getChannel().sendMessage("Pong!\n" + event.getJDA().getGatewayPing() + "ms\n").queue();
return;
}
channel.sendMessage("You are: " + blob.getUserID()).queue(); if (argumentMap.get("all") != null) {
if (blob.getArgs().size() == 0) {
// new Main();
channel.sendMessage("Pong!\n" + event.getJDA().getGatewayPing() + "ms\n"
// + "Sorry if the ping is too high, im currently hosting on an under powered
// laptop out in the countryside...\n"
// + "This will be fixed in at most 2 days..."
).queue();
// long pang = Main.bot.getGatewayPing();
} else if (blob.getArgs().get(0).contentEquals("all")) { event.getChannel().sendMessage("Gathering data. . .").queue(msg -> {
long timeToProcess = System.currentTimeMillis();
try { // TODO rewrite this block, all tries are not good practice
channel.sendMessage("Gathering data...").queue(msg -> {
try {
long timeToProcess = System.currentTimeMillis();
String out = "Pong!\n" + "Google: " + services.UptimePing.sendPing("www.google.com") + "ms\n" String out = "Pong!\n" + "Google: " + services.UptimePing.sendPing("www.google.com") + "ms\n"
+ "JDA Gateway: " + event.getJDA().getGatewayPing() + "ms\n" + "www.discord.com: " + "JDA Gateway: " + event.getJDA().getGatewayPing() + "ms\n" + "www.discord.com: "
+ services.UptimePing.sendPing("www.discord.com") + "ms"; + services.UptimePing.sendPing("www.discord.com") + "ms";
msg.editMessage(out + "\nTime to process: " + (System.currentTimeMillis() - timeToProcess) + "ms")
msg.editMessage(out + "\nTime to process: " + (timeToProcess - System.currentTimeMillis()) + "ms").queue(); .queue();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace();
} }
}); });
} else }
Tools.wrongUsage(channel, this);
// if (argumentArray == null || argumentArray.isEmpty()) {
//
// return;
// } else {
//
// for (Argument i : argumentArray) {
// if (i.getArgName().contentEquals("all")) {
//
// } else {
// Tools.wrongUsage(event.getChannel(), this);
// }
// }
// return;
// }
} }
@Override @Override
public String[] getCommandCalls() { public String[] getCommandCalls() {
return new String[] {"ping"}; return new String[] { "ping" };
}
@Override
public String getUsage() {
return Global.prefix+"ping [" + Argument.argumentPrefix + "all]";
} }
@Override @Override
@ -60,4 +72,13 @@ public class Ping extends GuildCommand {
return HelpPage.Info; return HelpPage.Info;
} }
@Override
public HashMap<String, Argument> getArguments() {
HashMap<String, Argument> args = new HashMap<String, Argument>();
args.put("all", new Argument("all").setPrefixRequirement(true));
return args;
}
} }

View file

@ -62,7 +62,7 @@ public class Boot {
long bootTime = System.currentTimeMillis() - preBootTime; long bootTime = System.currentTimeMillis() - preBootTime;
System.out.println("Taken "+bootTime+"ms to boot"); Log.boot("Taken "+bootTime+"ms to boot");
} }
} }

View file

@ -24,6 +24,7 @@ public class DatabaseTools {
Global.updatePrefix(); Global.updatePrefix();
} }
@SuppressWarnings("deprecation")
public static Connection createConnection(String password) throws SQLException { public static Connection createConnection(String password) throws SQLException {
String dbName = Boot.isProd ? "nopalmo" : "chaos"; String dbName = Boot.isProd ? "nopalmo" : "chaos";
@ -40,34 +41,11 @@ public class DatabaseTools {
try { try {
return DriverManager.getConnection(url, username, password); return DriverManager.getConnection(url, username, password);
} catch (SQLException e) { } catch (SQLException e) {
sqlTranslate("Generate connection", e); SQLCode.sqlTranslate("Generate connection", e);
throw new SQLException(e); throw new SQLException(e);
} }
} }
private static void sqlTranslate(String action, int errorCode) {
switch (errorCode) {
case 1062:
System.err.println("Failed to execute; errorCode=" + errorCode + "; ER_DUP_ENTRY; On action " + action);
break;
case 1054:
System.err.println("Failed to execute; errorCode=" + errorCode + "; ER_BAD_FIELD_ERROR; On action " + action);
break;
default:
System.err.println("Failed to execute; errorCode=" + errorCode + "; Unknown code; On action " + action);
break;
}
}
@Deprecated
private static void sqlTranslate(String action, SQLException e) {
sqlTranslate(action, e.getErrorCode());
}
private static void sqlTranslate(PreparedStatement pstmt, SQLException e) {
sqlTranslate(pstmt.toString(), e.getErrorCode());
}
private static void checkUpdateCounts(PreparedStatement pstmt, int[] updateCounts) { private static void checkUpdateCounts(PreparedStatement pstmt, int[] updateCounts) {
checkUpdateCounts(pstmt.toString(), updateCounts); checkUpdateCounts(pstmt.toString(), updateCounts);
} }
@ -87,8 +65,9 @@ public class DatabaseTools {
public class Tools { public class Tools {
// these sub classes will represent tables and the methods therein will be for actions within said table // these sub classes will represent tables and the methods therein will be for
// actions within said table
public class Guild { public class Guild {
public class Prefix { public class Prefix {
@ -110,7 +89,7 @@ public class DatabaseTools {
return createPrefix(guildID, Global.prefix); return createPrefix(guildID, Global.prefix);
} }
} catch (SQLException e) { } catch (SQLException e) {
sqlTranslate(query, e.getErrorCode()); SQLCode.getMessage(query, e.getErrorCode());
return null; return null;
} finally { // @formatter:off } finally { // @formatter:off
try {if (rs != null)rs.close();} catch (Exception e) {} try {if (rs != null)rs.close();} catch (Exception e) {}
@ -143,7 +122,7 @@ public class DatabaseTools {
// connection.commit(); // connection.commit();
return prefix; return prefix;
} catch (SQLException e) { } catch (SQLException e) {
sqlTranslate(pstmt, e); SQLCode.sqlTranslate(pstmt, e);
for (int i : new int[] { 1062 }) { for (int i : new int[] { 1062 }) {
if (i == e.getErrorCode()) { if (i == e.getErrorCode()) {
return setPrefix(connection, guildID, prefix); return setPrefix(connection, guildID, prefix);
@ -181,7 +160,7 @@ public class DatabaseTools {
conn.commit(); conn.commit();
return prefix; return prefix;
} catch (SQLException e) { } catch (SQLException e) {
sqlTranslate(pstmt, e); SQLCode.sqlTranslate(pstmt, e);
try { try {
conn.rollback(); conn.rollback();
} catch (Exception e2) { } catch (Exception e2) {
@ -212,7 +191,7 @@ public class DatabaseTools {
} }
} catch (SQLException e) { } catch (SQLException e) {
sqlTranslate(query, e.getErrorCode()); SQLCode.getMessage(query, e.getErrorCode());
// System.out.println("eeeeee"); // System.out.println("eeeeee");
return false; return false;
} finally { } finally {
@ -236,7 +215,7 @@ public class DatabaseTools {
} }
public class Global { public class Global {
public static String prefix = null; public static String prefix = null;
public static void updatePrefix() throws SQLException { public static void updatePrefix() throws SQLException {
@ -257,7 +236,7 @@ public class DatabaseTools {
} }
} catch (SQLException e) { } catch (SQLException e) {
sqlTranslate(query, e.getErrorCode()); SQLCode.getMessage(query, e.getErrorCode());
// System.out.println("eeeeee"); // System.out.println("eeeeee");
throw new SQLException(e); throw new SQLException(e);
} finally { } finally {

View file

@ -0,0 +1,33 @@
package pkg.deepCurse.nopalmo.database;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class SQLCode {
public static String getMessage(String action, int errorCode) {
switch (errorCode) {
case 1062:
System.err.println("Failed to execute; errorCode=" + errorCode + "; ER_DUP_ENTRY; On action " + action);
return "";
case 1054:
System.err.println("Failed to execute; errorCode=" + errorCode + "; ER_BAD_FIELD_ERROR; On action " + action);
return "";
default:
System.err.println("Failed to execute; errorCode=" + errorCode + "; Unknown code; On action " + action);
return "";
}
}
@Deprecated
public static void sqlTranslate(String action, SQLException e) {
SQLCode.getMessage(action, e.getErrorCode());
}
public static void sqlTranslate(PreparedStatement pstmt, SQLException e) {
SQLCode.getMessage(pstmt.toString(), e.getErrorCode());
}
}

View file

@ -6,7 +6,7 @@ import pkg.deepCurse.nopalmo.command.GuildCommand;
public class Tools { public class Tools {
public static void wrongUsage(TextChannel tc, GuildCommand c) { public static void wrongUsage(TextChannel tc, GuildCommand c) {
tc.sendMessage("Wrong Command Usage!\n" + c.getCommandName()).queue(); tc.sendMessage("Wrong Command Usage!\n" + c.getUsage()).queue();
} }
} }

View file

@ -9,11 +9,9 @@ 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;
import pkg.deepCurse.nopalmo.database.DatabaseTools.Tools.Global; import pkg.deepCurse.nopalmo.database.DatabaseTools.Tools.Global;
import pkg.deepCurse.nopalmo.global.Reactions;
import pkg.deepCurse.nopalmo.manager.GuildCommandManager;
public class GuildMessageReceivedListener extends ListenerAdapter { public class GuildMessageReceivedListener extends ListenerAdapter {
@Override @Override
public void onReady(@Nonnull ReadyEvent event) { public void onReady(@Nonnull ReadyEvent event) {
System.out.println("GuildMessageReceivedListener is now ready\n" + event.getGuildAvailableCount() + "/" System.out.println("GuildMessageReceivedListener is now ready\n" + event.getGuildAvailableCount() + "/"
@ -25,38 +23,44 @@ public class GuildMessageReceivedListener extends ListenerAdapter {
public void onGuildMessageReceived(@Nonnull GuildMessageReceivedEvent event) { public void onGuildMessageReceived(@Nonnull GuildMessageReceivedEvent event) {
Message message = event.getMessage(); Message message = event.getMessage();
String messageRaw = message.getContentRaw(); String messageRaw = message.getContentRaw();
if (messageRaw.contentEquals(Global.prefix + Global.prefix)
&& DatabaseTools.Tools.Developers.canPowerOffBot(event.getAuthor().getIdLong())) {
// message.addReaction(Reactions.getReaction("galaxyThumb")).complete(); TODO re enable
message.delete().complete();
// pause thread as last resort
event.getJDA().shutdown();
System.exit(0);
}
String[] prefixArray = new String[] { DatabaseTools.Tools.Guild.Prefix.getPrefix(event.getGuild().getIdLong()), String[] prefixArray = new String[] { DatabaseTools.Tools.Guild.Prefix.getPrefix(event.getGuild().getIdLong()),
"<@!" + event.getJDA().getSelfUser().getIdLong() + ">" }; "<@!" + event.getJDA().getSelfUser().getIdLong() + ">" };
boolean shouldReturn = true; boolean shouldReturn = true;
for (String i : prefixArray) { for (String i : prefixArray) { // switch to [] to skip for loop?
if (messageRaw.startsWith(i)) { if (messageRaw.startsWith(i)) {
shouldReturn = false; shouldReturn = false;
} }
} }
if (messageRaw.contentEquals(Global.prefix + Global.prefix)
&& DatabaseTools.Tools.Developers.canPowerOffBot(event.getAuthor().getIdLong())) {
message.addReaction(Reactions.getReaction("galaxyThumb")).complete();
System.out.println("Shutting down; id " + event.getAuthor().getIdLong() + " used");
// pause thread as last resort
event.getJDA().shutdown();
System.exit(0);
}
// TODO add pre manager commands // TODO add pre manager commands
if (shouldReturn) { if (shouldReturn) {
return; return;
} }
if (!event.getAuthor().isBot()) { if (!event.getAuthor().isBot()) {
Boot.guildCommandManager.startCommand(event); Boot.guildCommandManager.startCommand(event);
} }
} }
} }

View file

@ -0,0 +1,122 @@
package pkg.deepCurse.nopalmo.manager;
public class Argument {
// README
//
// This tool is used simply for now, but it will be worth while if you get used to it
//
// this allows extra organization and ease of use throughout the creation of commands
// instead of one simple list where if args[1] == "all" do code
// this allows the same functionality, if not more with a little bit of learning
// you can go back to what it was before, but i honestly believe this system is worthwhile
// it just needs polish and reports on usage for optimization
//
//// TL;DR ITS A NEW FEATURE, GIVE IT TIME
private int requiredArgs = 0;
private String argName = null;
private Argument[] subArgs = null;
private boolean requiresPrefix = false;
public static final String argumentPrefix = "-"; // This exists for the sole reason of customization and will
// generally not change, ever, its recommended you keep it to
// something other than empty to help ensure that what the user
// entered is an arg and not something else
/**
* @implNote This is an experimental feature that has potential, but i have no
* use for it currently, imagine it akin to redstone, mojang had no
* idea people would build computers or 9x9 piston doors out of it
* @param requiredArgs
* @param argName
* @param subArgs
*/
public Argument(int requiredArgs, String argName, Argument[] subArgs) {
this.requiredArgs = requiredArgs;
this.argName = argName;
this.subArgs = subArgs;
}
/**
* @implNote This is an experimental feature that has potential, but i have no
* use for it currently, imagine it akin to redstone, mojang had no
* idea people would build computers or 9x9 piston doors out of it
* @param requiredArgs
* @param argName
* @param subArgs
*/
public Argument(int requiredArgs, String argName, Argument[] subArgs, RunnableArg runnableArg) {
this.requiredArgs = requiredArgs;
this.argName = argName;
this.subArgs = subArgs;
}
/**
* @implNote This is an experimental feature that has potential, but i have no
* use for it currently, imagine it akin to redstone, mojang had no
* idea people would build computers or 9x9 piston doors out of it
* @param requiredArgs
* @param argName
* @param subArgs
*/
public Argument(String argName) {
this.argName = argName;
}
/**
* @implNote This is an experimental feature that has potential, but i have no
* use for it currently, imagine it akin to redstone, mojang had no
* idea people would build computers or 9x9 piston doors out of it
* @param requiredArgs
* @param argName
* @param subArgs
*/
public Argument(String argName, RunnableArg runnableArg) {
this.argName = argName;
}
public int getRequiredArgs() {
return requiredArgs;
}
// public Argument setRequiredArgs(int requiredArgs) {
// this.requiredArgs = requiredArgs;
// return this;
// }
public String getArgName() {
return argName;
}
// public Argument setArgName(String argName) {
// this.argName = argName;
// return this;
// }
public Argument[] getSubArgs() {
return subArgs;
}
// public Argument setSubArgs(Argument[] subArgs) {
// this.subArgs = subArgs;
// return this;
// }
public Argument setPrefixRequirement(Boolean bool) {
this.requiresPrefix = bool;
return this;
}
public boolean getPrefixRequirement() {
return this.requiresPrefix;
}
public interface RunnableArg {
public void run();
}
}

View file

@ -0,0 +1,32 @@
package pkg.deepCurse.nopalmo.manager;
public class ArgumentList { // ON HOLD FOR NOW, IT MAY BE USELESS
//
// private HashMap<String, Argument> args = new HashMap<String, Argument>();
// private Command command = null;
//
// public ArgumentList(Command command) {
// this.command = command;
// }
//
// public void addArgument(String name) {
//
//
//
// args.put(null, null);
// }
//
// @Nullable
// public ArrayList<Argument> getArguments() {
// return (ArrayList<Argument>) args.values();
// }
//
// public Argument getArgumentFromName(String argumentName) {
// return args.get(argumentName);
// }
//
// @Nullable
// public List<String> getArgumentNames() {
// return (@Nullable List<String>) args.keySet();
// }
}

View file

@ -33,24 +33,28 @@ public abstract class CommandBlob {
return this.modifiedRaw; return this.modifiedRaw;
} }
@Deprecated
public ArrayList<String> getArgs() { public ArrayList<String> getArgs() {
return args; return args;
} }
public void setUser(long userID) { public CommandBlob setUser(long userID) {
this.userID = userID; this.userID = userID;
return this;
} }
public long getUserID() { public long getUserID() {
return this.userID; return this.userID;
} }
public void setJDA(JDA bot) { public CommandBlob setJDA(JDA bot) {
return this;
} }
public void setArgs(ArrayList<String> newArguments) { public CommandBlob setArgs(ArrayList<String> newArguments) {
this.args = newArguments; this.args = newArguments;
return this;
} }
} }

View file

@ -3,8 +3,9 @@ package pkg.deepCurse.nopalmo.manager;
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent; import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
public class GuildCommandBlob extends CommandBlob { public class GuildCommandBlob extends CommandBlob {
@SuppressWarnings("deprecation") String[][] argumentArray = null;
public GuildCommandBlob(GuildMessageReceivedEvent event) { public GuildCommandBlob(GuildMessageReceivedEvent event) {
super(event); super(event);
setUser(event.getMessage().getAuthor().getIdLong()); setUser(event.getMessage().getAuthor().getIdLong());

View file

@ -2,7 +2,6 @@ package pkg.deepCurse.nopalmo.manager;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.io.StringWriter; import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
@ -14,9 +13,11 @@ 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.GuildCommand; import pkg.deepCurse.nopalmo.command.GuildCommand;
import pkg.deepCurse.nopalmo.command.guildCommand.info.Help;
import pkg.deepCurse.nopalmo.command.guildCommand.info.Ping; import pkg.deepCurse.nopalmo.command.guildCommand.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;
import pkg.deepCurse.nopalmo.global.Tools;
public class GuildCommandManager { public class GuildCommandManager {
@ -29,6 +30,7 @@ public class GuildCommandManager {
} }
public void init() { public void init() {
// addCommand(new Help(this)); FIXME
addCommand(new Ping()); addCommand(new Ping());
} }
@ -67,14 +69,16 @@ public class GuildCommandManager {
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>();
// ArrayList<String[]> extractedFlags = new ArrayList<String[]>(); // not needed currently, remnant idea of bash-ish
GuildCommandBlob commandBlob = new GuildCommandBlob(guildMessage); GuildCommandBlob commandBlob = new GuildCommandBlob(guildMessage);
GuildCommand guildCommand = guildCommandMap.get(command);
HashMap<String, Argument> argumentList = new HashMap<String, Argument>();
boolean printTime = false; boolean printTime = false;
byte argSkipCount = 0; byte argSkipCount = 0;
boolean remainsValid = true;
for (String x : args) { for (String x : args) {
switch (x) { switch (x) {
@ -88,52 +92,83 @@ public class GuildCommandManager {
case "\\del": case "\\del":
guildMessage.getMessage().delete().queue(); guildMessage.getMessage().delete().queue();
break; break;
default: default: // in the rewrite process
if (argSkipCount<=0) { if (argSkipCount <= 0) {
newArguments.add(x);
if (guildCommand.getArguments() != null) {
if (x.startsWith(Argument.argumentPrefix)) {
String pre = x.substring(Argument.argumentPrefix.length());
argumentList.put(pre, guildCommand.getArguments().get(pre));
} else {
if (guildCommand.getArguments().get(x).getPrefixRequirement()) {
Tools.wrongUsage(guildMessage.getChannel(), guildCommand);
remainsValid = false;
} else {
argumentList.put(x, guildCommand.getArguments().get(x));
}
}
}
//
// if (guildCommand.getArguments() != null) {
//
// String newArg = x;
//
// if (!newArg.startsWith(Argument.argumentPrefix)) {
// if (guildCommand.getArguments().get(newArg)!=null) {
//
// }
// }
//
// if (guildCommand.getArguments().containsKey(newArg)) {
//
// argumentList.put(guildCommand.getArguments().get(newArg).getArgName(),
// guildCommand.getArguments().get(newArg));
// } else
//
// argumentList.put(newArg, new Argument(newArg));
// }
} }
} }
} }
commandBlob.setArgs(newArguments); if (remainsValid) {
guildCommand.runCommand(commandBlob, this, argumentList);
guildCommandMap.get(command).run(commandBlob, this); }
if (printTime) { if (printTime) {
guildMessage.getChannel() guildMessage.getChannel()
.sendMessage("Time to run: " + (commandStartTime - System.currentTimeMillis()) + "ms") .sendMessage("Time to run: " + (System.currentTimeMillis() - commandStartTime) + "ms")
.queue(); .queue();
} }
} catch (Exception e) { } catch (Exception e) {
if (Boot.isProd) { if (Boot.isProd) {
guildMessage.getChannel().sendMessage("```\n" + e + "```").queue(); guildMessage.getChannel().sendMessage("```yaml\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);
guildMessage.getChannel().sendMessage("```\n" + sw.toString() + "```").queue(); guildMessage.getChannel().sendMessage("```yaml\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) {
guildMessage.getChannel().sendMessage("```\n" + t + "```").queue(); guildMessage.getChannel().sendMessage("```apache\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);
guildMessage.getChannel().sendMessage("```\n" + sw.toString() + "```").queue(); guildMessage.getChannel().sendMessage("```apache\n" + sw.toString() + "```").queue();
System.err.println("Error caught in: " + t.toString()); System.err.println("Error caught in: " + t.toString());
t.printStackTrace(); t.printStackTrace();
} }
} }
}); });
} }
} }
} }

View file

@ -1,7 +1,5 @@
package pkg.deepCurse.simpleLoggingGarbage.core; package pkg.deepCurse.simpleLoggingGarbage.core;
import javax.security.auth.login.LoginException;
import pkg.deepCurse.nopalmo.core.Boot; import pkg.deepCurse.nopalmo.core.Boot;
/** /**

View file

@ -1,22 +0,0 @@
package services;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.Serializable;
public class CTimer extends Object implements Serializable {
private static final long serialVersionUID = 1861304556437295528L;
int delay = 1000; // milliseconds
int timerResetCount;
ActionListener taskPerformer = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.print("timer: " + timerResetCount);
timerResetCount += 1;
}
};
}

View file

@ -1,72 +0,0 @@
package services;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.List;
public class ConsoleOut {
private ByteArrayOutputStream baos;
private PrintStream previous;
private boolean capturing;
public void start() {
if (capturing) {
return;
}
capturing = true;
previous = System.out;
baos = new ByteArrayOutputStream();
OutputStream outputStreamCombiner = new OutputStreamCombiner(Arrays.asList(previous, baos));
PrintStream custom = new PrintStream(outputStreamCombiner);
System.setOut(custom);
}
public String stop() {
if (!capturing) {
return "";
}
System.setOut(previous);
String capturedValue = baos.toString();
baos = null;
previous = null;
capturing = false;
return capturedValue;
}
private static class OutputStreamCombiner extends OutputStream {
private List<OutputStream> outputStreams;
public OutputStreamCombiner(List<OutputStream> outputStreams) {
this.outputStreams = outputStreams;
}
public void write(int b) throws IOException {
for (OutputStream os : outputStreams) {
os.write(b);
}
}
public void flush() throws IOException {
for (OutputStream os : outputStreams) {
os.flush();
}
}
public void close() throws IOException {
for (OutputStream os : outputStreams) {
os.close();
}
}
}
}