From 79a79472e3a08389577f8dc7b1b8bd697eea1307 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Norbert=20=C5=9Aleziak?= Date: Sun, 28 Aug 2022 00:51:05 +0200 Subject: [PATCH] add error loger --- .../java/pl/norbit/discordmc/DiscordMc.java | 88 +++++++++++++------ .../pl/norbit/discordmc/bot/DiscordBot.java | 10 ++- .../pl/norbit/discordmc/sync/SyncManager.java | 1 + 3 files changed, 68 insertions(+), 31 deletions(-) diff --git a/src/main/java/pl/norbit/discordmc/DiscordMc.java b/src/main/java/pl/norbit/discordmc/DiscordMc.java index 3d76a4d..ba37ce0 100644 --- a/src/main/java/pl/norbit/discordmc/DiscordMc.java +++ b/src/main/java/pl/norbit/discordmc/DiscordMc.java @@ -1,9 +1,9 @@ package pl.norbit.discordmc; +import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.entities.MessageChannel; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.core.Logger; -import org.bukkit.ChatColor; import org.bukkit.command.ConsoleCommandSender; import org.bukkit.plugin.java.JavaPlugin; import pl.norbit.discordmc.bot.DiscordBot; @@ -19,7 +19,7 @@ import pl.norbit.discordmc.serverinfo.InfoUpdater; import pl.norbit.discordmc.sync.SyncTimerTask; - +import static pl.norbit.discordmc.server.config.PluginConfig.CHAT_MODULE; public final class DiscordMc extends JavaPlugin { private DiscordBot discordBot; @@ -31,6 +31,18 @@ public final class DiscordMc extends JavaPlugin { @Override public void onEnable() { + boolean start = false; + try { + start = start(); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + + if(!start){ + disablePlugin(); + } + } + private boolean start() throws InterruptedException { commandSender = getServer().getConsoleSender(); javaPlugin = this; @@ -43,40 +55,37 @@ public void onEnable() { discordBot = new DiscordBot(PluginConfig.TOKEN, this); discordBot.start(); + + JDA jda = discordBot.getJda(); + + if(discordBot.getJda() == null) return false; + + if(jda.awaitReady().getGuildById(PluginConfig.SERVER_ID) == null) { + sendMessage("&c[ERROR] Wrong discord server ID"); + sendMessage("&cHow to get discord server id?"); + sendMessage("&chttps://github.com/Norbit4/DiscordMc/wiki/Configuration#id"); + + return false; + } + timeServer = System.currentTimeMillis(); - PluginDBManager.init(discordBot.getJda(), this); + PluginDBManager.init(jda, this); + if(PluginConfig.SYNC_RANK_ENABLE) { - SyncManager.init(discordBot.getJda()); + SyncManager.init(jda); } //console module if(PluginConfig.CONSOLE_MODULE){ - MessageChannel messageChannel = null; - try { - messageChannel = discordBot.getJda().awaitReady().getTextChannelById(PluginConfig.CONSOLE_CHANNEL_ID); - } catch (InterruptedException e) { - e.printStackTrace(); - } - - if(messageChannel == null){ - - this.getPluginLoader().disablePlugin(this); - } + boolean b = checkDiscordChannel(PluginConfig.CONSOLE_CHANNEL_ID, "CONSOLE_MODULE"); + if(!b) return false; } //chat module - if(PluginConfig.CHAT_MODULE){ - MessageChannel messageChannel = null; - try { - messageChannel = discordBot.getJda().awaitReady().getTextChannelById(PluginConfig.CHAT_CHANNEL_ID); - } catch (InterruptedException e) { - e.printStackTrace(); - } - - if(messageChannel == null){ - this.getPluginLoader().disablePlugin(this); - } + if(CHAT_MODULE){ + boolean b = checkDiscordChannel(PluginConfig.CHAT_CHANNEL_ID, "CHAT_MODULE"); + if(!b) return false; } new CommandManager(discordBot.getJda(), this); @@ -98,15 +107,38 @@ public void onEnable() { //info module if(PluginConfig.DISCORD_INFO_MODULE){ + boolean b = checkDiscordChannel(PluginConfig.CHANNEL_INFO_ID, "INFO_MODULE"); + + if(!b) return false; + InfoUpdater.start(discordBot.getJda(), this); } SyncTimerTask.runTaskTimer(this); } else { - getServer().getConsoleSender().sendMessage(ChatColor.RED + "Enable plugin in config.yml"); - getServer().getPluginManager().disablePlugin(this); + sendMessage("&cEnable plugin in config.yml"); + disablePlugin(); } + return true; + } + + private boolean checkDiscordChannel(String channelID, String module){ + + MessageChannel messageChannel = null; + try { + messageChannel = discordBot.getJda().awaitReady().getTextChannelById(channelID); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + if(messageChannel == null){ + sendMessage("&c[ERROR] Wrong discord channel ID -> " + module); + sendMessage("&cHow to get discord channel id?"); + sendMessage("&chttps://github.com/Norbit4/DiscordMc/wiki/Configuration#id"); + } + + return messageChannel != null; } @Override diff --git a/src/main/java/pl/norbit/discordmc/bot/DiscordBot.java b/src/main/java/pl/norbit/discordmc/bot/DiscordBot.java index 55bcd28..af1bafe 100644 --- a/src/main/java/pl/norbit/discordmc/bot/DiscordBot.java +++ b/src/main/java/pl/norbit/discordmc/bot/DiscordBot.java @@ -2,8 +2,8 @@ import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.JDABuilder; -import org.bukkit.ChatColor; import org.bukkit.plugin.java.JavaPlugin; +import pl.norbit.discordmc.DiscordMc; import pl.norbit.discordmc.bot.builder.BotBuilder; import javax.security.auth.login.LoginException; @@ -22,7 +22,9 @@ public void start(){ try { jda = jdaBuilder.build(); } catch (LoginException e) { - javaPlugin.getServer().getConsoleSender().sendMessage(ChatColor.RED + "[ERROR] Wrong token!"); + DiscordMc.sendMessage("&c[ERROR] Wrong token!"); + DiscordMc.sendMessage("&cHow to get discord bot token?"); + DiscordMc.sendMessage("&chttps://github.com/Norbit4/DiscordMc/wiki/Configuration#bot-token"); } } @@ -35,6 +37,8 @@ public JDA getJda() { } public void close(){ - jda.shutdown(); + if(jda != null) { + jda.shutdownNow(); + } } } diff --git a/src/main/java/pl/norbit/discordmc/sync/SyncManager.java b/src/main/java/pl/norbit/discordmc/sync/SyncManager.java index 5f888ca..2befcc1 100644 --- a/src/main/java/pl/norbit/discordmc/sync/SyncManager.java +++ b/src/main/java/pl/norbit/discordmc/sync/SyncManager.java @@ -55,6 +55,7 @@ public static void changeToMinecraftName(Player p, String discordUUID){ Guild guild = jda.getGuildById(PluginConfig.SERVER_ID); Member member = guild.retrieveMemberById(discordUUID).complete(); + if(!member.isOwner()) { member.modifyNickname(minecraftNick).queue(); }