more shit en masse
This commit is contained in:
parent
172f86193d
commit
5bedad9a0f
9 changed files with 161 additions and 74 deletions
|
@ -1,14 +1,13 @@
|
|||
package pkg.deepCurse.nopalmo.command;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import net.dv8tion.jda.api.Permission;
|
||||
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
|
||||
import pkg.deepCurse.nopalmo.manager.CommandBlob;
|
||||
import pkg.deepCurse.nopalmo.manager.GuildCommandManager;
|
||||
|
||||
public abstract class GuildCommand {
|
||||
|
||||
public abstract void run(List<String> args, GuildMessageReceivedEvent guildMessage, GuildCommandManager commandManager) throws Exception;
|
||||
public abstract void run(CommandBlob blob, GuildMessageReceivedEvent guildMessage, GuildCommandManager commandManager) throws Exception;
|
||||
|
||||
public abstract String[] getCommandCalls();
|
||||
|
||||
|
|
|
@ -1,19 +1,18 @@
|
|||
package pkg.deepCurse.nopalmo.command.guildCommand;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
|
||||
import pkg.deepCurse.nopalmo.command.GuildCommand;
|
||||
import pkg.deepCurse.nopalmo.database.DatabaseTools;
|
||||
import pkg.deepCurse.nopalmo.manager.CommandBlob;
|
||||
import pkg.deepCurse.nopalmo.manager.GuildCommandManager;
|
||||
|
||||
public class Ping extends GuildCommand {
|
||||
|
||||
@Override
|
||||
public void run(List<String> args, GuildMessageReceivedEvent guildMessage, GuildCommandManager commandManager)
|
||||
public void run(CommandBlob blob, GuildMessageReceivedEvent guildMessage, GuildCommandManager commandManager)
|
||||
throws Exception {
|
||||
|
||||
DatabaseTools.Tools.Guild.Prefix.createPrefix(guildMessage.getGuild().getIdLong(), args.get(0));
|
||||
DatabaseTools.Tools.Guild.Prefix.createPrefix(guildMessage.getGuild().getIdLong(), blob.getArgs().get(0));
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
package pkg.deepCurse.nopalmo.core;
|
||||
|
||||
import java.sql.SQLException;
|
||||
|
||||
import javax.security.auth.login.LoginException;
|
||||
|
||||
import net.dv8tion.jda.api.JDA;
|
||||
|
@ -8,18 +10,43 @@ import net.dv8tion.jda.api.entities.Activity;
|
|||
import net.dv8tion.jda.api.requests.GatewayIntent;
|
||||
import net.dv8tion.jda.api.utils.ChunkingFilter;
|
||||
import net.dv8tion.jda.api.utils.MemberCachePolicy;
|
||||
import pkg.deepCurse.nopalmo.database.DatabaseTools;
|
||||
import pkg.deepCurse.nopalmo.global.Reactions;
|
||||
import pkg.deepCurse.nopalmo.listener.GuildMessageReceivedListener;
|
||||
import pkg.deepCurse.nopalmo.manager.GuildCommandManager;
|
||||
import pkg.deepCurse.simpleLoggingGarbage.core.Log;
|
||||
|
||||
public class Boot {
|
||||
|
||||
public static JDA bot;
|
||||
public static DatabaseTools databaseTools = null;
|
||||
public static GuildCommandManager guildCommandManager = new GuildCommandManager(); // move to master manager
|
||||
public static boolean isProd = false;
|
||||
|
||||
public static void main(String[] args) {
|
||||
Log.boot("Booting. . .");
|
||||
|
||||
long preBootTime = System.currentTimeMillis();
|
||||
|
||||
|
||||
isProd = args[2].contentEquals("prod");
|
||||
|
||||
Log.boot("Connecting to mariadb:nopalmo");
|
||||
try {
|
||||
databaseTools = new DatabaseTools(args[1]);
|
||||
Log.boot("Connected. . .");
|
||||
} catch (SQLException e1) {
|
||||
e1.printStackTrace();
|
||||
Log.boot("Failed to connect. . .\nShutting down. . .");
|
||||
System.exit(4);
|
||||
}
|
||||
|
||||
Log.boot("Init reaction/emote list");
|
||||
Reactions.init();
|
||||
Log.boot("Initialized reaction/emote list. . .");
|
||||
Log.boot("Init commands list");
|
||||
guildCommandManager.init();
|
||||
Log.boot("Initialized commands list. . .");
|
||||
|
||||
try {
|
||||
bot = JDABuilder.createDefault(args[0]).setChunkingFilter(ChunkingFilter.NONE)
|
||||
.setMemberCachePolicy(MemberCachePolicy.NONE).enableIntents(GatewayIntent.GUILD_MEMBERS)
|
||||
|
@ -32,7 +59,8 @@ public class Boot {
|
|||
}
|
||||
|
||||
long bootTime = System.currentTimeMillis() - preBootTime;
|
||||
|
||||
|
||||
System.out.println("Taken "+bootTime+"ms to boot");
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -11,20 +11,26 @@ import javax.annotation.Nonnull;
|
|||
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import pkg.deepCurse.nopalmo.core.Boot;
|
||||
import pkg.deepCurse.nopalmo.database.DatabaseTools.Tools.Global;
|
||||
import pkg.deepCurse.simpleLoggingGarbage.core.Log;
|
||||
|
||||
public class DatabaseTools {
|
||||
|
||||
private static Connection connection;
|
||||
private static Connection connection = null;
|
||||
|
||||
public DatabaseTools(String password) {
|
||||
public DatabaseTools(String password) throws SQLException {
|
||||
connection = createConnection(password);
|
||||
Global.updatePrefix();
|
||||
}
|
||||
|
||||
public static Connection createConnection(String password) {
|
||||
public static Connection createConnection(String password) throws SQLException {
|
||||
|
||||
String dbName = Boot.isProd ? "nopalmo" : "chaos";
|
||||
|
||||
String driver = "com.mysql.cj.jdbc.Driver";
|
||||
String url = "jdbc:mysql://localhost/nopalmo";
|
||||
String username = "u1d";
|
||||
String url = "jdbc:mysql://localhost/" + dbName;
|
||||
String username = "nopalmo";
|
||||
try {
|
||||
Class.forName(driver);
|
||||
} catch (ClassNotFoundException e) {
|
||||
|
@ -35,14 +41,17 @@ public class DatabaseTools {
|
|||
return DriverManager.getConnection(url, username, password);
|
||||
} catch (SQLException e) {
|
||||
sqlTranslate("Generate connection", e);
|
||||
throw new SQLException(e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static void sqlTranslate(String action, int errorCode) {
|
||||
switch (errorCode) {
|
||||
case 1062:
|
||||
System.err.println("Failed to execute; errorCode=1062; Duplicate ID; On action " + action);
|
||||
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);
|
||||
|
@ -78,6 +87,8 @@ public class DatabaseTools {
|
|||
|
||||
public class Tools {
|
||||
|
||||
// these sub classes will represent tables and the methods therein will be for actions within said table
|
||||
|
||||
public class Guild {
|
||||
|
||||
public class Prefix {
|
||||
|
@ -96,32 +107,19 @@ public class DatabaseTools {
|
|||
// throw new SQLException(null, null, 33); // we need a real catcher here
|
||||
System.err.println(
|
||||
"Failed to execute; errorCode=NO_ROW_FOUND; No row found; On action " + query);
|
||||
return null;
|
||||
return createPrefix(guildID, Global.prefix);
|
||||
}
|
||||
|
||||
} catch (SQLException e) {
|
||||
sqlTranslate(query, e.getErrorCode());
|
||||
// System.out.println("eeeeee");
|
||||
return null;
|
||||
} finally {
|
||||
try {
|
||||
if (rs != null)
|
||||
rs.close();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
|
||||
try {
|
||||
if (st != null)
|
||||
st.close();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
|
||||
// try { if (conn != null) conn.close(); } catch (Exception e) {};
|
||||
} finally { // @formatter:off
|
||||
try {if (rs != null)rs.close();} catch (Exception e) {}
|
||||
try {if (st != null)st.close();} catch (Exception e) {}
|
||||
// @formatter:on
|
||||
}
|
||||
// return null;
|
||||
}
|
||||
|
||||
public static void createPrefix(@Nonnull long guildID, @Nullable String prefix)
|
||||
public static String createPrefix(@Nonnull long guildID, @Nullable String prefix)
|
||||
throws IllegalArgumentException {
|
||||
|
||||
if (prefix == null || prefix.isEmpty()) {
|
||||
|
@ -142,27 +140,25 @@ public class DatabaseTools {
|
|||
int[] updateCounts = pstmt.executeBatch();
|
||||
checkUpdateCounts(pstmt, updateCounts);
|
||||
pstmt.close();
|
||||
connection.commit();
|
||||
// connection.commit();
|
||||
return prefix;
|
||||
} catch (SQLException e) {
|
||||
sqlTranslate(pstmt, e);
|
||||
|
||||
for (int i : new int[] { 1062 }) {
|
||||
if (i == e.getErrorCode()) {
|
||||
setPrefix(connection, guildID, prefix);
|
||||
break;
|
||||
return setPrefix(connection, guildID, prefix);
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
connection.rollback();
|
||||
} catch (Exception e2) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static void setPrefix(Connection conn, long guildID, String prefix)
|
||||
public static String setPrefix(Connection conn, long guildID, String prefix)
|
||||
throws IllegalArgumentException {
|
||||
|
||||
if (prefix.isEmpty()) {
|
||||
|
@ -183,6 +179,7 @@ public class DatabaseTools {
|
|||
checkUpdateCounts(pstmt, updateCounts);
|
||||
pstmt.close();
|
||||
conn.commit();
|
||||
return prefix;
|
||||
} catch (SQLException e) {
|
||||
sqlTranslate(pstmt, e);
|
||||
try {
|
||||
|
@ -190,7 +187,7 @@ public class DatabaseTools {
|
|||
} catch (Exception e2) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -239,27 +236,30 @@ public class DatabaseTools {
|
|||
}
|
||||
|
||||
public class Global {
|
||||
|
||||
public static String prefix = null;
|
||||
|
||||
public static String getPrefix() {
|
||||
public static void updatePrefix() throws SQLException {
|
||||
Statement st = null;
|
||||
ResultSet rs = null;
|
||||
String query = "select * from global where id = 'prefix'";
|
||||
String msg = "Failed to execute; errorCode=NO_ROW_FOUND; No row found; On action " + query;
|
||||
try {
|
||||
st = connection.createStatement();
|
||||
rs = st.executeQuery(query);
|
||||
if (rs.next()) {
|
||||
return rs.getString("val");
|
||||
prefix = rs.getString("value");
|
||||
} else {
|
||||
// throw new SQLException(null, null, 33); // we need a real catcher here
|
||||
System.err
|
||||
.println("Failed to execute; errorCode=NO_ROW_FOUND; No row found; On action " + query);
|
||||
return null;
|
||||
|
||||
System.err.println(msg);
|
||||
throw new NullPointerException(msg);
|
||||
}
|
||||
|
||||
} catch (SQLException e) {
|
||||
sqlTranslate(query, e.getErrorCode());
|
||||
// System.out.println("eeeeee");
|
||||
return null;
|
||||
throw new SQLException(e);
|
||||
} finally {
|
||||
try {
|
||||
if (rs != null)
|
||||
|
@ -277,6 +277,7 @@ public class DatabaseTools {
|
|||
}
|
||||
// return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// public class Reaction { // started off as a good idea but it sucks
|
||||
|
|
|
@ -7,7 +7,11 @@ public class Reactions {
|
|||
private static HashMap<String, Long> reactionMap = new HashMap<String, Long>();
|
||||
|
||||
public static void init() {
|
||||
reactionMap.put("galaxyThumb", 801657838358495232L);
|
||||
insert("galaxyThumb", 801657838358495232L);
|
||||
}
|
||||
|
||||
public static void insert(String input, long id) {
|
||||
reactionMap.put(input, id);
|
||||
}
|
||||
|
||||
public static String getReaction(String id) {
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
package pkg.deepCurse.nopalmo.global;
|
||||
|
||||
import net.dv8tion.jda.api.entities.Member;
|
||||
|
||||
public class Tools {
|
||||
|
||||
}
|
||||
|
|
|
@ -6,14 +6,13 @@ import net.dv8tion.jda.api.entities.Message;
|
|||
import net.dv8tion.jda.api.events.ReadyEvent;
|
||||
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
|
||||
import net.dv8tion.jda.api.hooks.ListenerAdapter;
|
||||
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.Reactions;
|
||||
import pkg.deepCurse.nopalmo.manager.GuildCommandManager;
|
||||
|
||||
public class GuildMessageReceivedListener extends ListenerAdapter {
|
||||
|
||||
public static GuildCommandManager m = new GuildCommandManager();
|
||||
|
||||
@Override
|
||||
public void onReady(@Nonnull ReadyEvent event) {
|
||||
|
@ -36,9 +35,9 @@ public class GuildMessageReceivedListener extends ListenerAdapter {
|
|||
}
|
||||
}
|
||||
|
||||
if (messageRaw.contentEquals(Global.getPrefix() + Global.getPrefix())
|
||||
if (messageRaw.contentEquals(Global.prefix + Global.prefix)
|
||||
&& DatabaseTools.Tools.Developers.canPowerOffBot(event.getAuthor().getIdLong())) {
|
||||
message.addReaction(Reactions.getReaction("galaxyThumb")).queue();
|
||||
message.addReaction(Reactions.getReaction("galaxyThumb")).complete();
|
||||
|
||||
System.out.println("Shutting down; id " + event.getAuthor().getIdLong() + " used");
|
||||
|
||||
|
@ -55,7 +54,7 @@ public class GuildMessageReceivedListener extends ListenerAdapter {
|
|||
}
|
||||
|
||||
if (!event.getAuthor().isBot()) {
|
||||
m.startCommand(event);
|
||||
Boot.guildCommandManager.startCommand(event);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
24
src/pkg/deepCurse/nopalmo/manager/CommandBlob.java
Normal file
24
src/pkg/deepCurse/nopalmo/manager/CommandBlob.java
Normal file
|
@ -0,0 +1,24 @@
|
|||
package pkg.deepCurse.nopalmo.manager;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class CommandBlob {
|
||||
|
||||
private String modifiedRaw = null;
|
||||
private String modified = null;
|
||||
|
||||
private ArrayList<String> args = null;
|
||||
|
||||
public String getModifiedMessageContents() {
|
||||
return this.modified;
|
||||
}
|
||||
|
||||
public String getModifiedRawMessageContents() {
|
||||
return this.modifiedRaw;
|
||||
}
|
||||
|
||||
public ArrayList<String> getArgs() {
|
||||
return args;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,5 +1,8 @@
|
|||
package pkg.deepCurse.nopalmo.manager;
|
||||
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
@ -10,22 +13,23 @@ 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.Ping;
|
||||
import pkg.deepCurse.nopalmo.database.DatabaseTools.Tools.Global;
|
||||
import pkg.deepCurse.nopalmo.core.Boot;
|
||||
import pkg.deepCurse.nopalmo.database.DatabaseTools;
|
||||
|
||||
public class GuildCommandManager {
|
||||
|
||||
|
||||
private final Map<String, GuildCommand> guildCommandMap = new HashMap<>();
|
||||
Executor executor = null;
|
||||
|
||||
|
||||
public GuildCommandManager() {
|
||||
init();
|
||||
executor = Executors.newSingleThreadExecutor();
|
||||
}
|
||||
|
||||
|
||||
public void init() {
|
||||
addCommand(new Ping());
|
||||
}
|
||||
|
||||
|
||||
private void addCommand(GuildCommand c) {
|
||||
if (!guildCommandMap.containsKey(c.getCommandName())) {
|
||||
guildCommandMap.put(c.getCommandName(), c);
|
||||
|
@ -34,31 +38,62 @@ public class GuildCommandManager {
|
|||
guildCommandMap.put(c.getCommandName(), c);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public Collection<GuildCommand> getguildCommandMap() {
|
||||
return guildCommandMap.values();
|
||||
}
|
||||
|
||||
|
||||
public GuildCommand getCommand(String commandName) {
|
||||
if (commandName != null) {
|
||||
return guildCommandMap.get(commandName);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public void startCommand(GuildMessageReceivedEvent guildMessage) {
|
||||
|
||||
|
||||
final String message = guildMessage.getMessage().getContentRaw();
|
||||
|
||||
final String[] split = message.replaceFirst("(?i)" + Pattern.quote(Global.getPrefix()), "").split("\\s+");
|
||||
|
||||
final String[] split = message.replaceFirst("(?i)" + Pattern.quote(DatabaseTools.Tools.Global.prefix), "")
|
||||
.split("\\s+");
|
||||
final String command = split[0].toLowerCase();
|
||||
|
||||
|
||||
executor.execute(() -> {
|
||||
long commandStartTime = System.currentTimeMillis();
|
||||
|
||||
|
||||
|
||||
try {
|
||||
|
||||
ArrayList<String> newArguments = new ArrayList<String>();
|
||||
|
||||
ArrayList<String[]> extractedFlags = new ArrayList<String[]>();
|
||||
|
||||
} catch (Exception e) {
|
||||
if (Boot.isProd) {
|
||||
guildMessage.getChannel().sendMessage("```\n" + e + "```").queue();
|
||||
} else {
|
||||
StringWriter sw = new StringWriter();
|
||||
PrintWriter pw = new PrintWriter(sw);
|
||||
e.printStackTrace(pw);
|
||||
guildMessage.getChannel().sendMessage("```\n" + sw.toString() + "```").queue();
|
||||
System.err.println("Exception caught in: " + e.toString());
|
||||
e.printStackTrace();
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
|
||||
if (Boot.isProd) {
|
||||
guildMessage.getChannel().sendMessage("```\n" + t + "```").queue();
|
||||
} else {
|
||||
StringWriter sw = new StringWriter();
|
||||
PrintWriter pw = new PrintWriter(sw);
|
||||
t.printStackTrace(pw);
|
||||
guildMessage.getChannel().sendMessage("```\n" + sw.toString() + "```").queue();
|
||||
System.err.println("Error caught in: " + t.toString());
|
||||
t.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue