From ee08b6b29c62d2242215254fd134da030829c70c Mon Sep 17 00:00:00 2001 From: Louis Vallat Date: Tue, 27 Oct 2020 16:53:28 +0100 Subject: [PATCH] Removed support for command line arguments are there are too many arguments to manage. Fixed small typo in DBManager Signed-off-by: Louis Vallat --- .../java/xyz/vallat/louis/MovieQuoteBot.java | 31 +++++----------- .../xyz/vallat/louis/database/DBManager.java | 35 ++++++++----------- 2 files changed, 22 insertions(+), 44 deletions(-) diff --git a/src/main/java/xyz/vallat/louis/MovieQuoteBot.java b/src/main/java/xyz/vallat/louis/MovieQuoteBot.java index e62711d..954d03c 100644 --- a/src/main/java/xyz/vallat/louis/MovieQuoteBot.java +++ b/src/main/java/xyz/vallat/louis/MovieQuoteBot.java @@ -11,6 +11,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import reactor.core.publisher.Mono; import xyz.vallat.louis.commands.*; +import xyz.vallat.louis.database.DBManager; import xyz.vallat.louis.subtitles.OpenSubtitles; import java.util.HashMap; @@ -18,10 +19,12 @@ import java.util.Map; public class MovieQuoteBot { + // TODO: MOVE TO ENUM public static final String DISCORD_TOKEN_ENVIRONMENT = "DISCORD_TOKEN"; public static final String OS_USERNAME_ENVIRONMENT = "OPEN_SUBTITLES_USERNAME"; public static final String OS_PASSWORD_ENVIRONMENT = "OPEN_SUBTITLES_PASSWORD"; public static final String OS_UA_ENVIRONMENT = "OPEN_SUBTITLES_USER_AGENT"; + public static final String PREFIX = "!"; public static final String NAME = "Movies Quote Bot"; public static final String DESCRIPTION = "I may know some quotes from some movies."; @@ -39,33 +42,15 @@ public class MovieQuoteBot { } public static void main(String[] args) { - if ((System.getenv(DISCORD_TOKEN_ENVIRONMENT) == null || - System.getenv(OS_USERNAME_ENVIRONMENT) == null || - System.getenv(OS_PASSWORD_ENVIRONMENT) == null || - System.getenv(OS_UA_ENVIRONMENT) == null - ) && args.length < 4) { - logger.error("Missing tokens. Exiting."); - System.exit(1); - } - - String discordToken = System.getenv(DISCORD_TOKEN_ENVIRONMENT) == null ? - args[0] : - System.getenv(DISCORD_TOKEN_ENVIRONMENT); - + DBManager.testConnection(); // TODO: FIX CRASH ON LOGIN IF OS IS IN MAINTENANCE OR BROKEN OpenSubtitles.login( - System.getenv(OS_USERNAME_ENVIRONMENT) == null ? - args[1] : - System.getenv(OS_USERNAME_ENVIRONMENT), - System.getenv(OS_PASSWORD_ENVIRONMENT) == null ? - args[2] : - System.getenv(OS_PASSWORD_ENVIRONMENT), + System.getenv(OS_USERNAME_ENVIRONMENT), + System.getenv(OS_PASSWORD_ENVIRONMENT), "en", - System.getenv(OS_UA_ENVIRONMENT) == null ? - args[3] : - System.getenv(OS_UA_ENVIRONMENT) + System.getenv(OS_UA_ENVIRONMENT) ); - discordLogin(discordToken); + discordLogin(System.getenv(DISCORD_TOKEN_ENVIRONMENT)); registerDiscordCommands(); Runtime.getRuntime().addShutdownHook(new Thread(() -> { diff --git a/src/main/java/xyz/vallat/louis/database/DBManager.java b/src/main/java/xyz/vallat/louis/database/DBManager.java index e29f806..1f5a81a 100644 --- a/src/main/java/xyz/vallat/louis/database/DBManager.java +++ b/src/main/java/xyz/vallat/louis/database/DBManager.java @@ -11,37 +11,30 @@ import java.sql.Statement; public final class DBManager { + public static final String DB_USERNAME_ENVIRONMENT = "DB_USERNAME"; + public static final String DB_PASSWORD_ENVIRONMENT = "DB_PASSWORD"; + public static final String DB_PORT_ENVIRONMENT = "DB_PORT"; + public static final String DB_HOST_ENVIRONMENT = "DB_HOST"; + public static final String DB_NAME_ENVIRONMENT = "DB_NAME"; private static final Logger logger = LoggerFactory.getLogger(MovieQuoteBot.class.getCanonicalName()); - private static final String DRIVER_CLASS = "org.posgresql.Driver"; + private static final String DRIVER_CLASS = "org.postgresql.Driver"; private static final String CONNECTION_PREFIX = "postgresql"; - - private static String username; - private static String password; - private static String database; - private static int port = 5432; - private static String host; + private static final String USERNAME = System.getenv(DB_USERNAME_ENVIRONMENT); + private static final String PASSWORD = System.getenv(DB_PASSWORD_ENVIRONMENT); + private static final String DATABASE = System.getenv(DB_NAME_ENVIRONMENT); + private static final int PORT = System.getenv(DB_PORT_ENVIRONMENT) == null ? 5432 : + Integer.parseInt(System.getenv(DB_PORT_ENVIRONMENT)); + private static final String HOST = System.getenv(DB_HOST_ENVIRONMENT); private DBManager() { } - public static void setLoginInformation(String host, String database, String username, String password) { - DBManager.host = host; - DBManager.database = database; - DBManager.username = username; - DBManager.password = password; - } - - public static void setLoginInformation(String host, int port, String database, String username, String password) { - DBManager.port = port; - setLoginInformation(host, database, username, password); - } - // TODO: EXIT CODES AS ENUM private static Connection getConnection() { try { Class.forName(DRIVER_CLASS); - return DriverManager.getConnection("jdbc:" + CONNECTION_PREFIX + "://" + host + ":" + port + "/" - + database, username, password); + return DriverManager.getConnection("jdbc:" + CONNECTION_PREFIX + "://" + HOST + ":" + PORT + "/" + + DATABASE, USERNAME, PASSWORD); } catch (ClassNotFoundException | SQLException e) { logger.error("Could not connect to database. Reason: {}.", e.getMessage()); System.exit(4);