From 653d332f31a2c4166dec6676e1cfe6e0fa96241c Mon Sep 17 00:00:00 2001 From: thmsdy Date: Fri, 15 Feb 2019 12:41:48 -0600 Subject: [PATCH] Implement option to require a specific role to join --- src/main/java/com/fpghoti/bcraftbot/Main.java | 44 +++++++++++++++++-- .../com/fpghoti/bcraftbot/bot/ServerBot.java | 2 +- .../bcraftbot/listener/PlayerListener.java | 32 +++++++++++++- 3 files changed, 72 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/fpghoti/bcraftbot/Main.java b/src/main/java/com/fpghoti/bcraftbot/Main.java index 930f145..7ee4061 100644 --- a/src/main/java/com/fpghoti/bcraftbot/Main.java +++ b/src/main/java/com/fpghoti/bcraftbot/Main.java @@ -30,9 +30,11 @@ public class Main extends JavaPlugin { private String bottoken; private String kickmsg; private String rolename; + private String requiredrole; private boolean stopTimer = false; - private boolean assignrole; + private boolean assignrole = false; + private boolean checkrole = false; private int mysqlTimer = 1140; @@ -63,7 +65,24 @@ public class Main extends JavaPlugin { } public void onDisable() { - bot.shutDown(); + try { + + bot.shutDown(); + + }catch(NoClassDefFoundError e) { + + // This Should only be an issue when the user replaces the .jar file before shutting the server down. + // The reason for catching this is to provide information to the user. + + log(Level.WARNING, "The plugin .jar file has been removed or replaced since the plugin has been loaded! Everything MIGHT work just fine, but this is not encouraged, and you will not get support for any issues that come from this."); + } + long initial = System.currentTimeMillis(); + + //Give bot enough time to shut down + while(true) { + long current = System.currentTimeMillis(); + if(current - initial >= 1000) break; + } stopTimer = true; sql.disconnect(); } @@ -96,6 +115,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."); if (config.get("Host") == null){ config.createSection("Host"); config.set("Host", "0.0.0.0"); @@ -124,6 +144,14 @@ public class Main extends JavaPlugin { config.createSection("Bot-Token"); config.set("Bot-Token", "inserttokenhere"); } + if (config.get("CheckRole") == null){ + config.createSection("CheckRole"); + config.set("CheckRole", false); + } + if (config.get("RequiredRole") == null){ + config.createSection("RequiredRole"); + config.set("RequiredRole", "Whitelisted"); + } if (config.get("ExemptUsernames") == null){ config.createSection("ExemptUsernames"); config.set("ExemptUsernames", "test1,test2,test3"); @@ -155,6 +183,8 @@ public class Main extends JavaPlugin { sqldatabase = config.getString("Database"); sqltable = config.getString("TableName"); bottoken = config.getString("Bot-Token"); + checkrole = config.getBoolean("CheckRole"); + requiredrole = config.getString("RequiredRole"); setExemptList(config.getString("ExemptUsernames")); kickmsg = config.getString("KickMessage"); assignrole = config.getBoolean("AssignRole"); @@ -166,12 +196,16 @@ public class Main extends JavaPlugin { for(String item: s.split(",")) { exempt.add(item); } - } + } public String getTableName() { return sqltable; } + public boolean checkRole() { + return checkrole; + } + public void addExempt(String username) { String updated = config.getString("ExemptUsernames") + "," + username; if (config.get("ExemptUsernames") != null){ @@ -272,6 +306,10 @@ public class Main extends JavaPlugin { return false; } + public String getRequiredRole() { + return requiredrole; + } + public User getDiscordUser(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 db770aa..fd44f6a 100644 --- a/src/main/java/com/fpghoti/bcraftbot/bot/ServerBot.java +++ b/src/main/java/com/fpghoti/bcraftbot/bot/ServerBot.java @@ -84,7 +84,7 @@ public class ServerBot { } public void shutDown(){ - jda.shutdown(); + jda.shutdownNow(); } } diff --git a/src/main/java/com/fpghoti/bcraftbot/listener/PlayerListener.java b/src/main/java/com/fpghoti/bcraftbot/listener/PlayerListener.java index 9edea9f..c194eca 100644 --- a/src/main/java/com/fpghoti/bcraftbot/listener/PlayerListener.java +++ b/src/main/java/com/fpghoti/bcraftbot/listener/PlayerListener.java @@ -29,9 +29,37 @@ public class PlayerListener implements Listener { 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; + } + } + + 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()) { Role role = null;