Changed settings class to remove dependency to JDA

This commit is contained in:
anthorob committed 2022-01-06 16:10:48 -05:00
1 parent 132fa92557
commit 2a1c7c4f3b
9 files changed
+36 -38

No files matched your search

+13 -11
View File
@@ -26,19 +26,9 @@ public class MusicBot {
public final static Logger logger = LoggerFactory.getLogger(MusicBot.class); public final static Logger logger = LoggerFactory.getLogger(MusicBot.class);
public static void main(String[] args) { public static void main(String[] args) {
System.setProperty("log4j.skipJansi", "false");
DBController.init(); DBController.init();
try { initJDA();
jda = JDABuilder.createDefault(Settings.getInstance().get("BOT_TOKEN"))
.enableIntents(GatewayIntent.GUILD_MEMBERS, GatewayIntent.GUILD_MESSAGE_REACTIONS, GatewayIntent.GUILD_VOICE_STATES)
.build();
jda.awaitReady();
} catch (LoginException | IllegalArgumentException | InterruptedException e) {
logger.error(e.getMessage());
System.exit(0);
}
jda.getPresence().setActivity(Activity.watching("me load...")); jda.getPresence().setActivity(Activity.watching("me load..."));
jda.addEventListener(new NewCommandListener(), new EditHandler()); jda.addEventListener(new NewCommandListener(), new EditHandler());
@@ -57,6 +47,18 @@ public class MusicBot {
} }
public static void initJDA() {
try {
jda = JDABuilder.createDefault(Settings.getInstance().get("BOT_TOKEN"))
.enableIntents(GatewayIntent.GUILD_MEMBERS, GatewayIntent.GUILD_MESSAGE_REACTIONS, GatewayIntent.GUILD_VOICE_STATES)
.build();
jda.awaitReady();
} catch (LoginException | IllegalArgumentException | InterruptedException e) {
logger.error(e.getMessage());
System.exit(0);
}
}
public static void shutdown() { public static void shutdown() {
System.out.println("[Net Neutrality] Shutting down..."); System.out.println("[Net Neutrality] Shutting down...");
MusicManager.disconnectPlayers(); MusicManager.disconnectPlayers();
@@ -4,10 +4,8 @@ import java.awt.Color;
import java.time.OffsetDateTime; import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.function.Consumer;
import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.PrivateChannel;
import net.dv8tion.jda.api.entities.User; import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent; import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import netneutrality.command.AbstractCommand; import netneutrality.command.AbstractCommand;
@@ -66,7 +64,7 @@ public class DatabaseCommand extends AbstractCommand {
event.getMessage().delete().queue(); event.getMessage().delete().queue();
if (!event.getAuthor().getDiscriminator().equals(Settings.getInstance().getCreator().getDiscriminator())) if (!event.getAuthor().getId().equals(Settings.getInstance().getCreatorId()))
return null; return null;
if (args.length > 0) { if (args.length > 0) {
@@ -62,7 +62,7 @@ public class EvalMethod extends AbstractCommand {
@Override @Override
public String execute(MessageReceivedEvent event, String[] args) { public String execute(MessageReceivedEvent event, String[] args) {
if (event.getAuthor().getId().equals(Settings.getInstance().getCreator().getId())) { if (event.getAuthor().getId().equals(Settings.getInstance().getCreatorId())) {
ScriptEngine se = new ScriptEngineManager().getEngineByName("Nashorn"); ScriptEngine se = new ScriptEngineManager().getEngineByName("Nashorn");
try { try {
se.eval("var imports = new JavaImporter(java.io, java.lang, java.util);"); se.eval("var imports = new JavaImporter(java.io, java.lang, java.util);");
@@ -57,7 +57,7 @@ public class TestCommand extends AbstractCommand {
@Override @Override
public String execute(MessageReceivedEvent event, String[] args) { public String execute(MessageReceivedEvent event, String[] args) {
if (event.getAuthor().getDiscriminator().equals(Settings.getInstance().getCreator().getDiscriminator())) { if (event.getAuthor().getId().equals(Settings.getInstance().getCreatorId())) {
MessageFunction.isStartingWithMention(event.getMessage()); MessageFunction.isStartingWithMention(event.getMessage());
} }
@@ -1,5 +1,7 @@
package netneutrality.db; package netneutrality.db;
import com.mysql.cj.jdbc.MysqlDataSource;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
import java.sql.SQLException; import java.sql.SQLException;
@@ -25,14 +27,15 @@ public class MysqlConnector {
* @return {@link MysqlConnector} * @return {@link MysqlConnector}
*/ */
public MysqlConnector connect() throws SQLException { public MysqlConnector connect() throws SQLException {
MysqlDataSource dataSource = new MysqlDataSource();
String jdbc = "jdbc:mysql://" + dbHost + ":3306/" + dbName; String jdbc = "jdbc:mysql://" + dbHost + ":3306/" + dbName;
try { dataSource.setURL(jdbc);
Class.forName("com.mysql.cj.jdbc.Driver"); dataSource.setUser(dbUser);
c = DriverManager.getConnection(jdbc, dbUser, dbPass); dataSource.setPassword(dbPass);
} catch (ClassNotFoundException e) { dataSource.setAutoReconnect(true);
e.printStackTrace(); dataSource.setConnectTimeout(5000);
} c = dataSource.getConnection();
return this; return this;
} }
@@ -42,7 +42,7 @@ public abstract class AbstractEntity implements EntityInterface {
this.table = DBController.get().prepareAndExecuteAsync(sql, this.primaryKey, this.primaryKeys).blocking(); this.table = DBController.get().prepareAndExecuteAsync(sql, this.primaryKey, this.primaryKeys).blocking();
else else
this.table = DBController.get().prepareAndExecuteAsync(sql, this.primaryKey).blocking(); this.table = DBController.get().prepareAndExecuteAsync(sql, this.primaryKey).blocking();
} }
} }
@@ -19,7 +19,8 @@ public class LogHandler {
logger.error(e.getMessage()); logger.error(e.getMessage());
//TODO: to remove or add check if jda is started //TODO: to remove or add check if jda is started
Settings.getInstance().getLogChannel().sendMessage("Error : " + e).queue(); String channel = Settings.getInstance().getLogChannel();
MusicBot.getJDA().getTextChannelById(channel).sendMessage("Error : " + e).queue();
} }
@@ -1,10 +1,5 @@
package netneutrality.utils; package netneutrality.utils;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.entities.User;
import netneutrality.MusicBot;
import java.io.*; import java.io.*;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
@@ -68,16 +63,16 @@ public class Settings {
public final String DATETIMEFORMAT = "yyyy-MM-dd HH:mm:ss"; public final String DATETIMEFORMAT = "yyyy-MM-dd HH:mm:ss";
public User getCreator() { public String getCreatorId() {
return MusicBot.getJDA().getUserById(get("BOT_OWNER")); return get("BOT_OWNER");
} }
public Guild getOwnerGuild() { public String getOwnerGuild() {
return MusicBot.getJDA().getGuildById(get("BOT_OWNERGUILD")); return get("BOT_OWNERGUILD");
} }
public TextChannel getLogChannel() { public String getLogChannel() {
return MusicBot.getJDA().getTextChannelById(get("BOT_ERRORCHANNEL")); return get("BOT_ERRORCHANNEL");
} }
@@ -3,15 +3,14 @@ package netneutrality.utils;
import net.dv8tion.jda.api.Permission; import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.Member; import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.Role; import net.dv8tion.jda.api.entities.Role;
import net.dv8tion.jda.api.entities.User;
public class UserFunction { public class UserFunction {
public static boolean isAdmin(Member user) { public static boolean isAdmin(Member user) {
// This check will only be present during the development of the bot // This check will only be present during the development of the bot
User creator = Settings.getInstance().getCreator(); String creatorId = Settings.getInstance().getCreatorId();
if (creator != null && user.getId().equals(creator.getId())) { if (creatorId != null && user.getId().equals(creatorId)) {
return true; return true;
} }