From 9a6a7946f77047ef1d778f4f61e07204e68e6ec6 Mon Sep 17 00:00:00 2001 From: Pingex Date: Fri, 8 Apr 2016 04:34:54 +0200 Subject: [PATCH] Login to Discord, better Configuration errors handling. --- config.example.ini | 7 +++ .../net/pingex/discordbot/Configuration.java | 11 ++++ .../discordbot/ConfigurationException.java | 34 +++++++++++ .../net/pingex/discordbot/DiscordBot.java | 58 +++++++++++++++++-- 4 files changed, 106 insertions(+), 4 deletions(-) create mode 100644 src/main/java/net/pingex/discordbot/ConfigurationException.java diff --git a/config.example.ini b/config.example.ini index e69de29..b7f6167 100644 --- a/config.example.ini +++ b/config.example.ini @@ -0,0 +1,7 @@ +[discord] +; Change next value to true if the account is a bot, thus logins using a token. +; Don't forget to uncomment token then +isBot = false +email = bot@example.com +password = samplePassword +; token = sampleToken \ No newline at end of file diff --git a/src/main/java/net/pingex/discordbot/Configuration.java b/src/main/java/net/pingex/discordbot/Configuration.java index e86b08f..8fd33ad 100644 --- a/src/main/java/net/pingex/discordbot/Configuration.java +++ b/src/main/java/net/pingex/discordbot/Configuration.java @@ -85,4 +85,15 @@ public final class Configuration datastore.get(section).replace(key, value); } } + + /** + * Say if the key exists in the datastore + * @param section Section of the datastore + * @param key Key to research + * @return `true` if the key exists, `false` otherwise + */ + public static boolean exists(String section, String key) + { + return (datastore.get(section, key) != null) ? true : false; + } } diff --git a/src/main/java/net/pingex/discordbot/ConfigurationException.java b/src/main/java/net/pingex/discordbot/ConfigurationException.java new file mode 100644 index 0000000..79407a0 --- /dev/null +++ b/src/main/java/net/pingex/discordbot/ConfigurationException.java @@ -0,0 +1,34 @@ +package net.pingex.discordbot; + +/** + * Exception thrown when an issue involving Configuration occurs. + * @version 0.1-dev + * @author Raphael "Pingex" NAAS + */ +public class ConfigurationException extends Exception +{ + public ConfigurationException(String message) + { + super(message); + } + + protected ConfigurationException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) + { + super(message, cause, enableSuppression, writableStackTrace); + } + + public ConfigurationException(Throwable cause) + { + super(cause); + } + + public ConfigurationException(String message, Throwable cause) + { + super(message, cause); + } + + public ConfigurationException() + { + super(); + } +} diff --git a/src/main/java/net/pingex/discordbot/DiscordBot.java b/src/main/java/net/pingex/discordbot/DiscordBot.java index c4ced47..6a5fc6c 100644 --- a/src/main/java/net/pingex/discordbot/DiscordBot.java +++ b/src/main/java/net/pingex/discordbot/DiscordBot.java @@ -1,5 +1,11 @@ package net.pingex.discordbot; +import sx.blah.discord.api.ClientBuilder; +import sx.blah.discord.api.Event; +import sx.blah.discord.api.IDiscordClient; +import sx.blah.discord.api.IListener; +import sx.blah.discord.handle.impl.events.ReadyEvent; +import sx.blah.discord.util.DiscordException; import java.io.IOException; import java.util.logging.Logger; @@ -11,6 +17,20 @@ import java.util.logging.Logger; public class DiscordBot { private static final Logger LOGGER = Logger.getLogger(DiscordBot.class.getName()); + private IDiscordClient client; + + public DiscordBot(IDiscordClient client) + { + this.client = client; + LOGGER.info("Successfully logged in !"); + + this.client.getDispatcher().registerListener((IListener) event -> + LOGGER.info("Logged in as \"" + event.getClient().getOurUser().getName() + "#" + event.getClient().getOurUser().getDiscriminator() + "\" (#" + event.getClient().getOurUser().getID() + ")")); + } + + // ==================================================== + // Static functions: main, config loader, etc. + // ==================================================== /** * Entry point @@ -19,11 +39,8 @@ public class DiscordBot public static void main(String[] args) { LOGGER.info("Hello World, starting up"); - loadConfigurationSubroutine(); - } - private static void loadConfigurationSubroutine() // TODO: Configuration file passed via CLI - { + // LOAD CONFIGURATION try { Configuration.loadConfiguration("config.ini"); @@ -32,5 +49,38 @@ public class DiscordBot LOGGER.severe("Could not load Configuration, reason: " + e.getMessage()); System.exit(10); } + + LOGGER.info("Logging in to Discord"); + try + { + loginToDiscord(); + } catch (ConfigurationException e) + { + LOGGER.severe("There is a problem with the Configuration: " + e.getMessage()); + System.exit(9); + } catch (DiscordException e) + { + LOGGER.severe(e.getMessage()); + System.exit(2); + } + } + + private static DiscordBot loginToDiscord() throws ConfigurationException, DiscordException + { + ClientBuilder builder = new ClientBuilder(); + if(Configuration.getValue("discord", "isBot", boolean.class)) + { + if(!Configuration.exists("discord", "token")) + throw new ConfigurationException("No login token defined"); + builder.withToken(Configuration.getValue("discord", "token")); + } + else + { + if(!Configuration.exists("discord", "email") || !Configuration.exists("discord", "password")) + throw new ConfigurationException("Can't login: Either the email or the password wasn't provided."); + builder.withLogin(Configuration.getValue("discord", "email"), Configuration.getValue("discord", "password")); + } + + return new DiscordBot(builder.login()); } }