package me.mrkirby153.KirBot;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import me.mrkirby153.KirBot.utils.BotConfiguration;
import net.dv8tion.jda.core.JDA;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * Main Application class for the discord robot
 */
public class KirBot {

    /**
     * The location of the configuration file
     */
    private static final File CONFIG_LOCATION = new File("config.json");

    /**
     * The main Gson instance of the application
     */
    public static Gson GSON = new GsonBuilder().setPrettyPrinting().create();

    /**
     * The main robot logger
     */
    public static Logger logger = LogManager.getLogger("KirBot");


    /**
     * The robot's configuration
     */
    public BotConfiguration configuration;

    /**
     * The main JDA instance of the robot
     */
    private JDA jda;

    protected KirBot() {

    }

    /**
     * Initialize the robot
     */
    public void initialize() {
        // Load configuration
        loadConfiguration();


    }

    private void loadConfiguration() {
        logger.info("Loading configuration from " + CONFIG_LOCATION.getAbsolutePath());

        // Check if the configuration file exists
        if (!CONFIG_LOCATION.exists()) {
            logger.warn("The configuration file does not exist, creating...");

            BotConfiguration blankConfig = new BotConfiguration();
            String json = GSON.toJson(blankConfig);

            // Write the JSON
            try {
                FileWriter writer = new FileWriter(CONFIG_LOCATION);
                writer.write(json);
                writer.flush();
                writer.close();
                logger.info("Configuration file written successfully");
            } catch (IOException e) {
                logger.error("Encountered an error when creating the configuration file!", e);
            }
        }

        // Load the configuration
        try{
            FileReader reader = new FileReader(CONFIG_LOCATION);

            this.configuration = GSON.fromJson(reader, BotConfiguration.class);

            reader.close();

            logger.info("Configuration file loaded successfully");
        } catch (IOException e){
            logger.error("Encountered an error when reading the configuration file", e);
        }
    }
}

@@ -169,19 +169,161 @@
 da.core.
-JDA
+AccountType;%0Aimport net.dv8tion.jda.core.JDA;%0Aimport net.dv8tion.jda.core.JDABuilder;%0Aimport net.dv8tion.jda.core.exceptions.RateLimitedException
 ;%0Aimport
@@ -401,16 +401,65 @@
 ogger;%0A%0A
+import javax.security.auth.login.LoginException;%0A
 import j
@@ -1367,23 +1367,1525 @@
 ion();%0A%0A
-%0A    %7D%0A
+        // Connect to Discord%0A        connectToDiscord();%0A%0A    %7D%0A%0A    /**%0A     * Shuts down the robot%0A     *%0A     * @param reason   The reason why the robot is shutting down%0A     * @param exitCode An exit code to terminate with%0A     */%0A    public void shutdown(String reason, int exitCode) %7B%0A        if (reason != null && !reason.isEmpty())%0A            logger.info(%22Shutting down: %22 + reason);%0A        else%0A            logger.info(%22Shutting down%22);%0A%0A        System.exit(exitCode);%0A    %7D%0A%0A    /**%0A     * Shuts down the robot with exit code -99%0A     *%0A     * @param reason The reason why the robot is shutting down%0A     */%0A    public void shutdown(String reason) %7B%0A        shutdown(reason, -99);%0A    %7D%0A%0A    /**%0A     * Connects the robot to discord%0A     */%0A    private void connectToDiscord() %7B%0A        logger.info(%22Connecting to Discord%22);%0A        if (this.configuration.discordAPIKey == null %7C%7C this.configuration.discordAPIKey.isEmpty()) %7B%0A            logger.error(%22No API key is set, the robot will now shut down%22);%0A            this.shutdown(%22No API key set%22, -1);%0A        %7D%0A        try %7B%0A            jda = new JDABuilder(AccountType.BOT).setToken(this.configuration.discordAPIKey).buildBlocking();%0A        %7D catch (LoginException %7C InterruptedException %7C RateLimitedException e) %7B%0A            logger.error(%22Encountered an error when attempting to connect to Discord%22, e);%0A            shutdown(%22Error when connecting to Discord%22, -2);%0A        %7D%0A    %7D%0A%0A    /**%0A     * Loads the configuration from file%0A     */
 %0A    pri
@@ -3788,16 +3788,17 @@
      try
+ 
 %7B%0A      
@@ -4065,16 +4065,17 @@
 ption e)
+ 
 %7B%0A      

Connect to Discord