From fa5c9371f1c7be4a4861e7a354824c9a3d168f85 Mon Sep 17 00:00:00 2001 From: thmsdy Date: Mon, 22 Jun 2020 01:40:07 -0500 Subject: [PATCH] bug fixes, Reaction Toggle Role, Booster Exclusive roles --- dependency-reduced-pom.xml | 2 +- pom.xml | 2 +- src/main/java/com/fpghoti/biscuit/Main.java | 5 + .../commands/client/NotSpammerCommand.java | 2 +- .../commands/client/SoftMuteCommand.java | 2 +- .../commands/client/ToggleRoleCommand.java | 42 +++++---- .../biscuit/commands/client/UIDCommand.java | 22 ++--- .../commands/client/UnSoftMuteCommand.java | 2 +- .../biscuit/config/ConfigRetrieval.java | 12 +-- .../biscuit/config/PropertiesRetrieval.java | 91 +++++++++++++++++-- .../listener/MessageReceiveListener.java | 2 + .../biscuit/listener/ReactionListener.java | 91 +++++++++++++++++++ .../biscuit/listener/RoleListener.java | 20 ++++ .../com/fpghoti/biscuit/user/PreUser.java | 1 - .../com/fpghoti/biscuit/util/PermUtil.java | 52 +++++++++-- .../java/com/fpghoti/biscuit/util/Util.java | 9 ++ src/main/resources/config.properties | 16 ++++ 17 files changed, 317 insertions(+), 56 deletions(-) create mode 100644 src/main/java/com/fpghoti/biscuit/listener/ReactionListener.java create mode 100644 src/main/java/com/fpghoti/biscuit/listener/RoleListener.java diff --git a/dependency-reduced-pom.xml b/dependency-reduced-pom.xml index e83832a..901ba2f 100644 --- a/dependency-reduced-pom.xml +++ b/dependency-reduced-pom.xml @@ -3,7 +3,7 @@ 4.0.0 com.fpghoti Biscuit - 1.3 + 1.4 src/main/java diff --git a/pom.xml b/pom.xml index acfbbbd..844db07 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ 4.0.0 com.fpghoti Biscuit - 1.3 + 1.4 src/main/java diff --git a/src/main/java/com/fpghoti/biscuit/Main.java b/src/main/java/com/fpghoti/biscuit/Main.java index 32f1c5b..5894278 100644 --- a/src/main/java/com/fpghoti/biscuit/Main.java +++ b/src/main/java/com/fpghoti/biscuit/Main.java @@ -32,6 +32,8 @@ import com.fpghoti.biscuit.listener.JoinListener; import com.fpghoti.biscuit.listener.MessageDeleteListener; import com.fpghoti.biscuit.listener.MessageEditListener; import com.fpghoti.biscuit.listener.MessageReceiveListener; +import com.fpghoti.biscuit.listener.ReactionListener; +import com.fpghoti.biscuit.listener.RoleListener; import com.fpghoti.biscuit.timer.task.BotMsgRemoveTimer; import com.fpghoti.biscuit.timer.task.ChatCountTimer; import com.fpghoti.biscuit.timer.task.DecrementTimer; @@ -72,6 +74,8 @@ public class Main { jda.addEventListener(new MessageDeleteListener()); jda.addEventListener(new JoinListener()); jda.addEventListener(new DMListener()); + jda.addEventListener(new ReactionListener()); + jda.addEventListener(new RoleListener()); String link = "NULL"; @@ -119,6 +123,7 @@ public class Main { ready = true; } + @SuppressWarnings("deprecation") private static void startJDA() { String token = PropertiesRetrieval.getToken(); log.info("Connecting bot to Discord."); diff --git a/src/main/java/com/fpghoti/biscuit/commands/client/NotSpammerCommand.java b/src/main/java/com/fpghoti/biscuit/commands/client/NotSpammerCommand.java index 1061457..ddc443c 100644 --- a/src/main/java/com/fpghoti/biscuit/commands/client/NotSpammerCommand.java +++ b/src/main/java/com/fpghoti/biscuit/commands/client/NotSpammerCommand.java @@ -29,7 +29,7 @@ public class NotSpammerCommand extends ClientCommand{ for(Member m : event.getMessage().getMentionedMembers()){ User u = m.getUser(); String s = u.getAsMention(); - if(event.getChannel().getName().equals("public-spam-test") || (PermUtil.isMod(event.getMember()) || PermUtil.canMute(event.getMember()))) { + if(event.getChannel().getName().equals("public-spam-test") || (PermUtil.isMod(event.getMember()))) { SpamRecords.spammers.remove(u); event.getTextChannel().sendMessage(s+ " is no longer flagged as spam.").queue(); } diff --git a/src/main/java/com/fpghoti/biscuit/commands/client/SoftMuteCommand.java b/src/main/java/com/fpghoti/biscuit/commands/client/SoftMuteCommand.java index 2ea298a..541eeed 100644 --- a/src/main/java/com/fpghoti/biscuit/commands/client/SoftMuteCommand.java +++ b/src/main/java/com/fpghoti/biscuit/commands/client/SoftMuteCommand.java @@ -29,7 +29,7 @@ public class SoftMuteCommand extends ClientCommand{ for(Member m : event.getMessage().getMentionedMembers()){ User u = m.getUser(); String s = u.getAsMention(); - if(event.getChannel().getName().equals("public-softmute-test") || (PermUtil.isMod(event.getMember()) || PermUtil.canMute(event.getMember()))) { + if(event.getChannel().getName().equals("public-softmute-test") || (PermUtil.isMod(event.getMember()))) { SpamRecords.softmute.add(u); u.openPrivateChannel().queue(); event.getTextChannel().sendMessage(s+ " is now soft-muted. They will now be only able to send one message every two minutes.").queue(); diff --git a/src/main/java/com/fpghoti/biscuit/commands/client/ToggleRoleCommand.java b/src/main/java/com/fpghoti/biscuit/commands/client/ToggleRoleCommand.java index 2c905d9..7b0b289 100644 --- a/src/main/java/com/fpghoti/biscuit/commands/client/ToggleRoleCommand.java +++ b/src/main/java/com/fpghoti/biscuit/commands/client/ToggleRoleCommand.java @@ -12,35 +12,35 @@ import net.dv8tion.jda.api.events.message.MessageReceivedEvent; public class ToggleRoleCommand extends ClientCommand{ - public ToggleRoleCommand() { - name = "ToggleRole"; - description = "Toggles specified role on/off"; - usage = PropertiesRetrieval.getCommandSignifier() + "togglerole "; - minArgs = 1; - maxArgs = 1; - identifiers.add("togglerole"); - identifiers.add("tr"); - } - + public ToggleRoleCommand() { + name = "ToggleRole"; + description = "Toggles specified role on/off"; + usage = PropertiesRetrieval.getCommandSignifier() + "togglerole "; + minArgs = 1; + maxArgs = 1; + identifiers.add("togglerole"); + identifiers.add("tr"); + } + @Override public void execute(String[] args, MessageReceivedEvent event) { Biscuit b = Main.getBiscuit(); if(!event.getAuthor().isBot()) { b.log(event.getAuthor().getName() + " issued a command: -togglerole " + args[0]); - + String rolename = ""; - + for(String s : PropertiesRetrieval.getToggleRoles()) { if(s.equalsIgnoreCase(args[0])) { rolename = s; } } - + if(rolename.equals("")) { event.getTextChannel().sendMessage("Sorry! This role either cannot be toggled or does not exist!").queue(); return; } - + Role role = null; for(Role r : event.getGuild().getRoles()) { if(r.getName().toLowerCase().equalsIgnoreCase(rolename)) { @@ -51,7 +51,7 @@ public class ToggleRoleCommand extends ClientCommand{ b.error("Cannot find role!"); return; } - + Emote done = null; for(Emote e : event.getGuild().getEmotes()) { if(e.getName().contains(PropertiesRetrieval.getDoneEmote())) { @@ -65,7 +65,17 @@ public class ToggleRoleCommand extends ClientCommand{ if(PermUtil.hasRole(event.getMember(), role)){ event.getGuild().removeRoleFromMember(event.getMember(),role).queue(); }else { - event.getGuild().addRoleToMember(event.getMember(), role).queue(); + boolean canAdd = false; + if(PermUtil.isBoosterExclusive(role)) { + if(PermUtil.isBooster(event.getMember())) { + canAdd = true; + } + }else { + canAdd = true; + } + if(canAdd) { + event.getGuild().addRoleToMember(event.getMember(), role).queue(); + } } event.getMessage().addReaction(done).queue(); } diff --git a/src/main/java/com/fpghoti/biscuit/commands/client/UIDCommand.java b/src/main/java/com/fpghoti/biscuit/commands/client/UIDCommand.java index 02c0995..c1449f5 100644 --- a/src/main/java/com/fpghoti/biscuit/commands/client/UIDCommand.java +++ b/src/main/java/com/fpghoti/biscuit/commands/client/UIDCommand.java @@ -12,14 +12,14 @@ import net.dv8tion.jda.api.events.message.MessageReceivedEvent; public class UIDCommand extends ClientCommand{ - public UIDCommand() { - name = "User ID"; - description = "Retrieves a user's ID."; - usage = PropertiesRetrieval.getCommandSignifier() + "uid @"; - minArgs = 1; - maxArgs = 1; - identifiers.add("uid"); - } + public UIDCommand() { + name = "User ID"; + description = "Retrieves a user's ID."; + usage = PropertiesRetrieval.getCommandSignifier() + "uid @"; + minArgs = 1; + maxArgs = 1; + identifiers.add("uid"); + } @Override public void execute(String[] args, MessageReceivedEvent event) { @@ -27,9 +27,9 @@ public class UIDCommand extends ClientCommand{ b.log(event.getAuthor().getName() + " issued a command: -uid " + args[0]); for(Member m : event.getMessage().getMentionedMembers()){ User u = m.getUser(); - String s = u.getAsMention(); - if(PermUtil.isMod(event.getMember()) || PermUtil.canMute(event.getMember())) - event.getTextChannel().sendMessage("DEBUG: " + s+ " retrieved.").queue(); + if(PermUtil.isMod(event.getMember())) { + event.getTextChannel().sendMessage(u.getId()).queue(); + } } } diff --git a/src/main/java/com/fpghoti/biscuit/commands/client/UnSoftMuteCommand.java b/src/main/java/com/fpghoti/biscuit/commands/client/UnSoftMuteCommand.java index 68969fd..52473d1 100644 --- a/src/main/java/com/fpghoti/biscuit/commands/client/UnSoftMuteCommand.java +++ b/src/main/java/com/fpghoti/biscuit/commands/client/UnSoftMuteCommand.java @@ -29,7 +29,7 @@ public class UnSoftMuteCommand extends ClientCommand{ for(Member m : event.getMessage().getMentionedMembers()){ User u = m.getUser(); String s = u.getAsMention(); - if(event.getChannel().getName().equals("public-softmute-test") || (PermUtil.isMod(event.getMember()) || PermUtil.canMute(event.getMember()))) { + if(event.getChannel().getName().equals("public-softmute-test") || (PermUtil.isMod(event.getMember()))) { SpamRecords.softmute.remove(u); event.getTextChannel().sendMessage(s+ " is no longer soft-muted.").queue(); } diff --git a/src/main/java/com/fpghoti/biscuit/config/ConfigRetrieval.java b/src/main/java/com/fpghoti/biscuit/config/ConfigRetrieval.java index e502d9c..d49c6c8 100644 --- a/src/main/java/com/fpghoti/biscuit/config/ConfigRetrieval.java +++ b/src/main/java/com/fpghoti/biscuit/config/ConfigRetrieval.java @@ -2,15 +2,10 @@ package com.fpghoti.biscuit.config; import java.io.File; import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.OutputStream; -import java.io.StringWriter; import java.nio.file.Files; import java.nio.file.Path; import java.util.Properties; @@ -95,7 +90,12 @@ public class ConfigRetrieval { added = addProperty("Custom-Command-Names", "", prop, added); added = addProperty("DisabledCommands", "cmd1,cmd2,cmd3", prop, added); added = addProperty("DisabledUserCommands", "cmd4,cmd5,cmd6", prop, added); - + added = addProperty("ModRole", "biscuit-key", prop, added); + added = addProperty("AdminRole", "biscuit-admin", prop, added); + added = addProperty("Toggle-Role-React-Channels", "toggleroles1,toggleroles2,toggleroles3", prop, added); + added = addProperty("Boost-Exclusive-Roles", "role2,role3", prop, added); + added = addProperty("Treat-Like-Booster", "Nitro Booster,silver,gold", prop, added); + return added; } diff --git a/src/main/java/com/fpghoti/biscuit/config/PropertiesRetrieval.java b/src/main/java/com/fpghoti/biscuit/config/PropertiesRetrieval.java index ade95bd..e59f438 100644 --- a/src/main/java/com/fpghoti/biscuit/config/PropertiesRetrieval.java +++ b/src/main/java/com/fpghoti/biscuit/config/PropertiesRetrieval.java @@ -38,6 +38,14 @@ public class PropertiesRetrieval { return ConfigRetrieval.getFromConfig("DefaultRoleName"); } + public static String getModRole(){ + return ConfigRetrieval.getFromConfig("ModRole"); + } + + public static String getAdminRole(){ + return ConfigRetrieval.getFromConfig("AdminRole"); + } + public static boolean noCaptchaKick(){ String value = ConfigRetrieval.getFromConfig("No-Captcha-Kick"); return value.equalsIgnoreCase("true"); @@ -57,7 +65,12 @@ public class PropertiesRetrieval { } public static String[] getNaughtyWords(){ - return ConfigRetrieval.getFromConfig("NaughtyList").replace(" ", "").split(","); + String [] list = ConfigRetrieval.getFromConfig("NaughtyList").replace(" ", "").split(","); + if(list.length == 1 && list[0].equals("")) { + String[] blank = {}; + return blank; + } + return list; } public static boolean restrictCmdChannels(){ @@ -66,30 +79,92 @@ public class PropertiesRetrieval { } public static String[] getCmdChannels(){ - return ConfigRetrieval.getFromConfig("CmdChannels").replace(" ", "").split(","); + String [] list = ConfigRetrieval.getFromConfig("CmdChannels").replace(" ", "").split(","); + if(list.length == 1 && list[0].equals("")) { + String[] blank = {}; + return blank; + } + return list; } public static String[] getToggleRoles(){ - return ConfigRetrieval.getFromConfig("ToggleRoles").replace(" ", "").split(","); + String [] list = ConfigRetrieval.getFromConfig("ToggleRoles").replace(" , ", ",").replace(", ", ",").split(","); + if(list.length == 1 && list[0].equals("")) { + String[] blank = {}; + return blank; + } + return list; + } + + public static String[] getBoostExclusiveRoles(){ + String [] list = ConfigRetrieval.getFromConfig("Boost-Exclusive-Roles").replace(" , ", ",").replace(", ", ",").split(","); + if(list.length == 1 && list[0].equals("")) { + String[] blank = {}; + return blank; + } + return list; + } + + public static String[] getBoosterRoles(){ + String [] list = ConfigRetrieval.getFromConfig("Treat-Like-Booster").replace(" , ", ",").replace(", ", ",").split(","); + if(list.length == 1 && list[0].equals("")) { + String[] blank = {}; + return blank; + } + return list; } public static String[] blockedUnicodeEmotes(){ - return ConfigRetrieval.getFromConfig("Block-Unicode-Emotes").replace(" ", "").split(","); + String [] list = ConfigRetrieval.getFromConfig("Block-Unicode-Emotes").replace(" ", "").split(","); + if(list.length == 1 && list[0].equals("")) { + String[] blank = {}; + return blank; + } + return list; } public static String[] blockedCustomEmotes(){ - return ConfigRetrieval.getFromConfig("Block-Custom-Emotes").replace(" ", "").split(","); + String [] list = ConfigRetrieval.getFromConfig("Block-Custom-Emotes").replace(" ", "").split(","); + if(list.length == 1 && list[0].equals("")) { + String[] blank = {}; + return blank; + } + return list; } public static String[] getCustomCmds(){ - return ConfigRetrieval.getFromConfig("Custom-Command-Names").replace(" ", "").split(","); + String [] list = ConfigRetrieval.getFromConfig("Custom-Command-Names").replace(" ", "").split(","); + if(list.length == 1 && list[0].equals("")) { + String[] blank = {}; + return blank; + } + return list; } public static String[] disabledCommands(){ - return ConfigRetrieval.getFromConfig("DisabledCommands").replace(" ", "").split(","); + String [] list = ConfigRetrieval.getFromConfig("DisabledCommands").replace(" ", "").split(","); + if(list.length == 1 && list[0].equals("")) { + String[] blank = {}; + return blank; + } + return list; } public static String[] disabledUserCommands(){ - return ConfigRetrieval.getFromConfig("DisabledUserCommands").replace(" ", "").split(","); + String [] list = ConfigRetrieval.getFromConfig("DisabledUserCommands").replace(" ", "").split(","); + if(list.length == 1 && list[0].equals("")) { + String[] blank = {}; + return blank; + } + return list; + } + + public static String[] getToggleChannels(){ + String [] list = ConfigRetrieval.getFromConfig("Toggle-Role-React-Channels").replace(" ", "").split(","); + if(list.length == 1 && list[0].equals("")) { + String[] blank = {}; + return blank; + } + return list; } } diff --git a/src/main/java/com/fpghoti/biscuit/listener/MessageReceiveListener.java b/src/main/java/com/fpghoti/biscuit/listener/MessageReceiveListener.java index e7acd97..3970724 100644 --- a/src/main/java/com/fpghoti/biscuit/listener/MessageReceiveListener.java +++ b/src/main/java/com/fpghoti/biscuit/listener/MessageReceiveListener.java @@ -8,6 +8,7 @@ import com.fpghoti.biscuit.config.PropertiesRetrieval; import com.fpghoti.biscuit.global.MessageQueue; import com.fpghoti.biscuit.global.SpamRecords; import com.fpghoti.biscuit.util.ChatFilter; +import com.fpghoti.biscuit.util.PermUtil; import com.fpghoti.biscuit.util.Util; import net.dv8tion.jda.api.Permission; @@ -25,6 +26,7 @@ public class MessageReceiveListener extends ListenerAdapter{ @Override public void onMessageReceived(MessageReceivedEvent event){ if (event.isFromType(ChannelType.TEXT)) { + PermUtil.clearUndeservedRoles(event.getMember()); if(Util.isLoggable(event.getTextChannel())) { if(PropertiesRetrieval.logChat()) { log.info( "NEW MSG - MSGID: " + event.getMessageId() + "- @" + event.getAuthor().getName() + " " + event.getAuthor().getAsMention() + " - CHANNEL: #" + event.getChannel().getName() + " - " + event.getMessage().getContentDisplay()); diff --git a/src/main/java/com/fpghoti/biscuit/listener/ReactionListener.java b/src/main/java/com/fpghoti/biscuit/listener/ReactionListener.java new file mode 100644 index 0000000..b22e240 --- /dev/null +++ b/src/main/java/com/fpghoti/biscuit/listener/ReactionListener.java @@ -0,0 +1,91 @@ +package com.fpghoti.biscuit.listener; + +import org.slf4j.Logger; + +import com.fpghoti.biscuit.Main; +import com.fpghoti.biscuit.config.PropertiesRetrieval; +import com.fpghoti.biscuit.util.PermUtil; +import com.fpghoti.biscuit.util.Util; + +import net.dv8tion.jda.api.entities.Guild; +import net.dv8tion.jda.api.entities.Member; +import net.dv8tion.jda.api.entities.Role; +import net.dv8tion.jda.api.events.message.react.GenericMessageReactionEvent; +import net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent; +import net.dv8tion.jda.api.events.message.react.MessageReactionRemoveEvent; +import net.dv8tion.jda.api.hooks.ListenerAdapter; + +public class ReactionListener extends ListenerAdapter{ + + Logger log = Main.log; + + @Override + public void onMessageReactionAdd(MessageReactionAddEvent event){ + if(event.getGuild() == null) { + return; + } + if(Util.contains(PropertiesRetrieval.getToggleChannels(),event.getTextChannel().getName())) { + handleMessageRole(event, false); + } + } + + @Override + public void onMessageReactionRemove(MessageReactionRemoveEvent event){ + if(event.getGuild() == null) { + return; + } + if(Util.contains(PropertiesRetrieval.getToggleChannels(),event.getTextChannel().getName())) { + handleMessageRole(event, true); + } + } + + private void handleMessageRole(GenericMessageReactionEvent event, boolean remove) { + event.getTextChannel().retrieveMessageById(event.getMessageId()).queue((message) -> { + String msg = message.getContentDisplay(); + for(String rolename : PropertiesRetrieval.getToggleRoles()) { + for(Role r : event.getGuild().getRoles()) { + if(r.getName().toLowerCase().equalsIgnoreCase(rolename)) { + if(msg.toLowerCase().contains("[toggle " + rolename.toLowerCase() + "]")) { + toggleRole(r, event, remove); + } + } + } + } + + }, (failure) -> { + failure.printStackTrace(); + }); + } + + private void toggleRole(Role role, GenericMessageReactionEvent event, boolean remove) { + Guild guild = event.getGuild(); + if(role == null) { + return; + } + Member m = event.getMember(); + if(remove) { + if(PermUtil.hasRole(m, role)) { + log.info("REACTION TOGGLE (#" + event.getTextChannel().getName() + ") - Removing role " + role.getName() + " from " + m.getUser().getName() + "(" + m.getId() + ")"); + guild.removeRoleFromMember(m, role).queue(); + } + }else { + if(!PermUtil.hasRole(m, role)) { + + boolean canAdd = false; + if(PermUtil.isBoosterExclusive(role)) { + if(PermUtil.isBooster(event.getMember())) { + canAdd = true; + } + }else { + canAdd = true; + } + if(canAdd) { + log.info("REACTION TOGGLE (#" + event.getTextChannel().getName() + ") - Adding role " + role.getName() + " too " + m.getUser().getName() + "(" + m.getId() + ")"); + guild.addRoleToMember(m, role).queue(); + } + } + } + } + + +} \ No newline at end of file diff --git a/src/main/java/com/fpghoti/biscuit/listener/RoleListener.java b/src/main/java/com/fpghoti/biscuit/listener/RoleListener.java new file mode 100644 index 0000000..1768612 --- /dev/null +++ b/src/main/java/com/fpghoti/biscuit/listener/RoleListener.java @@ -0,0 +1,20 @@ +package com.fpghoti.biscuit.listener; + +import org.slf4j.Logger; + +import com.fpghoti.biscuit.Main; +import com.fpghoti.biscuit.util.PermUtil; + +import net.dv8tion.jda.api.events.guild.member.GuildMemberRoleRemoveEvent; +import net.dv8tion.jda.api.hooks.ListenerAdapter; + +public class RoleListener extends ListenerAdapter{ + + Logger log = Main.log; + + @Override + public void onGuildMemberRoleRemove(GuildMemberRoleRemoveEvent event){ + PermUtil.clearUndeservedRoles(event.getMember()); + } + +} \ No newline at end of file diff --git a/src/main/java/com/fpghoti/biscuit/user/PreUser.java b/src/main/java/com/fpghoti/biscuit/user/PreUser.java index a36418c..b58def2 100644 --- a/src/main/java/com/fpghoti/biscuit/user/PreUser.java +++ b/src/main/java/com/fpghoti/biscuit/user/PreUser.java @@ -7,7 +7,6 @@ import java.util.concurrent.CopyOnWriteArrayList; import com.fpghoti.biscuit.Main; import com.fpghoti.biscuit.PluginCore; import com.fpghoti.biscuit.config.PropertiesRetrieval; -import com.fpghoti.biscuit.util.Util; import com.github.cage.Cage; import net.dv8tion.jda.api.JDA; diff --git a/src/main/java/com/fpghoti/biscuit/util/PermUtil.java b/src/main/java/com/fpghoti/biscuit/util/PermUtil.java index 8c20742..2e81b88 100644 --- a/src/main/java/com/fpghoti/biscuit/util/PermUtil.java +++ b/src/main/java/com/fpghoti/biscuit/util/PermUtil.java @@ -1,5 +1,9 @@ package com.fpghoti.biscuit.util; +import com.fpghoti.biscuit.Main; +import com.fpghoti.biscuit.config.PropertiesRetrieval; +import com.jcabi.aspects.Async; + import net.dv8tion.jda.api.Permission; import net.dv8tion.jda.api.entities.Member; import net.dv8tion.jda.api.entities.Role; @@ -9,6 +13,12 @@ public class PermUtil { public static boolean isAdmin(Member member){ if(member.hasPermission(Permission.ADMINISTRATOR)){ return true; + }else{ + for(Role role : member.getRoles()){ + if(role.getName().equalsIgnoreCase(PropertiesRetrieval.getAdminRole())){ + return true; + } + } } return false; } @@ -18,7 +28,7 @@ public class PermUtil { return true; }else{ for(Role role : member.getRoles()){ - if(role.getName().equalsIgnoreCase("biscuit-key")){ + if(role.getName().equalsIgnoreCase(PropertiesRetrieval.getModRole())){ return true; } } @@ -26,6 +36,16 @@ public class PermUtil { return false; } + + public static boolean hasRole(Member member, String role){ + for(Role r : member.getRoles()){ + if(r.getName().equalsIgnoreCase(role)){ + return true; + } + } + + return false; + } public static boolean hasRole(Member member, Role role){ for(Role r : member.getRoles()){ @@ -36,18 +56,32 @@ public class PermUtil { return false; } - - public static boolean canMute(Member member){ - if(member.hasPermission(Permission.MESSAGE_MANAGE)){ + + public static boolean isBooster(Member member) { + if(isAdmin(member)) { return true; - }else{ - for(Role role : member.getRoles()){ - if(role.getName().equalsIgnoreCase("biscuit-key")){ - return true; - } + } + for(String r : PropertiesRetrieval.getBoosterRoles()) { + if(hasRole(member,r)) { + return true; } } return false; } + + public static boolean isBoosterExclusive(Role r) { + return Util.containsIgnoreCase(PropertiesRetrieval.getBoostExclusiveRoles(),r.getName()); + } + + @Async + public static void clearUndeservedRoles(Member m) { + boolean booster = isBooster(m); + for(Role r : m.getRoles()) { + if(!booster && isBoosterExclusive(r)) { + m.getGuild().removeRoleFromMember(m,r).queue(); + Main.log.info("BOOST ROLE REMOVED - Member: " + m.getUser().getName() + " (" + m.getId() + ") Role: " + r.getName()); + } + } + } } diff --git a/src/main/java/com/fpghoti/biscuit/util/Util.java b/src/main/java/com/fpghoti/biscuit/util/Util.java index 7e037c5..60b5a15 100644 --- a/src/main/java/com/fpghoti/biscuit/util/Util.java +++ b/src/main/java/com/fpghoti/biscuit/util/Util.java @@ -40,6 +40,15 @@ public class Util { } return false; } + + public static boolean containsIgnoreCase(String[] list, String s) { + for(String l : list) { + if(s.equalsIgnoreCase(l)) { + return true; + } + } + return false; + } public static boolean isDeciDigit(String s){ int i = 0; diff --git a/src/main/resources/config.properties b/src/main/resources/config.properties index 45cee97..38ab344 100644 --- a/src/main/resources/config.properties +++ b/src/main/resources/config.properties @@ -25,9 +25,25 @@ CmdChannels = bot,bot2,bot3 #Allow users to toggle these roles ToggleRoles = role1,role2,role3 +#Restrict these toggle roles to boosters. Will be removed on boost cancel +Boost-Exclusive-Roles = role2,role3 + +#Treat these roles like booster. +Treat-Like-Booster = Nitro Booster,silver,gold + #Bot can automatically assign role upon user join UseCustomDefaultRole = true +#This role will give Biscuit mod commands to users with the role +ModRole = biscuit-key + +#This role will give complete admin bot access to users with the role +AdminRole = biscuit-admin + +#In these channels, the bot will toggle roles for the ToggleRoles for anyone +#who reacts to a message with [Toggle ] +Toggle-Role-React-Channels = roles1,roles2,roles3 + #Does nothing if UseCustomDefaultRole is off DefaultRoleName = Standard