@ -1,12 +1,12 @@
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.lang.reflect.InvocationTargetException ;
import java.util.logging.Logger ;
/ * *
@ -17,15 +17,61 @@ import java.util.logging.Logger;
public class DiscordBot
{
private static final Logger LOGGER = Logger . getLogger ( DiscordBot . class . getName ( ) ) ;
/ * *
* Discord Client instance
* /
private IDiscordClient client ;
public DiscordBot ( IDiscordClient client )
/ * *
* Constructor of the singleton
* @param client Discord client instance used to Bootstrap the whole program
* /
private 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 ( ) + ")" ) ) ;
enableModules ( ) ;
}
private void enableModules ( )
{
LOGGER . info ( "Enabling modules" ) ;
if ( ! Configuration . exists ( "plugins" , "enable" ) | | Configuration . getValue ( "plugins" , "enable" ) . isEmpty ( ) )
{
LOGGER . warning ( "Key \"plugins/enable\" doesn't exist in Configuration. No module will be enabled." ) ;
return ;
}
for ( String i : Configuration . getValue ( "plugins" , "enable" ) . split ( "," ) )
{
i = i . trim ( ) ;
try
{
Class < ? > clazz = Class . forName ( i ) ;
if ( ! AbstractModule . class . isAssignableFrom ( clazz ) )
throw new InvalidModuleException ( i + " isn't a valid module." ) ;
clazz . getConstructor ( IDiscordClient . class ) . newInstance ( client ) ;
} catch ( ClassNotFoundException e )
{
LOGGER . warning ( "Class " + i + " wasn't found, thus won't be loaded." ) ;
} catch ( InvalidModuleException e )
{
LOGGER . warning ( e . getMessage ( ) ) ;
} catch ( NoSuchMethodException e )
{
LOGGER . warning ( "No valid Constructor for " + i + " was found." ) ;
} catch ( InstantiationException | IllegalAccessException | InvocationTargetException e )
{
LOGGER . warning ( "An error occured while creating an instance of " + i ) ;
e . printStackTrace ( ) ;
}
}
}
// ====================================================
@ -43,7 +89,7 @@ public class DiscordBot
// LOAD CONFIGURATION
try
{
Configuration . loadConfiguration ( "config.ini" ) ;
Configuration . loadConfiguration ( "config.ini" ) ; // TODO: pass configuration file using commandline argument
} catch ( IOException e )
{
LOGGER . severe ( "Could not load Configuration, reason: " + e . getMessage ( ) ) ;
@ -65,6 +111,12 @@ public class DiscordBot
}
}
/ * *
* Discord login subroutine . Called by ` main ( ) ` once
* @return Instance of DiscordBot if login was successful
* @throws ConfigurationException When token / email / password field in config . ini isn ' t filled
* @throws DiscordException
* /
private static DiscordBot loginToDiscord ( ) throws ConfigurationException , DiscordException
{
ClientBuilder builder = new ClientBuilder ( ) ;