Skip to content
This repository has been archived by the owner on Jun 13, 2024. It is now read-only.

Commit

Permalink
add error loger
Browse files Browse the repository at this point in the history
  • Loading branch information
Norbit4 committed Aug 27, 2022
1 parent 882d775 commit 79a7947
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 31 deletions.
88 changes: 60 additions & 28 deletions src/main/java/pl/norbit/discordmc/DiscordMc.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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;

Expand All @@ -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);
Expand All @@ -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
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/pl/norbit/discordmc/bot/DiscordBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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");
}
}

Expand All @@ -35,6 +37,8 @@ public JDA getJda() {
}

public void close(){
jda.shutdown();
if(jda != null) {
jda.shutdownNow();
}
}
}
1 change: 1 addition & 0 deletions src/main/java/pl/norbit/discordmc/sync/SyncManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down

0 comments on commit 79a7947

Please sign in to comment.