pingex
/
DiscordBot
Archived
1
0
Fork 0

Login to Discord, better Configuration errors handling.

master
Pingex aka Raphaël 9 years ago
parent 1f08fa0d9d
commit 9a6a7946f7

@ -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

@ -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;
}
}

@ -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();
}
}

@ -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<ReadyEvent>) 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());
}
}