From 585fdb79927112b7aff6a331e7857f59b2a5f668 Mon Sep 17 00:00:00 2001 From: speed <43330808+speedxx@users.noreply.github.com> Date: Thu, 24 Dec 2020 19:48:52 -0500 Subject: [PATCH] fix verification toggle breaking everything (FS-39) --- .../totalfreedommod/discord/Discord.java | 46 ++++++++----------- 1 file changed, 20 insertions(+), 26 deletions(-) diff --git a/src/main/java/me/totalfreedom/totalfreedommod/discord/Discord.java b/src/main/java/me/totalfreedom/totalfreedommod/discord/Discord.java index aef79cb4..3f78bd10 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/discord/Discord.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/discord/Discord.java @@ -66,13 +66,14 @@ public class Discord extends FreedomService if (!verificationEnabled) { FLog.info("Discord Verification has been manually disabled."); - return; } + enabled = !Strings.isNullOrEmpty(ConfigEntry.DISCORD_TOKEN.getString()); if (!enabled) { return; } + if (bot != null) { RATELIMIT_EXECUTOR = new ScheduledThreadPoolExecutor(5, new CountingThreadFactory(this::poolIdentifier, "RateLimit")); @@ -82,6 +83,7 @@ public class Discord extends FreedomService bot.removeEventListener(object); } } + try { bot = JDABuilder.createDefault(ConfigEntry.DISCORD_TOKEN.getString()) @@ -212,13 +214,13 @@ public class Discord extends FreedomService public String generateCode(int size) { - String code = ""; + StringBuilder code = new StringBuilder(); Random random = new Random(); for (int i = 0; i < size; i++) { - code += random.nextInt(10); + code.append(random.nextInt(10)); } - return code; + return code.toString(); } public List generateEncryptedBackupCodes(List codes) @@ -240,7 +242,7 @@ public class Discord extends FreedomService for (String code : codes) { - text.append(code + "\n"); + text.append(code).append("\n"); } String fileUrl = plugin.getDataFolder().getAbsolutePath() + "/TF-Backup-Codes-" + name + ".txt"; @@ -281,18 +283,15 @@ public class Discord extends FreedomService @EventHandler(priority = EventPriority.MONITOR) public void onPlayerDeath(PlayerDeathEvent event) { - try - { - if (!event.getEntity().getWorld().getGameRuleValue(GameRule.SHOW_DEATH_MESSAGES)) - { - return; - } - } - catch (NullPointerException e) + if (!event.getEntity().getWorld().getGameRuleValue(GameRule.SHOW_DEATH_MESSAGES)) { + return; } - messageChatChannel("**" + deformat(event.getDeathMessage()) + "**"); + if (event.getDeathMessage() != null) + { + messageChatChannel("**" + deformat(event.getDeathMessage()) + "**"); + } } @Override @@ -332,14 +331,9 @@ public class Discord extends FreedomService return; } - if (message.contains("_")) - { - message = message.replace("_", "\\_"); - } - if (enabled && !chat_channel_id.isEmpty()) { - CompletableFuture sentMessage = bot.getTextChannelById(chat_channel_id).sendMessage(message).submit(true); + CompletableFuture sentMessage = bot.getTextChannelById(chat_channel_id).sendMessage(deformat(message)).submit(true); sentMessages.add(sentMessage); } } @@ -357,14 +351,9 @@ public class Discord extends FreedomService return; } - if (message.contains("_")) - { - message = message.replace("_", "\\_"); - } - if (enabled && !chat_channel_id.isEmpty()) { - CompletableFuture sentMessage = bot.getTextChannelById(chat_channel_id).sendMessage(message).submit(true); + CompletableFuture sentMessage = bot.getTextChannelById(chat_channel_id).sendMessage(deformat(message)).submit(true); sentMessages.add(sentMessage); } } @@ -394,6 +383,7 @@ public class Discord extends FreedomService { messageChatChannel("**Server has stopped**"); } + FLog.info("Discord verification bot has successfully shutdown."); } @@ -408,23 +398,27 @@ public class Discord extends FreedomService { return false; } + if (ConfigEntry.DISCORD_SERVER_ID.getString().isEmpty()) { FLog.severe("No Discord server ID was specified in the config, but there is a report channel ID."); return false; } + Guild server = bot.getGuildById(ConfigEntry.DISCORD_SERVER_ID.getString()); if (server == null) { FLog.severe("The Discord server ID specified is invalid, or the bot is not on the server."); return false; } + TextChannel channel = server.getTextChannelById(ConfigEntry.DISCORD_REPORT_CHANNEL_ID.getString()); if (channel == null) { FLog.severe("The report channel ID specified in the config is invalid."); return false; } + EmbedBuilder embedBuilder = new EmbedBuilder(); embedBuilder.setTitle("Report for " + reported.getName()); embedBuilder.setDescription(reason);