Merge pull request #4 from lever1209/development

Development
This commit is contained in:
lever1209 2021-12-07 00:01:06 -04:00 committed by GitHub
commit cec5f0e97a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
22 changed files with 535 additions and 379 deletions

View file

@ -1,49 +0,0 @@
package pkg.deepCurse.nopalmo.command;
import java.util.HashMap;
import org.jetbrains.annotations.Nullable;
import pkg.deepCurse.nopalmo.manager.Argument;
public abstract class AbstractCommand { // TODO rewrite to implement type args?
public abstract String[] getCommandCalls();
public String getCommandName() {
return getCommandCalls()[0];
}
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() {
return null;
}
public String getUsage() {
return null;
}
public int getTimeout() {
return 0;
}
@Nullable
public HashMap<String, Argument> getArguments() {
return null;
}
}

View file

@ -0,0 +1,84 @@
package pkg.deepCurse.nopalmo.command;
import java.util.HashMap;
import org.jetbrains.annotations.Nullable;
import net.dv8tion.jda.api.Permission;
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.DirectCommandBlob;
import pkg.deepCurse.nopalmo.manager.GuildCommandBlob;
public interface CommandInterface { // TODO rewrite to implement type args?
public abstract String[] getCommandCalls();
public default String getCommandName() {
return getCommandCalls()[0];
}
public default boolean isNSFW() {
return false;
}
public default 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, Moderation, Fun, Info, Extra, TESTING, DEV, EGG
}
public String getHelp();
public default String getUsage() {
return Global.prefix + getCommandName();
}
public default int getTimeout() {
return 0;
}
@Nullable
public default HashMap<String, Argument> getArguments() {
return null;
}
public interface DualCommandInterface extends DirectCommandInterface, GuildCommandInterface {
@Override
public default void runGuildCommand(GuildCommandBlob blob, HashMap<String, Argument> argumentMap)
throws Exception {
runDualCommand(new CommandBlob(blob), argumentMap);
}
@Override
public default void runDirectCommand(DirectCommandBlob blob, HashMap<String, Argument> argumentMap)
throws Exception {
runDualCommand(new CommandBlob(blob), argumentMap);
}
public void runDualCommand(CommandBlob blob, HashMap<String, Argument> argumentMap) throws Exception;
}
public interface DirectCommandInterface extends CommandInterface {
public void runDirectCommand(DirectCommandBlob blob, HashMap<String, Argument> argumentList) throws Exception;
}
public interface GuildCommandInterface extends CommandInterface {
public void runGuildCommand(GuildCommandBlob blob, HashMap<String, Argument> argumentList) throws Exception;
public default Permission[] getRequiredPermissions() {
return null;
}
public default Permission getRequiredPermission() {
return null;
}
}
}

View file

@ -1,12 +0,0 @@
package pkg.deepCurse.nopalmo.command;
import java.util.HashMap;
import pkg.deepCurse.nopalmo.manager.Argument;
import pkg.deepCurse.nopalmo.manager.DirectCommandBlob;
public abstract class DirectCommand extends AbstractCommand {
public abstract void runCommand(DirectCommandBlob blob, HashMap<String, Argument> argumentList) throws Exception;
}

View file

@ -1,23 +0,0 @@
package pkg.deepCurse.nopalmo.command;
import java.util.HashMap;
import net.dv8tion.jda.api.Permission;
import pkg.deepCurse.nopalmo.manager.Argument;
import pkg.deepCurse.nopalmo.manager.GuildCommandBlob;
public abstract class GuildCommand extends AbstractCommand {
public abstract void runCommand(GuildCommandBlob blob, HashMap<String, Argument> argumentList) throws Exception;
public abstract String[] getCommandCalls();
public Permission[] getRequiredPermissions() {
return null;
}
public Permission getRequiredPermission() {
return null;
}
}

View file

@ -0,0 +1,58 @@
package pkg.deepCurse.nopalmo.command.commands.general;
import java.util.HashMap;
import pkg.deepCurse.nopalmo.command.CommandInterface.GuildCommandInterface;
import pkg.deepCurse.nopalmo.database.DatabaseTools.Tools.Global;
import pkg.deepCurse.nopalmo.database.DatabaseTools.Tools.Guild;
import pkg.deepCurse.nopalmo.manager.Argument;
import pkg.deepCurse.nopalmo.manager.GuildCommandBlob;
public class Prefix implements GuildCommandInterface {
@Override
public String[] getCommandCalls() {
return new String[] { "prefix", };
}
@Override
public HelpPage getHelpPage() {
return HelpPage.General;
}
@Override
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();
} else {
Guild.Prefix.setPrefix(
blob.getEvent().getGuild().getIdLong(), Global.prefix);
blob.getEvent().getChannel().sendMessage("Reset prefix to default").queue();
}
}
@Override
public HashMap<String, Argument> getArguments() {
HashMap<String, Argument> args = new HashMap<String, Argument>();
args.put("prefix", new Argument("prefix").setPosition(0).setIsWildcard(true));
return args;
}
@Override
public String getUsage() {
return Global.prefix + getCommandName() + " <prefix>";
}
@Override
public String getHelp() {
return "Sets a prefix for your guild";
}
}

View file

@ -0,0 +1,43 @@
package pkg.deepCurse.nopalmo.command.commands.info;
import java.util.HashMap;
import pkg.deepCurse.nopalmo.command.CommandInterface.DualCommandInterface;
import pkg.deepCurse.nopalmo.manager.Argument;
import pkg.deepCurse.nopalmo.manager.CommandBlob;
public class Git implements DualCommandInterface {
@Override
public void runDualCommand(CommandBlob blob, HashMap<String, Argument> argumentMap) throws Exception {
blob.getChannel().sendMessage("Heres the link: https://github.com/lever1209/nopalmo").queue();
}
@Override
public String[] getCommandCalls() {
return new String[] { "git", "source", "github" };
}
@Override
public HelpPage getHelpPage() {
return HelpPage.Info;
}
@Override
public HashMap<String, Argument> getArguments() {
HashMap<String, Argument> args = new HashMap<String, Argument>();
args.put("test", new Argument("test", (CommandBlob blob) -> {
blob.getChannel().sendMessage("This is the automatically running argument inside of " + this.getCommandName()).queue();
}).setPrefixRequirement(true).setAutoStartRunnable(true));
return args;
}
@Override
public String getHelp() {
return "Posts my github link";
}
}

View file

@ -1,4 +1,4 @@
package pkg.deepCurse.nopalmo.command.guildCommand.info;
package pkg.deepCurse.nopalmo.command.commands.info;
import java.time.Instant;
import java.util.HashMap;
@ -6,13 +6,13 @@ import java.util.Random;
import java.util.concurrent.TimeUnit;
import net.dv8tion.jda.api.EmbedBuilder;
import pkg.deepCurse.nopalmo.command.GuildCommand;
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.GuildCommandBlob;
import pkg.deepCurse.nopalmo.manager.GuildCommandManager;
public class Help extends GuildCommand {
public class Help implements GuildCommandInterface {
public final GuildCommandManager manager;
@ -21,14 +21,14 @@ public class Help extends GuildCommand {
}
@Override
public void runCommand(GuildCommandBlob blob, HashMap<String, Argument> argumentMap) throws Exception {
public void runGuildCommand(GuildCommandBlob blob, HashMap<String, Argument> argumentMap) throws Exception {
if (argumentMap.isEmpty()) {
EmbedBuilder embed = new EmbedBuilder().setTitle("Commands:");
HashMap<HelpPage, String> commandHash = new HashMap<HelpPage, String>();
for (GuildCommand command : manager.getGuildCommands()) {
for (GuildCommandInterface command : manager.getGuildCommands()) {
commandHash.put(command.getHelpPage(),
commandHash.get(command.getHelpPage()) + command.getCommandName());
@ -37,12 +37,12 @@ public class Help extends GuildCommand {
StringBuilder sB = new StringBuilder();
GuildCommand ping = blob.getCommandManager().getCommand("ping");
GuildCommandInterface ping = blob.getCommandManager().getCommand("ping");
if (ping != null) {
sB.append("`" + ping.getUsage() + "`\n");
}
GuildCommand help = blob.getCommandManager().getCommand("help");
GuildCommandInterface help = blob.getCommandManager().getCommand("help");
if (help != null) {
sB.append("`" + help.getUsage() + "`\n");
}
@ -86,7 +86,7 @@ public class Help extends GuildCommand {
// ##########################################################################################################################
try {
GuildCommand command = manager.getCommand(String.join("", blob.getArgs()));
GuildCommandInterface command = manager.getCommand(String.join("", blob.getArgs()));
// event.getChannel().sendMessage("Command help for `" + command.commandName() +
// "`:\n\tUsage: "+ command.usageString() + "\n" +

View file

@ -1,32 +1,32 @@
package pkg.deepCurse.nopalmo.command.guildCommand.info;
package pkg.deepCurse.nopalmo.command.commands.info;
import java.util.HashMap;
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
import pkg.deepCurse.nopalmo.command.GuildCommand;
import net.dv8tion.jda.api.entities.MessageChannel;
import pkg.deepCurse.nopalmo.command.CommandInterface.DualCommandInterface;
import pkg.deepCurse.nopalmo.database.DatabaseTools.Tools.Global;
import pkg.deepCurse.nopalmo.manager.Argument;
import pkg.deepCurse.nopalmo.manager.GuildCommandBlob;
import pkg.deepCurse.nopalmo.manager.CommandBlob;
import pkg.deepCurse.nopalmo.utils.UptimePing;
public class Ping extends GuildCommand {
public class Ping implements DualCommandInterface {
@Override
public void runCommand(GuildCommandBlob blob, HashMap<String, Argument> argumentMap) throws Exception {
GuildMessageReceivedEvent event = blob.getEvent();
public void runDualCommand(CommandBlob blob, HashMap<String, Argument> argumentMap) throws Exception {
MessageChannel channel = blob.getChannel();
if (argumentMap.isEmpty()) {
event.getChannel().sendMessage("Pong!\n" + event.getJDA().getGatewayPing() + "ms\n").queue();
channel.sendMessage("Pong!\n" + blob.getJDA().getGatewayPing() + "ms\n").queue();
return;
}
if (argumentMap.get("all") != null) {
event.getChannel().sendMessage("Gathering data. . .").queue(msg -> {
channel.sendMessage("Gathering data. . .").queue(msg -> {
long timeToProcess = System.currentTimeMillis();
long jdaPing = event.getJDA().getGatewayPing();
long jdaPing = blob.getJDA().getGatewayPing();
long googlePing = -1;
try {
googlePing = UptimePing.sendPing("www.google.com");
@ -50,21 +50,6 @@ public class Ping extends GuildCommand {
.queue();
});
}
// if (argumentArray == null || argumentArray.isEmpty()) {
//
// return;
// } else {
//
// for (Argument i : argumentArray) {
// if (i.getArgName().contentEquals("all")) {
//
// } else {
// Tools.wrongUsage(event.getChannel(), this);
// }
// }
// return;
// }
}
@Override
@ -74,7 +59,7 @@ public class Ping extends GuildCommand {
@Override
public String getUsage() {
return Global.prefix + "ping [" + Argument.argumentPrefix + "all]";
return Global.prefix + getCommandName()+" [" + Argument.argumentPrefix + "all]";
}
@Override
@ -91,4 +76,9 @@ public class Ping extends GuildCommand {
return args;
}
@Override
public String getHelp() {
return "Returns the jda heartbeat ping";
}
}

View file

@ -1,84 +0,0 @@
package pkg.deepCurse.nopalmo.command.directCommand.info;
import java.util.HashMap;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import pkg.deepCurse.nopalmo.command.DirectCommand;
import pkg.deepCurse.nopalmo.database.DatabaseTools.Tools.Global;
import pkg.deepCurse.nopalmo.manager.Argument;
import pkg.deepCurse.nopalmo.manager.DirectCommandBlob;
import pkg.deepCurse.nopalmo.utils.UptimePing;
public class DirectMessagePing extends DirectCommand {
@Override
public void runCommand(DirectCommandBlob blob,
HashMap<String, Argument> argumentMap) throws Exception {
MessageReceivedEvent event = blob.getEvent();
if (argumentMap.isEmpty()) {
event.getChannel().sendMessage("Pong!\n" + event.getJDA().getGatewayPing() + "ms\n").queue();
return;
}
if (argumentMap.get("all") != null) {
event.getChannel().sendMessage("Gathering data. . .").queue(msg -> {
long timeToProcess = System.currentTimeMillis();
try { // TODO rewrite this block, all tries are not good practice
String out = "Pong!\n" + "Google: " + UptimePing.sendPing("www.google.com") + "ms\n"
+ "JDA Gateway: " + event.getJDA().getGatewayPing() + "ms\n" + "www.discord.com: "
+ UptimePing.sendPing("www.discord.com") + "ms";
msg.editMessage(out + "\nTime to process: " + (System.currentTimeMillis() - timeToProcess) + "ms")
.queue();
} catch (Exception e) {
e.printStackTrace();
}
});
}
// if (argumentArray == null || argumentArray.isEmpty()) {
//
// return;
// } else {
//
// for (Argument i : argumentArray) {
// if (i.getArgName().contentEquals("all")) {
//
// } else {
// Tools.wrongUsage(event.getChannel(), this);
// }
// }
// return;
// }
}
@Override
public String[] getCommandCalls() {
return new String[] { "ping" };
}
@Override
public String getUsage() {
return Global.prefix + "ping [" + Argument.argumentPrefix + "all]";
}
@Override
public HelpPage getHelpPage() {
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

@ -1,28 +0,0 @@
package pkg.deepCurse.nopalmo.command.guildCommand.info;
import java.util.HashMap;
import pkg.deepCurse.nopalmo.command.GuildCommand;
import pkg.deepCurse.nopalmo.manager.Argument;
import pkg.deepCurse.nopalmo.manager.GuildCommandBlob;
public class Git extends GuildCommand {
@Override
public void runCommand(GuildCommandBlob blob, HashMap<String, Argument> argumentList) throws Exception {
blob.getEvent().getChannel().sendMessage("Heres the link: https://github.com/lever1209/nopalmo").queue();
}
@Override
public String[] getCommandCalls() {
// TODO Auto-generated method stub
return new String[] { "git", "source", "github" };
}
@Override
public HelpPage getHelpPage() {
// TODO Auto-generated method stub
return HelpPage.Info;
}
}

View file

@ -92,7 +92,7 @@ public class DatabaseTools {
if (prefix == null || prefix.isEmpty()) {
// throw new IllegalArgumentException("Input cannot be empty");
prefix = ";";
prefix = Global.prefix;
}
PreparedStatement pstmt = null;
@ -114,7 +114,7 @@ public class DatabaseTools {
SQLCode.sqlTranslate(pstmt, e);
for (int i : new int[] { 1062 }) {
if (i == e.getErrorCode()) {
return setPrefix(connection, guildID, prefix);
return setPrefix(guildID, prefix);
}
}
try {
@ -126,7 +126,7 @@ public class DatabaseTools {
}
}
public static String setPrefix(Connection conn, long guildID, String prefix)
public static String setPrefix(long guildID, String prefix)
throws IllegalArgumentException {
if (prefix.isEmpty()) {
@ -137,7 +137,7 @@ public class DatabaseTools {
String query = "update guilds set prefix = ? where guildid = ?;";
try {
pstmt = conn.prepareStatement(query);
pstmt = connection.prepareStatement(query);
pstmt.setLong(2, guildID);
pstmt.setString(1, prefix);
@ -146,12 +146,12 @@ public class DatabaseTools {
int[] updateCounts = pstmt.executeBatch();
checkUpdateCounts(pstmt, updateCounts);
pstmt.close();
conn.commit();
// conn.commit();
return prefix;
} catch (SQLException e) {
SQLCode.sqlTranslate(pstmt, e);
try {
conn.rollback();
connection.rollback();
} catch (Exception e2) {
e.printStackTrace();
}

View file

@ -8,6 +8,8 @@ public class Reactions {
public static void init() {
insert("galaxyThumb", 801657838358495232L);
insert("kirbo_wadafuq", 799633705068003338L);
}
public static void insert(String input, long id) {

View file

@ -1,18 +1,16 @@
package pkg.deepCurse.nopalmo.global;
import net.dv8tion.jda.api.entities.MessageChannel;
import net.dv8tion.jda.api.entities.TextChannel;
import pkg.deepCurse.nopalmo.command.DirectCommand;
import pkg.deepCurse.nopalmo.command.GuildCommand;
import pkg.deepCurse.nopalmo.command.CommandInterface;
public class Tools {
public static void wrongUsage(TextChannel tc, GuildCommand c) {
tc.sendMessage("Wrong Command Usage!\n" + c.getUsage()).queue();
public static void wrongUsage(MessageChannel messageChannel, CommandInterface command) {
messageChannel.sendMessage("Wrong Command Usage!\n" + command.getUsage()).queue();
}
public static void wrongUsage(MessageChannel tc, DirectCommand c) {
tc.sendMessage("Wrong Command Usage!\n" + c.getUsage()).queue();
}
// public static void wrongUsage(MessageChannel tc, DirectCommandInterface c) {
// tc.sendMessage("Wrong Command Usage!\n" + c.getUsage()).queue();
// }
}

View file

@ -21,16 +21,12 @@ public class DirectMessageReceivedListener extends ListenerAdapter {
public void onMessageReceived(@Nonnull MessageReceivedEvent event) {
Message message = event.getMessage();
String messageRaw = message.getContentRaw();
System.out.println(messageRaw + "\n<@!" + event.getJDA().getSelfUser().getIdLong() + ">");
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
// message.addReaction(Reactions.getReaction("galaxyThumb")).complete();
// TODO re enable
event.getJDA().shutdown();
System.exit(0);
@ -43,7 +39,6 @@ public class DirectMessageReceivedListener extends ListenerAdapter {
for (String i : prefixArray) { // TODO switch to [] to skip for loop?
if (messageRaw.startsWith(i)) {
// System.out.println("breaking");
shouldReturn = false;
}
}
@ -51,7 +46,6 @@ public class DirectMessageReceivedListener extends ListenerAdapter {
// TODO add pre manager commands
if (shouldReturn) {
return;
}

View file

@ -27,41 +27,17 @@ public class GuildMessageReceivedListener extends ListenerAdapter {
if (messageRaw.contentEquals(Global.prefix + Global.prefix)
&& DatabaseTools.Tools.Developers.canPowerOffBot(event.getAuthor().getIdLong())) {
// message.addReaction(Reactions.getReaction("galaxyThumb")).complete(); TODO re
// enable
// 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()),
"<@!" + event.getJDA().getSelfUser().getIdLong() + ">" }; // FIXME BROKEN PING PREFIX
boolean shouldReturn = true;
for (String i : prefixArray) { // TODO switch to [] to skip for loop?
if (messageRaw.startsWith(i)) {
shouldReturn = false;
break;
}
}
// TODO add pre manager commands
if (shouldReturn) {
return;
}
if (!event.getAuthor().isBot()) {
Boot.guildCommandManager.startCommand(event);
}
}
}

View file

@ -1,5 +1,7 @@
package pkg.deepCurse.nopalmo.manager;
import pkg.deepCurse.nopalmo.command.CommandInterface;
public class Argument {
// README
@ -21,6 +23,13 @@ public class Argument {
private String argName = null;
private Argument[] subArgs = null;
private boolean requiresPrefix = false;
private Boolean isWildcard;
private int position = -1;
private String wildCardString = null;
private boolean autoStartRunnable = false;
private boolean skipOriginalTaskOnRunnable = false;
private RunnableArg runnableArg = null;
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
@ -52,6 +61,7 @@ public class Argument {
this.requiredArgs = requiredArgs;
this.argName = argName;
this.subArgs = subArgs;
this.runnableArg = runnableArg;
}
/**
@ -76,6 +86,7 @@ public class Argument {
*/
public Argument(String argName, RunnableArg runnableArg) {
this.argName = argName;
this.runnableArg = runnableArg;
}
public int getRequiredArgs() {
@ -109,15 +120,69 @@ public class Argument {
this.requiresPrefix = bool;
return this;
}
public Argument setIsWildcard(Boolean bool) {
if (this.position<=-1) {
throw new IllegalArgumentException("Cannot create a wildcard without a position; set a position first");
}
this.isWildcard = bool;
return this;
}
public Boolean getIsWildcard() {
return isWildcard;
}
public boolean getPrefixRequirement() {
return this.requiresPrefix;
}
public int getPosition() {
return position;
}
public Argument setPosition(int position) {
this.position = position;
return this;
}
public interface RunnableArg {
public void run();
public void run(CommandBlob blob);
}
public Argument setWildCardString(String wildCardString) {
this.wildCardString = wildCardString;
return this;
}
public String getWildCardString() {
return wildCardString;
}
public Argument setAutoStartRunnable(boolean bool) {
this.autoStartRunnable = bool;
return this;
}
public boolean isAutoStartRunnable() {
return autoStartRunnable;
}
public boolean isSkipOriginalTaskOnRunnable() {
return skipOriginalTaskOnRunnable;
}
public Argument setSkipOriginalTaskOnRunnable(boolean skipOriginalTaskOnRunnable) {
this.skipOriginalTaskOnRunnable = skipOriginalTaskOnRunnable;
return this;
}
public RunnableArg getRunnableArg() {
return runnableArg;
}
}

View file

@ -0,0 +1,119 @@
package pkg.deepCurse.nopalmo.manager;
import java.util.ArrayList;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.MessageChannel;
import net.dv8tion.jda.api.entities.PrivateChannel;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.events.Event;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
public class CommandBlob {
// CONTAINER/TRANSLATOR CLASS FOR COMMAND BLOBS
private Event event = null;
private CommandManager commandManager = null;
private ArrayList<String> args = null;
private JDA bot = null;
private long userID = 0;
private long channelID = 0;
public CommandBlob(GuildMessageReceivedEvent event) {
this.event = event;
this.bot = event.getJDA();
}
public CommandBlob(MessageReceivedEvent event) {
this.event = event;
this.bot = event.getJDA();
}
public CommandBlob(GuildCommandBlob blob) {
this.args = blob.getArgs();
this.channelID = blob.getChannelID();
this.commandManager = blob.getCommandManager();
this.event = blob.getEvent();
this.userID = blob.getUserID();
this.bot = blob.getEvent().getJDA();
}
public CommandBlob(DirectCommandBlob blob) {
this.args = blob.getArgs();
this.channelID = blob.getChannelID();
this.commandManager = blob.getCommandManager();
this.event = blob.getEvent();
this.userID = blob.getUserID();
this.bot = blob.getEvent().getJDA();
}
public long getChannelID() {
return channelID;
}
public CommandBlob setChannelID(long channelID) {
this.channelID = channelID;
return this;
}
public MessageChannel getChannel() {
TextChannel textChannel = bot.getTextChannelById(channelID);
if (textChannel != null){
return textChannel;
}
PrivateChannel privateChannel = bot.getPrivateChannelById(channelID);
if (privateChannel != null){
return privateChannel;
}
return null;
}
public MessageChannel getMessageChannel(long channelID) {
return bot.getTextChannelById(channelID);
}
public long getUserID() {
return userID;
}
public JDA getJDA() {
return bot;
}
public CommandBlob setUserID(long userID) {
this.userID = userID;
return this;
}
public ArrayList<String> getArgs() {
return args;
}
public CommandBlob setArgs(ArrayList<String> args) {
this.args = args;
return this;
}
public CommandManager getCommandManager() {
return commandManager;
}
public CommandBlob setCommandManager(CommandManager commandManager) {
this.commandManager = commandManager;
return this;
}
public Event getEvent() {
if (event instanceof GuildMessageReceivedEvent) {
return (GuildMessageReceivedEvent) event;
} else if (event instanceof MessageReceivedEvent) {
return (MessageReceivedEvent) event;
} else
return null;
}
}

View file

@ -0,0 +1,5 @@
package pkg.deepCurse.nopalmo.manager;
public abstract class CommandManager {
}

View file

@ -7,13 +7,12 @@ 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 long userID = 0;
private long channelID = 0;
private MessageReceivedEvent event = null;
public DirectCommandBlob(MessageReceivedEvent event) {
setUserID(event.getAuthor().getIdLong());
setChannelID(event.getChannel().getIdLong());this.event = event;

View file

@ -12,15 +12,15 @@ import java.util.concurrent.Executors;
import java.util.regex.Pattern;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import pkg.deepCurse.nopalmo.command.DirectCommand;
import pkg.deepCurse.nopalmo.command.directCommand.info.DirectMessagePing;
import pkg.deepCurse.nopalmo.command.CommandInterface.DirectCommandInterface;
import pkg.deepCurse.nopalmo.command.commands.info.Ping;
import pkg.deepCurse.nopalmo.core.Boot;
import pkg.deepCurse.nopalmo.database.DatabaseTools;
import pkg.deepCurse.nopalmo.global.Tools;
public class DirectCommandManager {
public class DirectCommandManager extends CommandManager {
private final Map<String, DirectCommand> directCommandMap = new HashMap<>();
private final Map<String, DirectCommandInterface> directCommandMap = new HashMap<>();
private static Executor executor = null;
public DirectCommandManager() {
@ -29,12 +29,11 @@ public class DirectCommandManager {
}
public void init() {
addCommand(new DirectMessagePing());
addCommand(new Ping());
}
private void addCommand(DirectCommand c) {
private void addCommand(DirectCommandInterface c) {
if (!directCommandMap.containsKey(c.getCommandName())) {
directCommandMap.put(c.getCommandName(), c);
} else {
@ -43,11 +42,11 @@ public class DirectCommandManager {
}
}
public Collection<DirectCommand> getDirectCommands() {
public Collection<DirectCommandInterface> getDirectCommands() {
return directCommandMap.values();
}
public DirectCommand getCommand(String commandName) {
public DirectCommandInterface getCommand(String commandName) {
if (commandName != null) {
return directCommandMap.get(commandName);
}
@ -69,11 +68,8 @@ public class DirectCommandManager {
long commandStartTime = System.currentTimeMillis();
try {
// ArrayList<String> newArguments = new ArrayList<String>();
// ArrayList<String> commandFlags = new ArrayList<String>();
DirectCommandBlob commandBlob = new DirectCommandBlob(messageReceivedEvent);
DirectCommand directCommand = directCommandMap.get(command);
DirectCommandInterface directCommand = directCommandMap.get(command);
HashMap<String, Argument> argumentList = new HashMap<String, Argument>();
boolean printTime = false;
@ -116,25 +112,6 @@ public class DirectCommandManager {
}
}
}
//
// 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));
// }
}
}
}
@ -142,7 +119,7 @@ public class DirectCommandManager {
commandBlob.setCommandManager(this);
if (remainsValid) {
directCommand.runCommand(commandBlob, argumentList);
directCommand.runDirectCommand(commandBlob, argumentList);
}
if (printTime) {

View file

@ -12,17 +12,18 @@ import java.util.concurrent.Executors;
import java.util.regex.Pattern;
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
import pkg.deepCurse.nopalmo.command.GuildCommand;
import pkg.deepCurse.nopalmo.command.guildCommand.info.Git;
import pkg.deepCurse.nopalmo.command.guildCommand.info.Help;
import pkg.deepCurse.nopalmo.command.guildCommand.info.Ping;
import pkg.deepCurse.nopalmo.command.CommandInterface.GuildCommandInterface;
import pkg.deepCurse.nopalmo.command.commands.general.Prefix;
import pkg.deepCurse.nopalmo.command.commands.info.Git;
import pkg.deepCurse.nopalmo.command.commands.info.Help;
import pkg.deepCurse.nopalmo.command.commands.info.Ping;
import pkg.deepCurse.nopalmo.core.Boot;
import pkg.deepCurse.nopalmo.database.DatabaseTools;
import pkg.deepCurse.nopalmo.global.Tools;
public class GuildCommandManager {
public class GuildCommandManager extends CommandManager {
private final Map<String, GuildCommand> guildCommandMap = new HashMap<>();
private final Map<String, GuildCommandInterface> guildCommandMap = new HashMap<>();
private static Executor executor = null;
public GuildCommandManager() {
@ -34,9 +35,10 @@ public class GuildCommandManager {
addCommand(new Help(this));
addCommand(new Ping());
addCommand(new Git());
addCommand(new Prefix());
}
private void addCommand(GuildCommand c) {
private void addCommand(GuildCommandInterface c) {
if (!guildCommandMap.containsKey(c.getCommandName())) {
guildCommandMap.put(c.getCommandName(), c);
} else {
@ -45,23 +47,42 @@ public class GuildCommandManager {
}
}
public Collection<GuildCommand> getGuildCommands() {
public Collection<GuildCommandInterface> getGuildCommands() {
return guildCommandMap.values();
}
public GuildCommand getCommand(String commandName) {
public GuildCommandInterface getCommand(String commandName) {
if (commandName != null) {
return guildCommandMap.get(commandName);
}
return null;
}
public void startCommand(GuildMessageReceivedEvent guildMessage) {
public void startCommand(GuildMessageReceivedEvent guildMessageEvent) { // TODO SPLIT UP AND INHERIT
final String message = guildMessage.getMessage().getContentRaw();
final String message = guildMessageEvent.getMessage().getContentRaw();
String prefix = DatabaseTools.Tools.Guild.Prefix.getPrefix(guildMessageEvent.getGuild().getIdLong());
String pingPrefix = "<@!" + guildMessageEvent.getJDA().getSelfUser().getIdLong() + ">";
final String[] split = message.replaceFirst("(?i)" + Pattern.quote(DatabaseTools.Tools.Global.prefix), "")
.split("\\s+");
// 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 + " ";
} 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)) {
@ -74,105 +95,128 @@ public class GuildCommandManager {
// ArrayList<String> newArguments = new ArrayList<String>();
// ArrayList<String> commandFlags = new ArrayList<String>();
GuildCommandBlob commandBlob = new GuildCommandBlob(guildMessage);
GuildCommand guildCommand = guildCommandMap.get(command);
GuildCommandBlob commandBlob = new GuildCommandBlob(guildMessageEvent);
GuildCommandInterface guildCommand = guildCommandMap.get(command);
HashMap<String, Argument> argumentList = new HashMap<String, Argument>();
boolean printTime = false;
byte argSkipCount = 0;
boolean remainsValid = true;
// int id = 0;
for (String x : args) {
HashMap<Integer, Argument> positionalArgs = new HashMap<Integer, Argument>();
for (Argument i : guildCommand.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":
guildMessage.getMessage().delete().queue();
guildMessageEvent.getMessage().delete().queue();
break;
default: // in the rewrite process
default:
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)) {
argumentList.put(pre, guildCommand.getArguments().get(pre));
if (positionalArgs.get(i) == null) {
if (x.startsWith(Argument.argumentPrefix)) {
String pre = x.substring(Argument.argumentPrefix.length());
if (guildCommand.getArguments().keySet().contains(pre)) {
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.wrongUsage(guildMessageEvent.getChannel(), guildCommand);
remainsValid = false;
}
} else {
Tools.wrongUsage(guildMessage.getChannel(), guildCommand);
remainsValid = false;
if (guildCommand.getArguments().get(x) != null) {
if (guildCommand.getArguments().get(x).getPrefixRequirement()) {
Tools.wrongUsage(guildMessageEvent.getChannel(), guildCommand);
remainsValid = false;
} else {
argumentList.put(x, guildCommand.getArguments().get(x));
if (guildCommand.getArguments().get(x).isAutoStartRunnable()
&& guildCommand.getArguments().get(x)
.getRunnableArg() != null) {
guildCommand.getArguments().get(x).getRunnableArg()
.run(new CommandBlob(commandBlob));
}
}
} else {
Tools.wrongUsage(guildMessageEvent.getChannel(), guildCommand);
remainsValid = false;
}
}
} else {
if (guildCommand.getArguments().get(x).getPrefixRequirement()) {
Tools.wrongUsage(guildMessage.getChannel(), guildCommand);
remainsValid = false;
} else {
argumentList.put(x, guildCommand.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));
}
}
}
//
// 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.setCommandManager(this);
if (remainsValid) {
guildCommand.runCommand(commandBlob, argumentList);
guildCommand.runGuildCommand(commandBlob, argumentList);
}
if (printTime) {
guildMessage.getChannel()
guildMessageEvent.getChannel()
.sendMessage("Time to run: " + (System.currentTimeMillis() - commandStartTime) + "ms")
.queue();
}
} catch (Exception e) {
if (Boot.isProd) {
guildMessage.getChannel().sendMessage("```properties\n" + e + "```").queue();
guildMessageEvent.getChannel().sendMessage("```properties\n" + e + "```").queue();
} else {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
guildMessage.getChannel().sendMessage("```properties\n" + sw.toString() + "```").queue();
guildMessageEvent.getChannel().sendMessage("```properties\n" + sw.toString() + "```").queue();
System.err.println("Exception caught in: " + e.toString());
e.printStackTrace();
}
} catch (Throwable t) {
if (Boot.isProd) {
guildMessage.getChannel().sendMessage("```mathematica\n" + t + "```").queue();
guildMessageEvent.getChannel().sendMessage("```mathematica\n" + t + "```").queue();
} else {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
t.printStackTrace(pw);
guildMessage.getChannel().sendMessage("```mathematica\n" + sw.toString() + "```").queue();
guildMessageEvent.getChannel().sendMessage("```mathematica\n" + sw.toString() + "```").queue();
System.err.println("Error caught in: " + t.toString());
t.printStackTrace();
}

View file

@ -1,9 +1,7 @@
package pkg.deepCurse.nopalmo.utils;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.nio.channels.SocketChannel;
import java.util.Date;