From 87eac4114f5d6ee4df91d91e15d2534b44a22615 Mon Sep 17 00:00:00 2001 From: thmsdy Date: Sun, 3 Mar 2019 12:56:49 -0600 Subject: [PATCH] Add more features and improve multiserver compatability --- dependency-reduced-pom.xml | 2 +- pom.xml | 2 +- src/main/java/com/fpghoti/bcraftbot/Main.java | 23 +++++++++- .../com/fpghoti/bcraftbot/bot/ServerBot.java | 4 ++ .../bcraftbot/bot/command/AddMeCommand.java | 18 +++++--- .../bcraftbot/listener/PlayerListener.java | 45 +++++++++++-------- src/main/resources/plugin.yml | 2 +- 7 files changed, 68 insertions(+), 28 deletions(-) diff --git a/dependency-reduced-pom.xml b/dependency-reduced-pom.xml index 8095fa9..6b7f5d1 100644 --- a/dependency-reduced-pom.xml +++ b/dependency-reduced-pom.xml @@ -3,7 +3,7 @@ 4.0.0 com.fpghoti BCraftBot - 1.0 + 1.1 diff --git a/pom.xml b/pom.xml index a80af22..4a48daf 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 com.fpghoti BCraftBot - 1.0 + 1.1 jcenter diff --git a/src/main/java/com/fpghoti/bcraftbot/Main.java b/src/main/java/com/fpghoti/bcraftbot/Main.java index 7ee4061..27a357e 100644 --- a/src/main/java/com/fpghoti/bcraftbot/Main.java +++ b/src/main/java/com/fpghoti/bcraftbot/Main.java @@ -35,6 +35,9 @@ public class Main extends JavaPlugin { private boolean stopTimer = false; private boolean assignrole = false; private boolean checkrole = false; + private boolean outputtodiscord = false; + private boolean mainrecordkeeper = false; + private int mysqlTimer = 1140; @@ -115,7 +118,7 @@ public class Main extends JavaPlugin { FileConfiguration config = this.getConfig(); public void messageSet(){ - config.options().header("If CheckRole is true, users will need the Discord role listed in RequiredRole to join the server. AssignRole tells the bot whether or not it should give users the role specified in RoleName upon joining the server.\nIf you wish to limit the bot commands to a specific channel, edit its permissions accordingly."); + config.options().header("If CheckRole is true, users will need the Discord role listed in RequiredRole to join the server. AssignRole tells the bot whether or not it should give users the role specified in RoleName upon joining the server.\nIf you wish to limit the bot commands to a specific channel, edit its permissions accordingly.\nMainRecordKeeper tells the bot if it is the main record keeper. If you are using the same database for multiple servers, you will only want one bot to insert new users into the database."); if (config.get("Host") == null){ config.createSection("Host"); config.set("Host", "0.0.0.0"); @@ -172,6 +175,14 @@ public class Main extends JavaPlugin { config.createSection("RoleName"); config.set("RoleName", "Craftee"); } + if (config.get("OutputToDiscord") == null){ + config.createSection("OutputToDiscord"); + config.set("OutputToDiscord", true); + } + if (config.get("MainRecordKeeper") == null){ + config.createSection("MainRecordKeeper"); + config.set("MainRecordKeeper", true); + } this.saveConfig(); } @@ -189,6 +200,8 @@ public class Main extends JavaPlugin { kickmsg = config.getString("KickMessage"); assignrole = config.getBoolean("AssignRole"); rolename = config.getString("RoleName"); + outputtodiscord = config.getBoolean("OutputToDiscord"); + mainrecordkeeper = config.getBoolean("MainRecordKeeper"); } public void setExemptList(String s) { @@ -250,6 +263,14 @@ public class Main extends JavaPlugin { return sql; } + public boolean outputToDiscord() { + return outputtodiscord; + } + + public boolean mainRecordKeeper() { + return mainrecordkeeper; + } + public boolean isMember(Player p) { String name = p.getName().toLowerCase(); ArrayList ids = new ArrayList(); diff --git a/src/main/java/com/fpghoti/bcraftbot/bot/ServerBot.java b/src/main/java/com/fpghoti/bcraftbot/bot/ServerBot.java index fd44f6a..f42b2f1 100644 --- a/src/main/java/com/fpghoti/bcraftbot/bot/ServerBot.java +++ b/src/main/java/com/fpghoti/bcraftbot/bot/ServerBot.java @@ -87,4 +87,8 @@ public class ServerBot { jda.shutdownNow(); } + public Main getPlugin() { + return plugin; + } + } diff --git a/src/main/java/com/fpghoti/bcraftbot/bot/command/AddMeCommand.java b/src/main/java/com/fpghoti/bcraftbot/bot/command/AddMeCommand.java index 258861a..ccd31f5 100644 --- a/src/main/java/com/fpghoti/bcraftbot/bot/command/AddMeCommand.java +++ b/src/main/java/com/fpghoti/bcraftbot/bot/command/AddMeCommand.java @@ -31,12 +31,20 @@ public class AddMeCommand implements Command{ return; } bot.log(Level.INFO, event.getAuthor().getName() + " issued a Discord Bot command: !addme " + name); - if(bot.getSQL().itemExists("DiscordID", event.getAuthor().getId(), bot.getTableName())) { - bot.getSQL().set("MinecraftName", name, "DiscordID", "=", event.getAuthor().getId(), bot.getTableName()); - }else { - bot.getSQL().update("INSERT INTO " + bot.getTableName() + " (DiscordID,MinecraftName) VALUES (\'" + event.getAuthor().getId() + "\',\'" + name + "\');"); + + if(bot.getPlugin().mainRecordKeeper()) { + + if(bot.getSQL().itemExists("DiscordID", event.getAuthor().getId(), bot.getTableName())) { + bot.getSQL().set("MinecraftName", name, "DiscordID", "=", event.getAuthor().getId(), bot.getTableName()); + }else { + bot.getSQL().update("INSERT INTO " + bot.getTableName() + " (DiscordID,MinecraftName) VALUES (\'" + event.getAuthor().getId() + "\',\'" + name + "\');"); + } + + } + + if(bot.getPlugin().outputToDiscord()) { + event.getChannel().sendMessage("Updating Minecraft user database with username **" + args[0] + "**.").queue(); } - event.getChannel().sendMessage("Updating Minecraft user database with username **" + args[0] + "**.").queue(); } } diff --git a/src/main/java/com/fpghoti/bcraftbot/listener/PlayerListener.java b/src/main/java/com/fpghoti/bcraftbot/listener/PlayerListener.java index c194eca..645ace8 100644 --- a/src/main/java/com/fpghoti/bcraftbot/listener/PlayerListener.java +++ b/src/main/java/com/fpghoti/bcraftbot/listener/PlayerListener.java @@ -25,40 +25,47 @@ public class PlayerListener implements Listener { public void onPlayerLogin(PlayerLoginEvent event){ Player player = event.getPlayer(); + if(player.hasPermission("bcraftbot.bypasscheck")) { + return; + } + if(!plugin.isMember(player) && !plugin.isExempt(player.getName())) { event.disallow(Result.KICK_OTHER, plugin.getKickMessage()); return; } - + if(plugin.checkRole() && !plugin.isExempt(player.getName())) { boolean allow = false; + for(Guild guild : plugin.getBot().getJDA().getGuilds()) { - Role role = null; - for(Role r : guild.getRoles()) { - if(r.getName().equalsIgnoreCase(plugin.getRequiredRole())) { - role = r; + for(String rolename : plugin.getRequiredRole().split(",")) { + + Role role = null; + for(Role r : guild.getRoles()) { + if(r.getName().equalsIgnoreCase(rolename)) { + role = r; + } + } + + User user = plugin.getDiscordUser(player); + Member mem = guild.getMember(user); + + if(user != null && user.getMutualGuilds().contains(guild) && role != null) { + if(mem.getRoles().contains(role)) { + allow = true; + } } } - - User user = plugin.getDiscordUser(player); - Member mem = guild.getMember(user); - - if(user != null && user.getMutualGuilds().contains(guild) && role != null) { - if(mem.getRoles().contains(role)) { - allow = true; - } - } - } - + if(!allow) { event.disallow(Result.KICK_OTHER, plugin.getKickMessage()); return; } - + } - + if(plugin.assignRole() && !plugin.isExempt(player.getName())) { for(Guild guild : plugin.getBot().getJDA().getGuilds()) { @@ -80,5 +87,5 @@ public class PlayerListener implements Listener { } } - + } diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 8d5ce1a..eccaa81 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,5 +1,5 @@ main: com.fpghoti.bcraftbot.Main -version: 1.0 +version: 1.1 api-version: 1.13 name: BCraftBot descripion: Discord-based whitelisting system for Minecraft