Changed settings class into a singleton and improved logging
This commit is contained in:
1 parent
967856ce57
commit
a7d4c7d711
16 files changed
+75
-51
No files matched your search
@@ -22,20 +22,18 @@ import javax.security.auth.login.LoginException;
|
||||
public class MusicBot {
|
||||
|
||||
private static JDA jda;
|
||||
public static Logger LOGGER = LoggerFactory.getLogger(MusicBot.class);
|
||||
private final static Logger logger = LoggerFactory.getLogger(MusicBot.class);
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.setProperty("log4j.skipJansi", "false");
|
||||
Settings.init();
|
||||
DBController.init();
|
||||
|
||||
try {
|
||||
jda = JDABuilder.createDefault(Settings.TOKEN).build();
|
||||
jda = JDABuilder.createDefault(Settings.getInstance().getToken()).build();
|
||||
jda.awaitReady();
|
||||
} catch (LoginException | IllegalArgumentException e) {
|
||||
LogHandler.log(e);
|
||||
} catch (InterruptedException e) {
|
||||
LOGGER.error(e.getMessage());
|
||||
} catch (LoginException | IllegalArgumentException | InterruptedException e) {
|
||||
logger.error(e.getMessage());
|
||||
shutdown();
|
||||
}
|
||||
|
||||
|
||||
@@ -59,12 +57,12 @@ public class MusicBot {
|
||||
}
|
||||
|
||||
public static void shutdown() {
|
||||
LOGGER.info("[Net Neutrality] Shutting down...");
|
||||
logger.info("[Net Neutrality] Shutting down...");
|
||||
MusicManager.disconnectPlayers();
|
||||
ThreadManager.shutdownExecutor();
|
||||
disconnect();
|
||||
DBController.shutdown();
|
||||
LOGGER.info("[Net Neutrality] Shutdown complete");
|
||||
logger.info("[Net Neutrality] Shutdown complete");
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,9 @@ package netneutrality;
|
||||
* This file is closed-source. It should not be distributed or copied in any way or form
|
||||
*/
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
@@ -13,11 +16,12 @@ import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class ThreadManager {
|
||||
|
||||
private final static Logger logger = LoggerFactory.getLogger(ThreadManager.class);
|
||||
public static final ExecutorService executor = Executors.newSingleThreadExecutor();
|
||||
public static ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(2);
|
||||
|
||||
public static void shutdownExecutor() {
|
||||
MusicBot.LOGGER.info("[Executor] Shutting down...");
|
||||
logger.info("[Executor] Shutting down...");
|
||||
|
||||
try {
|
||||
executor.shutdown();
|
||||
@@ -26,22 +30,22 @@ public class ThreadManager {
|
||||
scheduler.awaitTermination(5, TimeUnit.SECONDS);
|
||||
}
|
||||
catch (InterruptedException e) {
|
||||
MusicBot.LOGGER.warn("[Executor] Running tasks interrupted");
|
||||
logger.warn("[Executor] Running tasks interrupted");
|
||||
}
|
||||
finally {
|
||||
if (!executor.isTerminated()) {
|
||||
MusicBot.LOGGER.warn("[Executor] Forcing shutdown of running tasks..");
|
||||
logger.warn("[Executor] Forcing shutdown of running tasks..");
|
||||
executor.shutdownNow();
|
||||
}
|
||||
|
||||
if (!scheduler.isTerminated()) {
|
||||
MusicBot.LOGGER.warn("[Executor] Forcing shutdown of running tasks..");
|
||||
logger.warn("[Executor] Forcing shutdown of running tasks..");
|
||||
scheduler.shutdownNow();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
MusicBot.LOGGER.info("[Executor] Shutdown finished!");
|
||||
logger.info("[Executor] Shutdown finished!");
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -9,6 +9,8 @@ package netneutrality.command;
|
||||
|
||||
import netneutrality.MusicBot;
|
||||
import org.reflections.Reflections;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Modifier;
|
||||
@@ -17,6 +19,7 @@ import java.util.Set;
|
||||
|
||||
public class CommandManager {
|
||||
|
||||
private final static Logger logger = LoggerFactory.getLogger(CommandManager.class);
|
||||
public static HashMap<String, AbstractCommand> commands = new HashMap<>();
|
||||
public static HashMap<String, AbstractCommand> commandsAlias = new HashMap<>();
|
||||
|
||||
@@ -26,7 +29,7 @@ public class CommandManager {
|
||||
}
|
||||
|
||||
private static void initCommands() {
|
||||
MusicBot.LOGGER.info("Loading commands..");
|
||||
logger.info("Loading commands..");
|
||||
|
||||
Reflections reflections = new Reflections("netneutrality.command");
|
||||
Set<Class<? extends AbstractCommand>> classes = reflections.getSubTypesOf(AbstractCommand.class);
|
||||
@@ -51,7 +54,7 @@ public class CommandManager {
|
||||
}
|
||||
}
|
||||
|
||||
MusicBot.LOGGER.info("Loaded " + commands.size() + " commands!");
|
||||
logger.info("Loaded " + commands.size() + " commands!");
|
||||
}
|
||||
|
||||
private static void initAlias() {
|
||||
|
||||
@@ -73,7 +73,7 @@ public class HelpCommand extends AbstractCommand {
|
||||
AbstractCommand command = commands.get(key);
|
||||
|
||||
if (command.isEnabled()) {
|
||||
help.append(Settings.PREFIX).append(command.getCommand());
|
||||
help.append(Settings.getInstance().getPrefix()).append(command.getCommand());
|
||||
if (command.getUsage() != null)
|
||||
help.append(" ").append(command.getUsage());
|
||||
|
||||
|
||||
@@ -59,7 +59,7 @@ public class DatabaseCommand extends AbstractCommand {
|
||||
//TODO: Clean up the mess
|
||||
event.getMessage().delete().queue();
|
||||
|
||||
if (!event.getAuthor().getDiscriminator().equals(Settings.getCreator().getDiscriminator()))
|
||||
if (!event.getAuthor().getDiscriminator().equals(Settings.getInstance().getCreator().getDiscriminator()))
|
||||
return null;
|
||||
|
||||
if (countArgs(args) > 0) {
|
||||
@@ -74,7 +74,7 @@ public class DatabaseCommand extends AbstractCommand {
|
||||
EmbedBuilder embed = new EmbedBuilder();
|
||||
embed.setColor(Color.BLUE);
|
||||
embed.setTimestamp(
|
||||
DateTimeFormatter.ofPattern(Settings.DATETIMEFORMAT).parse(message.getDateSent()));
|
||||
DateTimeFormatter.ofPattern(Settings.getInstance().DATETIMEFORMAT).parse(message.getDateSent()));
|
||||
embed.setFooter("Guild : " + message.getServerName() + " | ID : " + message.getMessageId(), null);
|
||||
embed.setDescription(
|
||||
"Message from " + message.getSender() + " on the channel " + message.getChannelName());
|
||||
|
||||
@@ -56,7 +56,7 @@ public class EvalMethod extends AbstractCommand {
|
||||
@Override
|
||||
public String execute(MessageReceivedEvent event, String args) {
|
||||
|
||||
if (event.getAuthor().getId().equals(Settings.getCreator().getId())) {
|
||||
if (event.getAuthor().getId().equals(Settings.getInstance().getCreator().getId())) {
|
||||
ScriptEngine se = new ScriptEngineManager().getEngineByName("Nashorn");
|
||||
try {
|
||||
se.eval("var imports = new JavaImporter(java.io, java.lang, java.util);");
|
||||
|
||||
@@ -55,7 +55,7 @@ public class TestCommand extends AbstractCommand {
|
||||
|
||||
@Override
|
||||
public String execute(MessageReceivedEvent event, String args) {
|
||||
if (event.getAuthor().getDiscriminator().equals(Settings.getCreator().getDiscriminator())) {
|
||||
if (event.getAuthor().getDiscriminator().equals(Settings.getInstance().getCreator().getDiscriminator())) {
|
||||
MessageFunction.isStartingWithMention(event.getMessage());
|
||||
}
|
||||
|
||||
|
||||
@@ -38,7 +38,7 @@ public class DBMessage extends AbstractController {
|
||||
String servername = guild.getName();
|
||||
String channelname = channel.getName();
|
||||
String username = author.getName();
|
||||
String datesent = message.getTimeCreated().format(DateTimeFormatter.ofPattern(Settings.DATETIMEFORMAT));
|
||||
String datesent = message.getTimeCreated().format(DateTimeFormatter.ofPattern(Settings.getInstance().DATETIMEFORMAT));
|
||||
String messageid = message.getId();
|
||||
String msg = message.getContentRaw().trim();
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package netneutrality.handler;
|
||||
|
||||
import netneutrality.MusicBot;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
@@ -8,9 +9,9 @@ import java.io.InputStreamReader;
|
||||
|
||||
public class ConsoleHandler {
|
||||
|
||||
|
||||
private final static Logger logger = LoggerFactory.getLogger(ConsoleHandler.class);
|
||||
public ConsoleHandler() {
|
||||
MusicBot.LOGGER.info("Console input ready!");
|
||||
logger.info("Console input ready!");
|
||||
}
|
||||
|
||||
public String init() {
|
||||
|
||||
@@ -2,9 +2,13 @@ package netneutrality.handler;
|
||||
|
||||
import netneutrality.MusicBot;
|
||||
import netneutrality.utils.Settings;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class LogHandler {
|
||||
|
||||
private final static Logger logger = LoggerFactory.getLogger(LogHandler.class);
|
||||
|
||||
/**
|
||||
* Log the exception to the owner's log TextChannel as defined in {@link Settings#getLogChannel()}
|
||||
*
|
||||
@@ -12,8 +16,10 @@ public class LogHandler {
|
||||
*/
|
||||
public static void log(Exception e) {
|
||||
|
||||
MusicBot.LOGGER.error(e.getMessage());
|
||||
Settings.getLogChannel().sendMessage("Error : " + e).queue();
|
||||
logger.error(e.getMessage());
|
||||
|
||||
//TODO: to remove or add check if jda is started
|
||||
Settings.getInstance().getLogChannel().sendMessage("Error : " + e).queue();
|
||||
|
||||
}
|
||||
}
|
||||
@@ -37,7 +37,7 @@ public class NewCommandListener extends ListenerAdapter {
|
||||
Message message = event.getMessage();
|
||||
String msg = message.getContentDisplay().trim();
|
||||
|
||||
if (msg.startsWith(Settings.PREFIX)) {
|
||||
if (msg.startsWith(Settings.getInstance().getPrefix())) {
|
||||
msg = msg.substring(1);
|
||||
String cmd = msg.split(" ", 2)[0];
|
||||
|
||||
|
||||
@@ -7,18 +7,21 @@ package netneutrality.http.server;
|
||||
*/
|
||||
|
||||
import com.sun.net.httpserver.HttpServer;
|
||||
import netneutrality.MusicBot;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
|
||||
public class WebApi {
|
||||
|
||||
private final static Logger logger = LoggerFactory.getLogger(WebApi.class);
|
||||
|
||||
private int port = 9000;
|
||||
private HttpServer server;
|
||||
|
||||
public WebApi() {
|
||||
MusicBot.LOGGER.debug("Starting web server...");
|
||||
logger.debug("Starting web server...");
|
||||
try {
|
||||
server = HttpServer.create(new InetSocketAddress(port), 0);
|
||||
} catch (IOException e) {
|
||||
@@ -28,7 +31,7 @@ public class WebApi {
|
||||
server.createContext("/guild", new GuildBrowser());
|
||||
server.setExecutor(null);
|
||||
server.start();
|
||||
MusicBot.LOGGER.debug("Web server started at [" + server.getAddress().getAddress().toString() + "]");
|
||||
logger.debug("Web server started at [" + server.getAddress().getAddress().toString() + "]");
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -75,7 +75,7 @@ public class MusicManager {
|
||||
leave();
|
||||
}, 10, 10, TimeUnit.SECONDS);
|
||||
} else
|
||||
MusicBot.LOGGER.error("Error connecting!");
|
||||
logger.error("Error connecting!");
|
||||
}
|
||||
|
||||
public boolean isConnected() {
|
||||
|
||||
@@ -12,33 +12,42 @@ import java.util.Properties;
|
||||
|
||||
public class Settings {
|
||||
|
||||
static Properties prop = new Properties();
|
||||
static String configFile = "bot.config";
|
||||
public static Settings INSTANCE;
|
||||
private Properties prop = new Properties();
|
||||
|
||||
public static void init() {
|
||||
public Settings() {
|
||||
String configFile = "bot.config";
|
||||
InputStream is = Settings.class.getClassLoader().getResourceAsStream(configFile);
|
||||
|
||||
try (FileInputStream fis = new FileInputStream(configFile)) {
|
||||
prop.load(fis);
|
||||
} catch (IOException ignored) {
|
||||
|
||||
try {
|
||||
prop.load(is);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static final String DATETIMEFORMAT = "yyyy-MM-dd HH:mm:ss";
|
||||
public static String PREFIX = prop.getProperty("bot.prefix");
|
||||
public static final String TOKEN = prop.getProperty("bot.token");
|
||||
public static Settings getInstance() {
|
||||
if(INSTANCE == null) {
|
||||
INSTANCE = new Settings();
|
||||
}
|
||||
|
||||
public static User getCreator() {
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
public final String DATETIMEFORMAT = "yyyy-MM-dd HH:mm:ss";
|
||||
public String getPrefix() { return prop.getProperty("bot.prefix"); }
|
||||
public String getToken() { return prop.getProperty("bot.token"); }
|
||||
|
||||
public User getCreator() {
|
||||
return MusicBot.getJDA().getUserById(prop.getProperty("bot.owner"));
|
||||
}
|
||||
|
||||
public static Guild getOwnerGuild() {
|
||||
public Guild getOwnerGuild() {
|
||||
return MusicBot.getJDA().getGuildById(prop.getProperty("bot.ownerguild"));
|
||||
}
|
||||
|
||||
public static TextChannel getLogChannel() {
|
||||
public TextChannel getLogChannel() {
|
||||
return MusicBot.getJDA().getTextChannelById(prop.getProperty("bot.errorchannel"));
|
||||
}
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ public class UserFunction {
|
||||
public static boolean isAdmin(Member user) {
|
||||
|
||||
// This check will only be present during the development of the bot
|
||||
if (user.getUser().getId().equals(Settings.getCreator().getId())) {
|
||||
if (user.getUser().getId().equals(Settings.getInstance().getCreator().getId())) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user