From 210e00700f656187045a0804d750fb8c2fd2ca6f Mon Sep 17 00:00:00 2001 From: JoeShimo <49585339+JoeShimo@users.noreply.github.com> Date: Mon, 4 Nov 2019 13:45:53 +0000 Subject: [PATCH] Removed JDA warning messages, Actually error out when bot fails to initialize, Update / Add entries to config without user input --- pom.xml | 15 +++ .../co/angrybee/joe/DiscordWhitelister.java | 125 +++++++++++++++--- .../co/angrybee/joe/ServerDiscordClient.java | 4 +- 3 files changed, 125 insertions(+), 19 deletions(-) diff --git a/pom.xml b/pom.xml index 0698d49..5b8a227 100755 --- a/pom.xml +++ b/pom.xml @@ -63,6 +63,21 @@ json-simple 1.1.1 + + org.apache.logging.log4j + log4j-api + 2.12.1 + + + org.apache.logging.log4j + log4j-core + 2.12.1 + + + org.apache.logging.log4j + log4j-slf4j-impl + 2.12.1 + diff --git a/src/main/java/uk/co/angrybee/joe/DiscordWhitelister.java b/src/main/java/uk/co/angrybee/joe/DiscordWhitelister.java index 99403b2..1fb60d5 100755 --- a/src/main/java/uk/co/angrybee/joe/DiscordWhitelister.java +++ b/src/main/java/uk/co/angrybee/joe/DiscordWhitelister.java @@ -78,8 +78,16 @@ public class DiscordWhitelister extends JavaPlugin serverDiscordClient.allowedToAddLimitedRoles[roles] = DiscordWhitelister.getWhitelisterBotConfig().getList("limited-add-roles").get(roles).toString(); } - serverDiscordClient.InitializeClient(botToken); - getLogger().info("Successfully initialized Discord client"); + int initializeSuccess = serverDiscordClient.InitializeClient(botToken); + + if(initializeSuccess == 0) + { + getLogger().info("Successfully initialized Discord client"); + } + else if(initializeSuccess == 1) + { + getLogger().severe("Discord Client failed to initialize, please check if your config file is valid"); + } } } @@ -197,37 +205,118 @@ public class DiscordWhitelister extends JavaPlugin e.printStackTrace(); } - if(configCreated) + // check if entries exist in config file and add them if they are not. only log entry creation if it is not first time set up + if(whitelisterBotConfigFile.exists()) { - getWhitelisterBotConfig().set("discord-bot-token", "Discord bot token goes here, you can find it here: " + - "https://discordapp.com/developers/applications/"); + if(getWhitelisterBotConfig().get("discord-bot-token") == null) + { + getWhitelisterBotConfig().set("discord-bot-token", "Discord bot token goes here, you can find it here: " + + "https://discordapp.com/developers/applications/"); + + if(!configCreated) + { + getPlugin().getLogger().warning("Entry 'discord-bot-token' was not found, adding it to the config..."); + } + } // allowed to add and remove from the whitelist - List tempAddRemoveRoles = Arrays.asList("Owner", "Admin"); - getWhitelisterBotConfig().set("add-remove-roles", tempAddRemoveRoles); + if(getWhitelisterBotConfig().get("add-remove-roles") == null) + { + List tempAddRemoveRoles = Arrays.asList("Owner", "Admin"); + getWhitelisterBotConfig().set("add-remove-roles", tempAddRemoveRoles); + + if(!configCreated) + { + getPlugin().getLogger().warning("Entry 'add-remove-roles' was not found, adding it to the config..."); + } + } // only allowed to add to the whitelist - List tempAddRoles = Arrays.asList("Mod", "Whitelister"); - getWhitelisterBotConfig().set("add-roles", tempAddRoles); + if(getWhitelisterBotConfig().get("add-roles") == null) + { + List tempAddRoles = Arrays.asList("Mod", "Whitelister"); + getWhitelisterBotConfig().set("add-roles", tempAddRoles); - getWhitelisterBotConfig().set("username-validation", true); + if(!configCreated) + { + getPlugin().getLogger().warning("Entry 'add-roles' was not found, adding it to the config..."); + } + } - getWhitelisterBotConfig().set("removed-list-enabled", true); + if(getWhitelisterBotConfig().get("username-validation") == null) + { + getWhitelisterBotConfig().set("username-validation", true); + + if(!configCreated) + { + getPlugin().getLogger().warning("Entry 'username-validation' was not found, adding it to the config..."); + } + } + + if(getWhitelisterBotConfig().get("removed-list-enabled") == null) + { + getWhitelisterBotConfig().set("removed-list-enabled", true); + + if(!configCreated) + { + getPlugin().getLogger().warning("Entry 'removed-list-enabled' was not found, adding it to the config..."); + } + } // if the limited whitelist feature should be enabled - getWhitelisterBotConfig().set("limited-whitelist-enabled", true); + if(getWhitelisterBotConfig().get("limited-whitelist-enabled") == null) + { + getWhitelisterBotConfig().set("limited-whitelist-enabled", true); + + if(!configCreated) + { + getPlugin().getLogger().warning("Entry 'limited-whitelist-enabled' was not found, adding it to the config..."); + } + } // the amount of times a non-staff user is allowed to whitelist - getWhitelisterBotConfig().set("max-whitelist-amount", 3); + if(getWhitelisterBotConfig().get("max-whitelist-amount") == null) + { + getWhitelisterBotConfig().set("max-whitelist-amount", 3); + + if(!configCreated) + { + getPlugin().getLogger().warning("Entry 'max-whitelist-amount' was not found, adding it to the config..."); + } + } // roles that are allowed whitelist a limited amount of times - List tempLimitedRoles = Arrays.asList("VIP", "LimitedWhitelister"); - getWhitelisterBotConfig().set("limited-add-roles", tempLimitedRoles); + if(getWhitelisterBotConfig().get("limited-add-roles") == null) + { + List tempLimitedRoles = Arrays.asList("VIP", "LimitedWhitelister"); + getWhitelisterBotConfig().set("limited-add-roles", tempLimitedRoles); - List tempChannelIds = Arrays.asList("445666834382061569", "488450157881327616"); - getWhitelisterBotConfig().set("target-text-channels", tempChannelIds); + if(!configCreated) + { + getPlugin().getLogger().warning("Entry 'limited-add-roles' was not found, adding it to the config..."); + } + } - getWhitelisterBotConfig().set("bot-enabled", true); + if(getWhitelisterBotConfig().get("target-text-channels") == null) + { + List tempChannelIds = Arrays.asList("445666834382061569", "488450157881327616"); + getWhitelisterBotConfig().set("target-text-channels", tempChannelIds); + + if(!configCreated) + { + getPlugin().getLogger().warning("Entry 'target-text-channels' was not found, adding it to the config..."); + } + } + + if(getWhitelisterBotConfig().get("bot-enabled") == null) + { + getWhitelisterBotConfig().set("bot-enabled", true); + + if(!configCreated) + { + getPlugin().getLogger().warning("Entry 'bot-enabled' was not found, adding it to the config..."); + } + } try { diff --git a/src/main/java/uk/co/angrybee/joe/ServerDiscordClient.java b/src/main/java/uk/co/angrybee/joe/ServerDiscordClient.java index e9e6f8d..44830e3 100755 --- a/src/main/java/uk/co/angrybee/joe/ServerDiscordClient.java +++ b/src/main/java/uk/co/angrybee/joe/ServerDiscordClient.java @@ -30,7 +30,7 @@ public class ServerDiscordClient extends ListenerAdapter final char[] validCharacters = {'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'z', 'x', 'c', 'v', 'b', 'n', 'm', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '_'}; - public void InitializeClient(String clientToken) + public int InitializeClient(String clientToken) { try { @@ -39,10 +39,12 @@ public class ServerDiscordClient extends ListenerAdapter .addEventListeners(new ServerDiscordClient()) .build(); javaDiscordAPI.awaitReady(); + return 0; } catch(LoginException | InterruptedException e) { e.printStackTrace(); + return 1; } }