From 1041bf9503f195a0293a40505cc0fb9cc4a7ea4a Mon Sep 17 00:00:00 2001 From: JoeShimell <49585339+JoeShimo@users.noreply.github.com> Date: Fri, 1 May 2020 20:21:06 +0100 Subject: [PATCH] Custom message support --- plugin.yml | 2 +- .../uk/co/angrybee/joe/DiscordClient.java | 445 +++++++++++++++--- .../co/angrybee/joe/DiscordWhitelister.java | 314 +++++++++++- .../java/uk/co/angrybee/joe/VersionInfo.java | 5 +- 4 files changed, 686 insertions(+), 80 deletions(-) diff --git a/plugin.yml b/plugin.yml index f2c73f1..a55d42d 100755 --- a/plugin.yml +++ b/plugin.yml @@ -1,5 +1,5 @@ name: DiscordWhitelister -version: 1.3.3 +version: 1.3.4 author: Joe Shimell main: uk.co.angrybee.joe.DiscordWhitelister description: Discord whitelister bot. diff --git a/src/main/java/uk/co/angrybee/joe/DiscordClient.java b/src/main/java/uk/co/angrybee/joe/DiscordClient.java index fe80460..f186a81 100644 --- a/src/main/java/uk/co/angrybee/joe/DiscordClient.java +++ b/src/main/java/uk/co/angrybee/joe/DiscordClient.java @@ -145,38 +145,74 @@ public class DiscordClient extends ListenerAdapter { TextChannel channel = messageReceivedEvent.getTextChannel(); // select different whitelist commands - if (messageContents.toLowerCase().equals("!whitelist")) { + if (messageContents.toLowerCase().equals("!whitelist")) + { // info command - if (authorPermissions.isUserCanUseCommand()) { + if (authorPermissions.isUserCanUseCommand()) + { channel.sendMessage(botInfo).queue(); - } else { + } + else + { EmbedBuilder insufficientPermission = new EmbedBuilder(); - insufficientPermission.addField("Insufficient Permissions", (author.getAsMention() + ", you do not have permission to use this command."), false); insufficientPermission.setColor(new Color(231, 76, 60)); + + if(!DiscordWhitelister.useCustomMessages) + { + insufficientPermission.addField("Insufficient Permissions", (author.getAsMention() + ", you do not have permission to use this command."), false); + } + else + { + String customTitle = DiscordWhitelister.getCustomMessagesConfig().getString("insufficient-permissions-title"); + String customMessage = DiscordWhitelister.getCustomMessagesConfig().getString("insufficient-permissions"); + customMessage = customMessage.replaceAll("\\{Sender}", author.getAsMention()); // Only checking for {Sender} + + insufficientPermission.addField(customTitle, customMessage, false); + } + channel.sendMessage(insufficientPermission.build()).queue(); } } - if (messageContents.toLowerCase().startsWith("!whitelist add")) { - // add command + // Add Command + if (messageContents.toLowerCase().startsWith("!whitelist add")) + { + // Permission check + if (!(authorPermissions.isUserCanAddRemove() || authorPermissions.isUserCanAdd() || limitedWhitelistEnabled && authorPermissions.isUserHasLimitedAdd())) + { + EmbedBuilder insufficientPermission = new EmbedBuilder(); + insufficientPermission.setColor(new Color(231, 76, 60)); - // permissions - if (!(authorPermissions.isUserCanAddRemove() || authorPermissions.isUserCanAdd() || limitedWhitelistEnabled && authorPermissions.isUserHasLimitedAdd())) { - EmbedBuilder embedBuilderFailure = new EmbedBuilder(); - embedBuilderFailure.addField("Insufficient Permissions", (author.getAsMention() + ", you do not have permission to use this command."), false); - embedBuilderFailure.setColor(new Color(231, 76, 60)); - channel.sendMessage(embedBuilderFailure.build()).queue(); + if(!DiscordWhitelister.useCustomMessages) + { + insufficientPermission.addField("Insufficient Permissions", (author.getAsMention() + ", you do not have permission to use this command."), false); + } + else + { + String customTitle = DiscordWhitelister.getCustomMessagesConfig().getString("insufficient-permissions-title"); + String customMessage = DiscordWhitelister.getCustomMessagesConfig().getString("insufficient-permissions"); + customMessage = customMessage.replaceAll("\\{Sender}", author.getAsMention()); // Only checking for {Sender} + + insufficientPermission.addField(customTitle, customMessage, false); + } + + channel.sendMessage(insufficientPermission.build()).queue(); return; } /* if limited whitelist is enabled, check if the user is in the limited whitelister group and add the user to the list which records how many times the user has successfully used the whitelist command */ - if (limitedWhitelistEnabled && authorPermissions.isUserHasLimitedAdd()) { - if (DiscordWhitelister.getUserList().getString(author.getId()) == null) { + if (limitedWhitelistEnabled && authorPermissions.isUserHasLimitedAdd()) + { + if (DiscordWhitelister.getUserList().getString(author.getId()) == null) + { DiscordWhitelister.getUserList().set(author.getId(), new ArrayList()); - try { + try + { DiscordWhitelister.getUserList().save(DiscordWhitelister.getUserListFile().getPath()); - } catch (IOException e) { + } + catch (IOException e) + { EmbedBuilder failure = new EmbedBuilder(); failure.addField("Internal Error", (author.getAsMention() + ", something went wrong while accessing config files. Please contact a staff member."), false); failure.setColor(new Color(231, 76, 60)); @@ -188,15 +224,18 @@ public class DiscordClient extends ListenerAdapter { } boolean usedAllWhitelists = false; - try { - usedAllWhitelists = - DiscordWhitelister.getRegisteredUsersCount(author.getId()) >= maxWhitelistAmount && + try + { + usedAllWhitelists = DiscordWhitelister.getRegisteredUsersCount(author.getId()) >= maxWhitelistAmount && !authorPermissions.isUserCanAddRemove() && !authorPermissions.isUserCanAdd(); - } catch (NullPointerException exception) { + } + catch (NullPointerException exception) + { exception.printStackTrace(); } - if (authorPermissions.isUserCanAddRemove() || authorPermissions.isUserCanAdd() || limitedWhitelistEnabled && authorPermissions.isUserHasLimitedAdd()) { + if (authorPermissions.isUserCanAddRemove() || authorPermissions.isUserCanAdd() || limitedWhitelistEnabled && authorPermissions.isUserHasLimitedAdd()) + { messageContents = messageContents.toLowerCase(); String messageContentsAfterCommand = messageContents.substring("!whitelist add".length() + 1); // get everything after !whitelist add[space] final String finalNameToAdd = messageContentsAfterCommand.replaceAll(" .*", ""); // The name is everything up to the first space @@ -208,50 +247,129 @@ public class DiscordClient extends ListenerAdapter { boolean onlyHasLimitedAdd = limitedWhitelistEnabled && authorPermissions.isUserHasLimitedAdd() && !authorPermissions.isUserCanAddRemove() && !authorPermissions.isUserCanAdd(); - if (onlyHasLimitedAdd) { + if (onlyHasLimitedAdd) + { timesWhitelisted = DiscordWhitelister.getRegisteredUsersCount(author.getId()); // set to current max in case the max whitelist amount was changed - if (timesWhitelisted > maxWhitelistAmount) { + if (timesWhitelisted > maxWhitelistAmount) + { timesWhitelisted = maxWhitelistAmount; } } - if (onlyHasLimitedAdd && usedAllWhitelists) { + if (onlyHasLimitedAdd && usedAllWhitelists) + { EmbedBuilder embedBuilderInfo = new EmbedBuilder(); - embedBuilderInfo.addField("No Whitelists Remaining", (author.getAsMention() + ", unable to whitelist. You have **" + (maxWhitelistAmount - timesWhitelisted) - + " out of " + maxWhitelistAmount + "** whitelists remaining."), false); embedBuilderInfo.setColor(new Color(104, 109, 224)); + + if(!DiscordWhitelister.useCustomMessages) + { + embedBuilderInfo.addField("No Whitelists Remaining", (author.getAsMention() + ", unable to whitelist. You have **" + (maxWhitelistAmount - timesWhitelisted) + + " out of " + maxWhitelistAmount + "** whitelists remaining."), false); + } + else + { + String customTitle = DiscordWhitelister.getCustomMessagesConfig().getString("no-whitelists-remaining-title"); + String customMessage = DiscordWhitelister.getCustomMessagesConfig().getString("no-whitelists-remaining"); + customMessage = customMessage.replaceAll("\\{Sender}", author.getAsMention()); + customMessage = customMessage.replaceAll("\\{RemainingWhitelists}", String.valueOf((maxWhitelistAmount - timesWhitelisted))); + customMessage = customMessage.replaceAll("\\{MaxWhitelistAmount}", String.valueOf(maxWhitelistAmount)); + + embedBuilderInfo.addField(customTitle, customMessage, false); + } + channel.sendMessage(embedBuilderInfo.build()).queue(); return; } - if (finalNameToAdd.isEmpty()) { + if (finalNameToAdd.isEmpty()) + { channel.sendMessage(addCommandInfo).queue(); - } else { - if (usernameValidation) { + } + else + { + if (usernameValidation) + { // Invalid char check - for (int a = 0; a < finalNameToWhitelistChar.length; ++a) { - if (new String(validCharacters).indexOf(finalNameToWhitelistChar[a]) == -1) { + for (int a = 0; a < finalNameToWhitelistChar.length; ++a) + { + if (new String(validCharacters).indexOf(finalNameToWhitelistChar[a]) == -1) + { EmbedBuilder embedBuilderInvalidChar = new EmbedBuilder(); embedBuilderInvalidChar.setColor(new Color(231, 76, 60)); - embedBuilderInvalidChar.addField("Invalid username", (author.getAsMention() + ", the username you have specified contains invalid characters. **Only letters, numbers and underscores are allowed**."), false); - if (onlyHasLimitedAdd) { - embedBuilderInvalidChar.addField("Whitelists Remaining", ("You have **" + (maxWhitelistAmount - timesWhitelisted) + " out of " + maxWhitelistAmount + "** whitelists remaining."), false); + + if(!DiscordWhitelister.useCustomMessages) + { + embedBuilderInvalidChar.addField("Invalid Username", (author.getAsMention() + ", the username you have specified contains invalid characters. **Only letters, numbers and underscores are allowed**."), false); } + else + { + String customTitle = DiscordWhitelister.getCustomMessagesConfig().getString("invalid-characters-warning-title"); + String customMessage = DiscordWhitelister.getCustomMessagesConfig().getString("invalid-characters-warning"); + customMessage = customMessage.replaceAll("\\{Sender}", author.getAsMention()); + + embedBuilderInvalidChar.addField(customTitle, customMessage,false); + } + + if (onlyHasLimitedAdd) + { + if(!DiscordWhitelister.useCustomMessages) + { + embedBuilderInvalidChar.addField("Whitelists Remaining", ("You have **" + (maxWhitelistAmount - timesWhitelisted) + " out of " + maxWhitelistAmount + "** whitelists remaining."), false); + } + else + { + String customTitle = DiscordWhitelister.getCustomMessagesConfig().getString("whitelists-remaining-title"); + String customMessage = DiscordWhitelister.getCustomMessagesConfig().getString("whitelists-remaining"); + customMessage = customMessage.replaceAll("\\{RemainingWhitelists}", String.valueOf((maxWhitelistAmount - timesWhitelisted))); + customMessage = customMessage.replaceAll("\\{MaxWhitelistAmount}", String.valueOf(maxWhitelistAmount)); + + embedBuilderInvalidChar.addField(customTitle, customMessage, false); + } + } + channel.sendMessage(embedBuilderInvalidChar.build()).queue(); return; } } // Length check - if (finalNameToAdd.length() < 3 || finalNameToAdd.length() > 16) { + if (finalNameToAdd.length() < 3 || finalNameToAdd.length() > 16) + { EmbedBuilder embedBuilderLengthInvalid = new EmbedBuilder(); embedBuilderLengthInvalid.setColor(new Color(231, 76, 60)); - embedBuilderLengthInvalid.addField("Invalid username", (author.getAsMention() + ", the username you have specified either contains too few or too many characters. **Usernames can only consist of 3-16 characters**."), false); - if (onlyHasLimitedAdd) { - embedBuilderLengthInvalid.addField("Whitelists Remaining", ("You have **" + (maxWhitelistAmount - timesWhitelisted) + " out of " + maxWhitelistAmount + "** whitelists remaining."), false); + + if(!DiscordWhitelister.useCustomMessages) + { + embedBuilderLengthInvalid.addField("Invalid Username", (author.getAsMention() + ", the username you have specified either contains too few or too many characters. **Usernames can only consist of 3-16 characters**."), false); } + else + { + String customTitle = DiscordWhitelister.getCustomMessagesConfig().getString("invalid-length-warning-title"); + String customMessage = DiscordWhitelister.getCustomMessagesConfig().getString("invalid-length-warning"); + customMessage = customMessage.replaceAll("\\{Sender}", author.getAsMention()); + + embedBuilderLengthInvalid.addField(customTitle, customMessage,false); + } + + if (onlyHasLimitedAdd) + { + if(!DiscordWhitelister.useCustomMessages) + { + embedBuilderLengthInvalid.addField("Whitelists Remaining", ("You have **" + (maxWhitelistAmount - timesWhitelisted) + " out of " + maxWhitelistAmount + "** whitelists remaining."), false); + } + else + { + String customTitle = DiscordWhitelister.getCustomMessagesConfig().getString("whitelists-remaining-title"); + String customMessage = DiscordWhitelister.getCustomMessagesConfig().getString("whitelists-remaining"); + customMessage = customMessage.replaceAll("\\{RemainingWhitelists}", String.valueOf((maxWhitelistAmount - timesWhitelisted))); + customMessage = customMessage.replaceAll("\\{MaxWhitelistAmount}", String.valueOf(maxWhitelistAmount)); + + embedBuilderLengthInvalid.addField(customTitle, customMessage, false); + } + } + channel.sendMessage(embedBuilderLengthInvalid.build()).queue(); return; } @@ -281,12 +399,27 @@ public class DiscordClient extends ListenerAdapter { } } - if (DiscordWhitelister.useEasyWhitelist || checkWhitelistJSON(whitelistJSON, finalNameToAdd)) { - if (tempFileConfiguration.getStringList("whitelisted").contains(finalNameToAdd)) { - + if (DiscordWhitelister.useEasyWhitelist || checkWhitelistJSON(whitelistJSON, finalNameToAdd)) + { + if (tempFileConfiguration.getStringList("whitelisted").contains(finalNameToAdd)) + { EmbedBuilder embedBuilderAlreadyWhitelisted = new EmbedBuilder(); - embedBuilderAlreadyWhitelisted.addField("This user is already on the whitelist", (author.getAsMention() + ", cannot add user as `" + finalNameToAdd + "` is already on the whitelist!"), false); embedBuilderAlreadyWhitelisted.setColor(new Color(104, 109, 224)); + + if(!DiscordWhitelister.useCustomMessages) + { + embedBuilderAlreadyWhitelisted.addField("User already on the whitelist", (author.getAsMention() + ", cannot add user as `" + finalNameToAdd + "` is already on the whitelist!"), false); + } + else + { + String customTitle = DiscordWhitelister.getCustomMessagesConfig().getString("already-on-whitelist-title"); + String customMessage = DiscordWhitelister.getCustomMessagesConfig().getString("already-on-whitelist"); + customMessage = customMessage.replaceAll("\\{Sender}", author.getAsMention()); + customMessage = customMessage.replaceAll("\\{MinecraftUsername}", finalNameToAdd); + + embedBuilderAlreadyWhitelisted.addField(customTitle, customMessage, false); + } + channel.sendMessage(embedBuilderAlreadyWhitelisted.build()).queue(); return; } @@ -294,22 +427,46 @@ public class DiscordClient extends ListenerAdapter { if (DiscordWhitelister.getRemovedList().get(finalNameToAdd) != null) // If the user has been removed before { - if (onlyHasLimitedAdd) { + if (onlyHasLimitedAdd) + { EmbedBuilder embedBuilderRemovedByStaff = new EmbedBuilder(); embedBuilderRemovedByStaff.setColor(new Color(231, 76, 60)); - embedBuilderRemovedByStaff.addField("This user was previously removed by a staff member", (author.getAsMention() + ", this user was previously removed by a staff member (<@" + DiscordWhitelister.getRemovedList().get(finalNameToAdd) + ">)." - + System.lineSeparator() + "Please ask a user with higher permissions to add this user." + System.lineSeparator()), false); - embedBuilderRemovedByStaff.addField("Whitelists Remaining", ("You have **" + (maxWhitelistAmount - timesWhitelisted) - + " out of " + DiscordWhitelister.getWhitelisterBotConfig().getString("max-whitelist-amount") + "** whitelists remaining."), false); + + if(!DiscordWhitelister.useCustomMessages) + { + embedBuilderRemovedByStaff.addField("This user was previously removed by a staff member", (author.getAsMention() + ", this user was previously removed by a staff member (<@" + DiscordWhitelister.getRemovedList().get(finalNameToAdd) + ">)." + + System.lineSeparator() + "Please ask a user with higher permissions to add this user." + System.lineSeparator()), false); + embedBuilderRemovedByStaff.addField("Whitelists Remaining", ("You have **" + (maxWhitelistAmount - timesWhitelisted) + + " out of " + DiscordWhitelister.getWhitelisterBotConfig().getString("max-whitelist-amount") + "** whitelists remaining."), false); + } + else + { + String customTitle = DiscordWhitelister.getCustomMessagesConfig().getString("user-was-removed-title"); + String customMessage = DiscordWhitelister.getCustomMessagesConfig().getString("user-was-removed"); + String customWhitelistsRemaining = DiscordWhitelister.getCustomMessagesConfig().getString("whitelists-remaining"); + String staffMemberMention = "<@" + DiscordWhitelister.getRemovedList().get(finalNameToAdd) + ">"; + + customMessage = customMessage.replaceAll("\\{Sender}", author.getAsMention()); + customMessage = customMessage.replaceAll("\\{StaffMember}", staffMemberMention); + + customWhitelistsRemaining = customWhitelistsRemaining.replaceAll("\\{RemainingWhitelists}", String.valueOf((maxWhitelistAmount - timesWhitelisted))); + customWhitelistsRemaining = customWhitelistsRemaining.replaceAll("\\{MaxWhitelistAmount}", String.valueOf(maxWhitelistAmount)); + + embedBuilderRemovedByStaff.addField(customTitle, customMessage + " " + customWhitelistsRemaining, false); + } + channel.sendMessage(embedBuilderRemovedByStaff.build()).queue(); return; - } else // Remove from removed list + } + else // Remove from removed list { DiscordWhitelister.getRemovedList().set(finalNameToAdd, null); - try { + try + { DiscordWhitelister.getRemovedList().save(DiscordWhitelister.getRemovedListFile().getPath()); - } catch (IOException e) { + } catch (IOException e) + { e.printStackTrace(); } @@ -326,18 +483,75 @@ public class DiscordClient extends ListenerAdapter { /* Configure success & failure messages here instead of on the main server thread - this will run even if the message is never sent, but is a good trade off */ EmbedBuilder embedBuilderWhitelistSuccess = new EmbedBuilder(); - embedBuilderWhitelistSuccess.addField((finalNameToAdd + " is now whitelisted!"), (author.getAsMention() + " has added `" + finalNameToAdd + "` to the whitelist."), false); - if (onlyHasLimitedAdd) { - embedBuilderWhitelistSuccess.addField("Whitelists Remaining", ("You have **" + (maxWhitelistAmount - (timesWhitelisted + 1)) + " out of " + maxWhitelistAmount + "** whitelists remaining."), false); - } embedBuilderWhitelistSuccess.setColor(new Color(46, 204, 113)); embedBuilderWhitelistSuccess.setThumbnail("https://minotar.net/bust/" + playerUUID + "/100.png"); + if(!DiscordWhitelister.useCustomMessages) + { + embedBuilderWhitelistSuccess.addField((finalNameToAdd + " is now whitelisted!"), (author.getAsMention() + " has added `" + finalNameToAdd + "` to the whitelist."), false); + } + else + { + String customTitle = DiscordWhitelister.getCustomMessagesConfig().getString("whitelist-success-title"); + customTitle = customTitle.replaceAll("\\{MinecraftUsername}", finalNameToAdd); + + String customMessage = DiscordWhitelister.getCustomMessagesConfig().getString("whitelist-success"); + customMessage = customMessage.replaceAll("\\{Sender}", author.getAsMention()); + customMessage = customMessage.replaceAll("\\{MinecraftUsername}", finalNameToAdd); + + embedBuilderWhitelistSuccess.addField(customTitle, customMessage, false); + } + + if (onlyHasLimitedAdd) + { + if(!DiscordWhitelister.useCustomMessages) + { + embedBuilderWhitelistSuccess.addField("Whitelists Remaining", ("You have **" + (maxWhitelistAmount - (timesWhitelisted + 1)) + " out of " + maxWhitelistAmount + "** whitelists remaining."), false); + } + else + { + String customTitle = DiscordWhitelister.getCustomMessagesConfig().getString("whitelists-remaining-title"); + String customMessage = DiscordWhitelister.getCustomMessagesConfig().getString("whitelists-remaining"); + customMessage = customMessage.replaceAll("\\{RemainingWhitelists}", String.valueOf((maxWhitelistAmount - (timesWhitelisted + 1)))); + customMessage = customMessage.replaceAll("\\{MaxWhitelistAmount}", String.valueOf(maxWhitelistAmount)); + + embedBuilderWhitelistSuccess.addField(customTitle, customMessage, false); + } + } + EmbedBuilder embedBuilderWhitelistFailure = new EmbedBuilder(); embedBuilderWhitelistFailure.setColor(new Color(231, 76, 60)); - embedBuilderWhitelistFailure.addField("Failed to Whitelist", (author.getAsMention() + ", failed to add `" + finalNameToAdd + "` to the whitelist. This is most likely due to an invalid Minecraft username."), false); - if (onlyHasLimitedAdd) { - embedBuilderWhitelistFailure.addField("Whitelists Remaining", ("You have **" + (maxWhitelistAmount - timesWhitelisted) + " out of " + maxWhitelistAmount + "** whitelists remaining."), false); + + if(!DiscordWhitelister.useCustomMessages) + { + embedBuilderWhitelistFailure.addField("Failed to whitelist", (author.getAsMention() + ", failed to add `" + finalNameToAdd + "` to the whitelist. This is most likely due to an invalid Minecraft username."), false); + } + else + { + String customTitle = DiscordWhitelister.getCustomMessagesConfig().getString("whitelist-failure-title"); + + String customMessage = DiscordWhitelister.getCustomMessagesConfig().getString("whitelist-failure"); + customMessage = customMessage.replaceAll("\\{Sender}", author.getAsMention()); + customMessage = customMessage.replaceAll("\\{MinecraftUsername}", finalNameToAdd); + + embedBuilderWhitelistFailure.addField(customTitle, customMessage, false); + } + + if (onlyHasLimitedAdd) + { + if(!DiscordWhitelister.useCustomMessages) + { + embedBuilderWhitelistFailure.addField("Whitelists Remaining", ("You have **" + (maxWhitelistAmount - timesWhitelisted) + " out of " + maxWhitelistAmount + "** whitelists remaining."), false); + } + else + { + String customTitle = DiscordWhitelister.getCustomMessagesConfig().getString("whitelists-remaining-title"); + String customMessage = DiscordWhitelister.getCustomMessagesConfig().getString("whitelists-remaining"); + customMessage = customMessage.replaceAll("\\{RemainingWhitelists}", String.valueOf((maxWhitelistAmount - (timesWhitelisted + 1)))); + customMessage = customMessage.replaceAll("\\{MaxWhitelistAmount}", String.valueOf(maxWhitelistAmount)); + + embedBuilderWhitelistFailure.addField(customTitle, customMessage, false); + } } int tempTimesWhitelisted = timesWhitelisted; @@ -464,48 +678,104 @@ public class DiscordClient extends ListenerAdapter { boolean notOnWhitelist = false; - if (DiscordWhitelister.useEasyWhitelist) { - if (!tempFileConfiguration.getStringList("whitelisted").contains(finalNameToRemove)) { + if (DiscordWhitelister.useEasyWhitelist) + { + if (!tempFileConfiguration.getStringList("whitelisted").contains(finalNameToRemove)) + { notOnWhitelist = true; EmbedBuilder embedBuilderInfo = new EmbedBuilder(); - embedBuilderInfo.addField("This user is not on the whitelist", (author.getAsMention() + ", cannot remove user as `" + finalNameToRemove + "` is not on the whitelist!"), false); embedBuilderInfo.setColor(new Color(104, 109, 224)); + + if(!DiscordWhitelister.useCustomMessages) + { + embedBuilderInfo.addField("This user is not on the whitelist", (author.getAsMention() + ", cannot remove user as `" + finalNameToRemove + "` is not on the whitelist!"), false); + } + else + { + String customTitle = DiscordWhitelister.getCustomMessagesConfig().getString("user-not-on-whitelist-title"); + String customMessage = DiscordWhitelister.getCustomMessagesConfig().getString("user-not-on-whitelist"); + customMessage = customMessage.replaceAll("\\{Sender}", author.getAsMention()); + customMessage = customMessage.replaceAll("\\{MinecraftUsername}", finalNameToRemove); + + embedBuilderInfo.addField(customTitle, customMessage, false); + } + channel.sendMessage(embedBuilderInfo.build()).queue(); } } - if (!DiscordWhitelister.useEasyWhitelist && !checkWhitelistJSON(whitelistJSON, finalNameToRemove)) { + if (!DiscordWhitelister.useEasyWhitelist && !checkWhitelistJSON(whitelistJSON, finalNameToRemove)) + { notOnWhitelist = true; EmbedBuilder embedBuilderInfo = new EmbedBuilder(); - embedBuilderInfo.addField("This user is not on the whitelist", (author.getAsMention() + ", cannot remove user as `" + finalNameToRemove + "` is not on the whitelist!"), false); embedBuilderInfo.setColor(new Color(104, 109, 224)); + + if(!DiscordWhitelister.useCustomMessages) + { + embedBuilderInfo.addField("This user is not on the whitelist", (author.getAsMention() + ", cannot remove user as `" + finalNameToRemove + "` is not on the whitelist!"), false); + } + else + { + String customTitle = DiscordWhitelister.getCustomMessagesConfig().getString("user-not-on-whitelist-title"); + String customMessage = DiscordWhitelister.getCustomMessagesConfig().getString("user-not-on-whitelist"); + customMessage = customMessage.replaceAll("\\{Sender}", author.getAsMention()); + customMessage = customMessage.replaceAll("\\{MinecraftUsername}", finalNameToRemove); + + embedBuilderInfo.addField(customTitle, customMessage, false); + } + channel.sendMessage(embedBuilderInfo.build()).queue(); } - if (!notOnWhitelist) { - if (DiscordWhitelister.useEasyWhitelist) { - try { + if (!notOnWhitelist) + { + if (DiscordWhitelister.useEasyWhitelist) + { + try + { tempFileConfiguration.load(new File(DiscordWhitelister.easyWhitelist.getDataFolder(), "config.yml")); - } catch (IOException | InvalidConfigurationException e) { + } + catch (IOException | InvalidConfigurationException e) + { e.printStackTrace(); } executeServerCommand("easywl remove " + finalNameToRemove); - } else { + } + else + { executeServerCommand("whitelist remove " + finalNameToRemove); } // Configure message here instead of on the main thread - this means this will run even if the message is never sent, but is a good trade off (I think) EmbedBuilder embedBuilderSuccess = new EmbedBuilder(); - embedBuilderSuccess.addField((finalNameToRemove + " has been removed"), (author.getAsMention() + " has removed `" + finalNameToRemove + "` from the whitelist."), false); embedBuilderSuccess.setColor(new Color(46, 204, 113)); + if(!DiscordWhitelister.useCustomMessages) + { + embedBuilderSuccess.addField((finalNameToRemove + " has been removed"), (author.getAsMention() + " has removed `" + finalNameToRemove + "` from the whitelist."), false); + } + else + { + String customTitle = DiscordWhitelister.getCustomMessagesConfig().getString("remove-success-title"); + customTitle = customTitle.replaceAll("\\{MinecraftUsername}", finalNameToRemove); + + String customMessage = DiscordWhitelister.getCustomMessagesConfig().getString("remove-success"); + customMessage = customMessage.replaceAll("\\{Sender}", author.getAsMention()); + customMessage = customMessage.replaceAll("\\{MinecraftUsername}", finalNameToRemove); + + embedBuilderSuccess.addField(customTitle, customMessage, false); + } + EmbedBuilder embedBuilderFailure = new EmbedBuilder(); + embedBuilderFailure.setColor(new Color(231, 76, 60)); + + // No custom message needed embedBuilderFailure.addField(("Failed to remove " + finalNameToRemove + " from the whitelist"), (author.getAsMention() + ", failed to remove `" + finalNameToRemove + "` from the whitelist. " + "This should never happen, you may have to remove the player manually and report the issue."), false); - embedBuilderFailure.setColor(new Color(231, 76, 60)); + if (DiscordWhitelister.useEasyWhitelist) { DiscordWhitelister.getPlugin().getServer().getScheduler().callSyncMethod(DiscordWhitelister.getPlugin(), () -> @@ -576,22 +846,51 @@ public class DiscordClient extends ListenerAdapter { } } - if (authorPermissions.isUserCanAdd() && !authorPermissions.isUserCanAddRemove()) { + if (authorPermissions.isUserCanAdd() && !authorPermissions.isUserCanAddRemove()) + { String higherPermRoles = DiscordWhitelister.getWhitelisterBotConfig().getList("add-remove-roles").toString(); higherPermRoles = higherPermRoles.replaceAll("\\[", ""); higherPermRoles = higherPermRoles.replaceAll("]", ""); EmbedBuilder embedBuilderInfo = new EmbedBuilder(); - embedBuilderInfo.addField("Insufficient Permissions", (author.getAsMention() + ", you only have permission to add people to the whitelist. To remove people from the whitelist you must be moved to the following roles: " - + higherPermRoles + "; or get the owner to move your role to 'add-remove-roles' in the config."), false); embedBuilderInfo.setColor(new Color(104, 109, 224)); + + if(!DiscordWhitelister.useCustomMessages) + { + embedBuilderInfo.addField("Insufficient Permissions", (author.getAsMention() + ", you only have permission to add people to the whitelist. To remove people from the whitelist you must be moved to the following roles: " + + higherPermRoles + "; or get the owner to move your role to 'add-remove-roles' in the config."), false); + } + else + { + String customTitle = DiscordWhitelister.getCustomMessagesConfig().getString("insufficient-permissions-remove-title"); + String customMessage = DiscordWhitelister.getCustomMessagesConfig().getString("insufficient-permissions-remove"); + customMessage = customMessage.replaceAll("\\{Sender}", author.getAsMention()); + customMessage = customMessage.replaceAll("\\{AddRemoveRoles}", higherPermRoles); + + embedBuilderInfo.addField(customTitle, customMessage, false); + } + channel.sendMessage(embedBuilderInfo.build()).queue(); return; } + // if the user doesn't have any allowed roles EmbedBuilder embedBuilderFailure = new EmbedBuilder(); embedBuilderFailure.setColor(new Color(231, 76, 60)); - embedBuilderFailure.addField("Insufficient Permissions", (author.getAsMention() + ", you do not have permission to use this command."), false); + + if(!DiscordWhitelister.useCustomMessages) + { + embedBuilderFailure.addField("Insufficient Permissions", (author.getAsMention() + ", you do not have permission to use this command."), false); + } + else + { + String customTitle = DiscordWhitelister.getCustomMessagesConfig().getString("insufficient-permissions-title"); + String customMessage = DiscordWhitelister.getCustomMessagesConfig().getString("insufficient-permissions"); + customMessage = customMessage.replaceAll("\\{Sender}", author.getAsMention()); // Only checking for {Sender} + + embedBuilderFailure.addField(customTitle, customMessage, false); + } + channel.sendMessage(embedBuilderFailure.build()).queue(); } } diff --git a/src/main/java/uk/co/angrybee/joe/DiscordWhitelister.java b/src/main/java/uk/co/angrybee/joe/DiscordWhitelister.java index 281cf51..8dcba0a 100755 --- a/src/main/java/uk/co/angrybee/joe/DiscordWhitelister.java +++ b/src/main/java/uk/co/angrybee/joe/DiscordWhitelister.java @@ -22,10 +22,12 @@ public class DiscordWhitelister extends JavaPlugin private static File whitelisterBotConfigFile; private static File userListFile; private static File removedListFile; + private static File customMessagesFile; private static FileConfiguration whitelisterBotConfig; private static FileConfiguration userList; private static FileConfiguration removedList; + private static FileConfiguration customMessagesConfig; // easy whitelist public static Plugin easyWhitelist; @@ -35,8 +37,10 @@ public class DiscordWhitelister extends JavaPlugin private boolean configCreated = false; private boolean userListCreated = false; private boolean removedListCreated = false; + private boolean customMessagesCreated = false; public static boolean useEasyWhitelist = false; + public static boolean useCustomMessages = false; private boolean botEnabled; @@ -52,6 +56,7 @@ public class DiscordWhitelister extends JavaPlugin whitelisterBotConfig = new YamlConfiguration(); userList = new YamlConfiguration(); removedList = new YamlConfiguration(); + customMessagesConfig = new YamlConfiguration(); vanishedPlayersCount = 0; ConfigSetup(); @@ -111,6 +116,12 @@ public class DiscordWhitelister extends JavaPlugin } } + // Custom messages check + if(getWhitelisterBotConfig().getBoolean("use-custom-messages")) + { + useCustomMessages = true; + } + int initializeSuccess = DiscordClient.InitializeClient(botToken); if(initializeSuccess == 0) @@ -154,16 +165,17 @@ public class DiscordWhitelister extends JavaPlugin return userListFile; } - public static FileConfiguration getRemovedList() - { - return removedList; - } + public static FileConfiguration getRemovedList() { return removedList; } public static File getRemovedListFile() { return removedListFile; } + //public static File getCustomMessagesFile() { return customMessagesFile; } + + public static FileConfiguration getCustomMessagesConfig() { return customMessagesConfig; } + public static List getRegisteredUsers(String userId) { return userList.getList(userId); } public static int getRegisteredUsersCount(String userId) { @@ -203,6 +215,8 @@ public class DiscordWhitelister extends JavaPlugin whitelisterBotConfigFile = new File(getDataFolder(), "discord-whitelister.yml"); userListFile = new File(getDataFolder(), "user-list.yml"); removedListFile = new File(getDataFolder(), "removed-list.yml"); + customMessagesFile = new File(getDataFolder(), "custom-messages.yml"); + if(!whitelisterBotConfigFile.getParentFile().exists()) { @@ -282,6 +296,30 @@ public class DiscordWhitelister extends JavaPlugin e.printStackTrace(); } + if(!customMessagesFile.exists()) + { + try + { + customMessagesFile.createNewFile(); + } + catch(IOException e) + { + e.printStackTrace(); + } + + getLogger().info("Custom messages file has been created at: " + customMessagesFile.getPath()); + customMessagesCreated = true; + } + + try + { + getCustomMessagesConfig().load(customMessagesFile); + } + catch(IOException | InvalidConfigurationException e) + { + e.printStackTrace(); + } + // check if entries exist in config file and add them if they are not. only log entry creation if it is not first time set up if(whitelisterBotConfigFile.exists()) { @@ -448,6 +486,16 @@ public class DiscordWhitelister extends JavaPlugin } } + if(getWhitelisterBotConfig().get("use-custom-messages") == null) + { + getWhitelisterBotConfig().set("use-custom-messages", false); + + if(!configCreated) + { + getPlugin().getLogger().warning("Entry 'use-custom-messages' was not found, adding it to the config..."); + } + } + try { getWhitelisterBotConfig().save((whitelisterBotConfigFile.getPath())); @@ -458,6 +506,264 @@ public class DiscordWhitelister extends JavaPlugin } } + if(customMessagesFile.exists()) + { + /* TODO: add a YAML comment (#) explaining the config file params + NOTE: only {params} in the original messages will be evaluated. For example: using {MaxWhitelistAmount} in the "insufficient-permissions" String will not work as it was never in the original message. + {Sender} == author.getAsMention(), {RemainingWhitelists} == (maxWhitelistAmount - timesWhitelisted), {MaxWhitelistAmount} == maxWhitelistAmount, + {MinecraftUsername} == finalNameToAdd/Remove, {StaffMember} == DiscordWhitelister.getRemovedList().get(finalNameToAdd), {AddRemoveRoles} = DiscordWhitelister.getWhitelisterBotConfig().getList("add-remove-roles") + Internal error messages & info messages will remain uneditable. No need to add custom remove failure messages as it should never happen */ + + if(getCustomMessagesConfig().getString("insufficient-permissions-title") == null) + { + getCustomMessagesConfig().set("insufficient-permissions-title", "Insufficient Permissions"); + + if(!customMessagesCreated) + { + getPlugin().getLogger().warning("Entry 'insufficient-permissions-title' was not found, adding it to the config..."); + } + } + + if(getCustomMessagesConfig().getString("insufficient-permissions") == null) + { + getCustomMessagesConfig().set("insufficient-permissions", "{Sender}, you do not have permission to use this command."); + + if(!customMessagesCreated) + { + getPlugin().getLogger().warning("Entry 'insufficient-permissions' was not found, adding it to the config..."); + } + } + + if(getCustomMessagesConfig().getString("insufficient-permissions-remove-title") == null) + { + getCustomMessagesConfig().set("insufficient-permissions-remove-title", "Insufficient Permissions"); + + if(!customMessagesCreated) + { + getPlugin().getLogger().warning("Entry 'insufficient-permissions-remove-title' was not found, adding it to the config..."); + } + } + + if(getCustomMessagesConfig().getString("insufficient-permissions-remove") == null) + { + getCustomMessagesConfig().set("insufficient-permissions-remove", "{Sender}, you only have permission to add people to the whitelist. To remove people from the whitelist you must be moved to the following roles: {AddRemoveRoles}; or get the owner to move your role to 'add-remove-roles' in the config."); + + if(!customMessagesCreated) + { + getPlugin().getLogger().warning("Entry 'insufficient-permissions-remove' was not found, adding it to the config..."); + } + } + + if(getCustomMessagesConfig().getString("no-whitelists-remaining-title") == null) + { + getCustomMessagesConfig().set("no-whitelists-remaining-title", "No Whitelists Remaining"); + + if(!customMessagesCreated) + { + getPlugin().getLogger().warning("Entry 'no-whitelists-remaining-title' was not found, adding it to the config..."); + } + } + + if(getCustomMessagesConfig().getString("no-whitelists-remaining") == null) + { + getCustomMessagesConfig().set("no-whitelists-remaining", "{Sender}, unable to whitelist. You have **{RemainingWhitelists} out of {MaxWhitelistAmount}** whitelists remaining."); + + if(!customMessagesCreated) + { + getPlugin().getLogger().warning("Entry 'insufficient-permissions' was not found, adding it to the config..."); + } + } + + if(getCustomMessagesConfig().getString("invalid-characters-warning-title") == null) + { + getCustomMessagesConfig().set("invalid-characters-warning-title", "Invalid Username"); + + if(!customMessagesCreated) + { + getPlugin().getLogger().warning("Entry 'invalid-characters-warning-title' was not found, adding it to the config..."); + } + } + + if(getCustomMessagesConfig().getString("invalid-characters-warning") == null) + { + getCustomMessagesConfig().set("invalid-characters-warning", "{Sender}, the username you have specified contains invalid characters. **Only letters, numbers and underscores are allowed**."); + + if(!customMessagesCreated) + { + getPlugin().getLogger().warning("Entry 'invalid-characters-warning' was not found, adding it to the config..."); + } + } + + if(getCustomMessagesConfig().getString("invalid-length-warning-title") == null) + { + getCustomMessagesConfig().set("invalid-length-warning-title", "Invalid Username"); + + if(!customMessagesCreated) + { + getPlugin().getLogger().warning("Entry 'invalid-length-warning-title' was not found, adding it to the config..."); + } + } + + if(getCustomMessagesConfig().getString("invalid-length-warning") == null) + { + getCustomMessagesConfig().set("invalid-length-warning", "{Sender}, the username you have specified either contains too few or too many characters. **Usernames can only consist of 3-16 characters**."); + + if(!customMessagesCreated) + { + getPlugin().getLogger().warning("Entry 'invalid-length-warning' was not found, adding it to the config..."); + } + } + + if(getCustomMessagesConfig().getString("already-on-whitelist-title") == null) + { + getCustomMessagesConfig().set("already-on-whitelist-title", "User already on the whitelist"); + + if(!customMessagesCreated) + { + getPlugin().getLogger().warning("Entry 'already-on-whitelist-title' was not found, adding it to the config..."); + } + } + + if(getCustomMessagesConfig().getString("already-on-whitelist") == null) + { + getCustomMessagesConfig().set("already-on-whitelist", "{Sender}, cannot add user as `{MinecraftUsername}` is already on the whitelist!"); + + if(!customMessagesCreated) + { + getPlugin().getLogger().warning("Entry 'already-on-whitelist' was not found, adding it to the config..."); + } + } + + if(getCustomMessagesConfig().getString("user-was-removed-title") == null) + { + getCustomMessagesConfig().set("user-was-removed-title", "This user was previously removed by a staff member"); + + if(!customMessagesCreated) + { + getPlugin().getLogger().warning("Entry 'user-was-removed-title' was not found, adding it to the config..."); + } + } + + if(getCustomMessagesConfig().getString("user-was-removed") == null) + { + getCustomMessagesConfig().set("user-was-removed", "{Sender}, this user was previously removed by a staff member ({StaffMember}). Please ask a user with higher permissions to add this user."); + + if(!customMessagesCreated) + { + getPlugin().getLogger().warning("Entry 'user-was-removed' was not found, adding it to the config..."); + } + } + + if(getCustomMessagesConfig().getString("whitelists-remaining-title") == null) + { + getCustomMessagesConfig().set("whitelists-remaining-title", "Whitelists Remaining"); + + if(!customMessagesCreated) + { + getPlugin().getLogger().warning("Entry 'whitelists-remaining-title' was not found, adding it to the config..."); + } + } + + if(getCustomMessagesConfig().getString("whitelists-remaining") == null) + { + getCustomMessagesConfig().set("whitelists-remaining", "You have **{RemainingWhitelists} out of {MaxWhitelistAmount}** whitelists remaining."); + + if(!customMessagesCreated) + { + getPlugin().getLogger().warning("Entry 'whitelists-remaining' was not found, adding it to the config..."); + } + } + + if(getCustomMessagesConfig().getString("whitelist-success-title") == null) + { + getCustomMessagesConfig().set("whitelist-success-title", "{MinecraftUsername} is now whitelisted!"); + + if(!customMessagesCreated) + { + getPlugin().getLogger().warning("Entry 'whitelist-success-title' was not found, adding it to the config..."); + } + } + + if(getCustomMessagesConfig().getString("whitelist-success") == null) + { + getCustomMessagesConfig().set("whitelist-success", "{Sender} has added `{MinecraftUsername}` to the whitelist."); + + if(!customMessagesCreated) + { + getPlugin().getLogger().warning("Entry 'whitelist-success' was not found, adding it to the config..."); + } + } + + if(getCustomMessagesConfig().getString("whitelist-failure-title") == null) + { + getCustomMessagesConfig().set("whitelist-failure-title", "Failed to whitelist"); + + if(!customMessagesCreated) + { + getPlugin().getLogger().warning("Entry 'whitelist-failure-title' was not found, adding it to the config..."); + } + } + + if(getCustomMessagesConfig().getString("whitelist-failure") == null) + { + getCustomMessagesConfig().set("whitelist-failure", "{Sender}, failed to add `{MinecraftUsername}` to the whitelist. This is most likely due to an invalid Minecraft username."); + + if(!customMessagesCreated) + { + getPlugin().getLogger().warning("Entry 'whitelist-failure' was not found, adding it to the config..."); + } + } + + if(getCustomMessagesConfig().getString("user-not-on-whitelist-title") == null) + { + getCustomMessagesConfig().set("user-not-on-whitelist-title", "This user is not on the whitelist"); + + if(!customMessagesCreated) + { + getPlugin().getLogger().warning("Entry 'user-not-on-whitelist-title' was not found, adding it to the config..."); + } + } + + if(getCustomMessagesConfig().getString("user-not-on-whitelist") == null) + { + getCustomMessagesConfig().set("user-not-on-whitelist", "{Sender}, cannot remove user as `{MinecraftUsername}` is not on the whitelist!"); + + if(!customMessagesCreated) + { + getPlugin().getLogger().warning("Entry 'user-not-on-whitelist' was not found, adding it to the config..."); + } + } + + if(getCustomMessagesConfig().getString("remove-success-title") == null) + { + getCustomMessagesConfig().set("remove-success-title", "{MinecraftUsername} has been removed"); + + if(!customMessagesCreated) + { + getPlugin().getLogger().warning("Entry 'remove-success-title' was not found, adding it to the config..."); + } + } + + if(getCustomMessagesConfig().getString("remove-success") == null) + { + getCustomMessagesConfig().set("remove-success", "{Sender} has removed {MinecraftUsername} from the whitelist"); + + if(!customMessagesCreated) + { + getPlugin().getLogger().warning("Entry 'remove-success' was not found, adding it to the config..."); + } + } + + try + { + getCustomMessagesConfig().save((customMessagesFile.getPath())); + } + catch(IOException e) + { + e.printStackTrace(); + } + } + if(userListCreated) { try diff --git a/src/main/java/uk/co/angrybee/joe/VersionInfo.java b/src/main/java/uk/co/angrybee/joe/VersionInfo.java index 050a976..038da85 100644 --- a/src/main/java/uk/co/angrybee/joe/VersionInfo.java +++ b/src/main/java/uk/co/angrybee/joe/VersionInfo.java @@ -1,6 +1,7 @@ package uk.co.angrybee.joe; -public class VersionInfo { +public class VersionInfo +{ public static String getVersion() { return version; } @@ -9,5 +10,5 @@ public class VersionInfo { return "v." + getVersion(); } - private static String version = "1.3.3"; + private static String version = "1.3.4"; }