Implement option to require a specific role to join
This commit is contained in:
parent
147620f3d6
commit
653d332f31
|
@ -30,9 +30,11 @@ public class Main extends JavaPlugin {
|
||||||
private String bottoken;
|
private String bottoken;
|
||||||
private String kickmsg;
|
private String kickmsg;
|
||||||
private String rolename;
|
private String rolename;
|
||||||
|
private String requiredrole;
|
||||||
|
|
||||||
private boolean stopTimer = false;
|
private boolean stopTimer = false;
|
||||||
private boolean assignrole;
|
private boolean assignrole = false;
|
||||||
|
private boolean checkrole = false;
|
||||||
|
|
||||||
private int mysqlTimer = 1140;
|
private int mysqlTimer = 1140;
|
||||||
|
|
||||||
|
@ -63,7 +65,24 @@ public class Main extends JavaPlugin {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onDisable() {
|
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;
|
stopTimer = true;
|
||||||
sql.disconnect();
|
sql.disconnect();
|
||||||
}
|
}
|
||||||
|
@ -96,6 +115,7 @@ public class Main extends JavaPlugin {
|
||||||
FileConfiguration config = this.getConfig();
|
FileConfiguration config = this.getConfig();
|
||||||
|
|
||||||
public void messageSet(){
|
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){
|
if (config.get("Host") == null){
|
||||||
config.createSection("Host");
|
config.createSection("Host");
|
||||||
config.set("Host", "0.0.0.0");
|
config.set("Host", "0.0.0.0");
|
||||||
|
@ -124,6 +144,14 @@ public class Main extends JavaPlugin {
|
||||||
config.createSection("Bot-Token");
|
config.createSection("Bot-Token");
|
||||||
config.set("Bot-Token", "inserttokenhere");
|
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){
|
if (config.get("ExemptUsernames") == null){
|
||||||
config.createSection("ExemptUsernames");
|
config.createSection("ExemptUsernames");
|
||||||
config.set("ExemptUsernames", "test1,test2,test3");
|
config.set("ExemptUsernames", "test1,test2,test3");
|
||||||
|
@ -155,6 +183,8 @@ public class Main extends JavaPlugin {
|
||||||
sqldatabase = config.getString("Database");
|
sqldatabase = config.getString("Database");
|
||||||
sqltable = config.getString("TableName");
|
sqltable = config.getString("TableName");
|
||||||
bottoken = config.getString("Bot-Token");
|
bottoken = config.getString("Bot-Token");
|
||||||
|
checkrole = config.getBoolean("CheckRole");
|
||||||
|
requiredrole = config.getString("RequiredRole");
|
||||||
setExemptList(config.getString("ExemptUsernames"));
|
setExemptList(config.getString("ExemptUsernames"));
|
||||||
kickmsg = config.getString("KickMessage");
|
kickmsg = config.getString("KickMessage");
|
||||||
assignrole = config.getBoolean("AssignRole");
|
assignrole = config.getBoolean("AssignRole");
|
||||||
|
@ -166,12 +196,16 @@ public class Main extends JavaPlugin {
|
||||||
for(String item: s.split(",")) {
|
for(String item: s.split(",")) {
|
||||||
exempt.add(item);
|
exempt.add(item);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getTableName() {
|
public String getTableName() {
|
||||||
return sqltable;
|
return sqltable;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean checkRole() {
|
||||||
|
return checkrole;
|
||||||
|
}
|
||||||
|
|
||||||
public void addExempt(String username) {
|
public void addExempt(String username) {
|
||||||
String updated = config.getString("ExemptUsernames") + "," + username;
|
String updated = config.getString("ExemptUsernames") + "," + username;
|
||||||
if (config.get("ExemptUsernames") != null){
|
if (config.get("ExemptUsernames") != null){
|
||||||
|
@ -272,6 +306,10 @@ public class Main extends JavaPlugin {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getRequiredRole() {
|
||||||
|
return requiredrole;
|
||||||
|
}
|
||||||
|
|
||||||
public User getDiscordUser(Player p) {
|
public User getDiscordUser(Player p) {
|
||||||
String name = p.getName().toLowerCase();
|
String name = p.getName().toLowerCase();
|
||||||
ArrayList<String> ids = new ArrayList<String>();
|
ArrayList<String> ids = new ArrayList<String>();
|
||||||
|
|
|
@ -84,7 +84,7 @@ public class ServerBot {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void shutDown(){
|
public void shutDown(){
|
||||||
jda.shutdown();
|
jda.shutdownNow();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,9 +29,37 @@ public class PlayerListener implements Listener {
|
||||||
event.disallow(Result.KICK_OTHER, plugin.getKickMessage());
|
event.disallow(Result.KICK_OTHER, plugin.getKickMessage());
|
||||||
return;
|
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())) {
|
if(plugin.assignRole() && !plugin.isExempt(player.getName())) {
|
||||||
|
|
||||||
|
|
||||||
for(Guild guild : plugin.getBot().getJDA().getGuilds()) {
|
for(Guild guild : plugin.getBot().getJDA().getGuilds()) {
|
||||||
|
|
||||||
Role role = null;
|
Role role = null;
|
||||||
|
|
Loading…
Reference in New Issue