diff --git a/plugin.yml b/plugin.yml index cc1fa0f..8b9b6a1 100755 --- a/plugin.yml +++ b/plugin.yml @@ -1,5 +1,5 @@ name: DiscordWhitelister -version: 1.0.8 +version: 1.0.9 author: Joe Shimell main: uk.co.angrybee.joe.DiscordWhitelister description: Discord whitelister bot. \ No newline at end of file diff --git a/src/main/java/uk/co/angrybee/joe/ServerDiscordClient.java b/src/main/java/uk/co/angrybee/joe/ServerDiscordClient.java index d1b0af3..644b8f9 100755 --- a/src/main/java/uk/co/angrybee/joe/ServerDiscordClient.java +++ b/src/main/java/uk/co/angrybee/joe/ServerDiscordClient.java @@ -1,6 +1,7 @@ package uk.co.angrybee.joe; import net.dv8tion.jda.api.AccountType; +import net.dv8tion.jda.api.EmbedBuilder; import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.JDABuilder; import net.dv8tion.jda.api.entities.ChannelType; @@ -14,13 +15,15 @@ import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; import org.json.simple.JSONArray; import org.json.simple.JSONObject; +import org.json.simple.JSONValue; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; import javax.security.auth.login.LoginException; -import java.io.File; -import java.io.FileReader; -import java.io.IOException; +import java.awt.*; +import java.io.*; +import java.net.MalformedURLException; +import java.net.URL; import java.util.Arrays; import java.util.Objects; @@ -130,13 +133,16 @@ public class ServerDiscordClient extends ListenerAdapter { if(userCanAddRemove || userCanAdd) { - channel.sendMessage("```Discord Whitelister Bot For Spigot" + System.lineSeparator() + - "Version: 1.0.8" + System.lineSeparator() + "Links:" + System.lineSeparator() + - "https://www.spigotmc.org/resources/discord-whitelister.69929/" + System.lineSeparator() + "https://github.com/JoeShimo/DiscordWhitelisterBot-Spigot" + System.lineSeparator() + - "Commands:" + System.lineSeparator() + "Add:" + System.lineSeparator() + - "!whitelist add -- Usage: Adds a user to the whitelist" + System.lineSeparator() + - "Remove:" + System.lineSeparator() + "!whitelist remove -- Usage: Removes the target user from the whitelist" + System.lineSeparator() + - "If you encounter an issue, please report it here: https://github.com/JoeShimo/DiscordWhitelisterBot-Spigot/issues```").queue(); + EmbedBuilder embedBuilderInfo = new EmbedBuilder(); + embedBuilderInfo.setTitle("Discord Whitelister Bot for Spigot"); + embedBuilderInfo.addField("Version", "1.0.9", false); + embedBuilderInfo.addField("Links", ("https://www.spigotmc.org/resources/discord-whitelister.69929/" + System.lineSeparator() + + "https://github.com/JoeShimo/DiscordWhitelisterBot-Spigot"), false); + embedBuilderInfo.addField("Commands", ("**Add:** !whitelist add minecraftUsername" + System.lineSeparator() + + "**Remove:** !whitelist remove minecraftUsername"), false); + embedBuilderInfo.addField("Experiencing issues?", "If you encounter an issue, please report it here: https://github.com/JoeShimo/DiscordWhitelisterBot-Spigot/issues", false); + embedBuilderInfo.setColor(new Color(104, 109, 224)); + channel.sendMessage(embedBuilderInfo.build()).queue(); } } @@ -151,7 +157,10 @@ public class ServerDiscordClient extends ListenerAdapter // if the user doesn't have any allowed roles if(!hasPerms) { - channel.sendMessage(author.getAsMention() + ", you do not have permission to use this command").queue(); + 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(); } } @@ -183,9 +192,11 @@ public class ServerDiscordClient extends ListenerAdapter { if(finalNameToWhitelist.isEmpty()) { - channel.sendMessage(author.getAsMention() + ", ```Whitelist Command:" + System.lineSeparator() + - "!whitelist add " + System.lineSeparator() + "Usage: Adds a user to the whitelist" + System.lineSeparator() + - "If you encounter an issue, please report it here: https://github.com/JoeShimo/DiscordWhitelisterBot-Spigot/issues```").queue(); + EmbedBuilder embedBuilderInfo = new EmbedBuilder(); + embedBuilderInfo.addField("Whitelist Add Command", ("!whitelist add minecraftUsername" + System.lineSeparator() + System.lineSeparator() + + "If you encounter any issues, please report them here: https://github.com/JoeShimo/DiscordWhitelisterBot-Spigot/issues"), false); + embedBuilderInfo.setColor(new Color(104, 109, 224)); + channel.sendMessage(embedBuilderInfo.build()).queue(); } else { @@ -196,7 +207,10 @@ public class ServerDiscordClient extends ListenerAdapter { if(new String(validCharacters).indexOf(finalNameToWhitelistChar[a]) == -1) { - channel.sendMessage(author.getAsMention() + ", the username you have specified contains invalid characters. **Only letters, numbers and underscores are allowed**.").queue(); + EmbedBuilder embedBuilderFailure = new EmbedBuilder(); + embedBuilderFailure.addField("Invalid username", (author.getAsMention() + ", the username you have specified contains invalid characters. **Only letters, numbers and underscores are allowed**."), false); + embedBuilderFailure.setColor(new Color(231, 76, 60)); + channel.sendMessage(embedBuilderFailure.build()).queue(); return; } } @@ -204,7 +218,10 @@ public class ServerDiscordClient extends ListenerAdapter // length check if(finalNameToWhitelist.length() < 3 || finalNameToWhitelist.length() > 16) { - channel.sendMessage(author.getAsMention() + ", the username you have specified either contains too few or too many characters. **Usernames can only consist of 3-16 characters**.").queue(); + EmbedBuilder embedBuilderFailure = new EmbedBuilder(); + embedBuilderFailure.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); + embedBuilderFailure.setColor(new Color(231, 76, 60)); + channel.sendMessage(embedBuilderFailure.build()).queue(); return; } } @@ -235,13 +252,21 @@ public class ServerDiscordClient extends ListenerAdapter if(Objects.requireNonNull(tempFileConfiguration.getStringList("whitelisted")).contains(finalNameToWhitelist)) { onWhitelist = true; - channel.sendMessage(author.getAsMention() + ", user is already on the whitelist!").queue(); + + EmbedBuilder embedBuilderInfo = new EmbedBuilder(); + embedBuilderInfo.addField("This user is already on the whitelist", (author.getAsMention() + ", `" + finalNameToWhitelist + "` is already on the whitelist."), false); + embedBuilderInfo.setColor(new Color(104, 109, 224)); + channel.sendMessage(embedBuilderInfo.build()).queue(); } } else if(checkWhitelistJSON(whitelistJSON, finalNameToWhitelist)) { onWhitelist = true; - channel.sendMessage(author.getAsMention() + ", user is already on the whitelist!").queue(); + + EmbedBuilder embedBuilderInfo = new EmbedBuilder(); + embedBuilderInfo.addField("This user is already on the whitelist", (author.getAsMention() + ", `" + finalNameToWhitelist + "` is already on the whitelist."), false); + embedBuilderInfo.setColor(new Color(104, 109, 224)); + channel.sendMessage(embedBuilderInfo.build()).queue(); } if(!onWhitelist) @@ -264,20 +289,84 @@ public class ServerDiscordClient extends ListenerAdapter + "(" + author.getId() + ")"); } - if(DiscordWhitelister.useEasyWhitelist) - { - DiscordWhitelister.getPlugin().getServer().getScheduler().callSyncMethod(DiscordWhitelister.getPlugin(), () -> DiscordWhitelister.getPlugin().getServer().dispatchCommand(DiscordWhitelister.getPlugin().getServer().getConsoleSender(), - "easywl add " + finalNameToWhitelist)); - } - else + if(!DiscordWhitelister.useEasyWhitelist) { DiscordWhitelister.getPlugin().getServer().getScheduler().callSyncMethod(DiscordWhitelister.getPlugin(), () -> DiscordWhitelister.getPlugin().getServer().dispatchCommand(DiscordWhitelister.getPlugin().getServer().getConsoleSender(), "whitelist add " + finalNameToWhitelist)); } - // run through the server so that the check doesn't execute before the server has had a chance to run the whitelist command -- unsure if this is the best way of doing this, but it works + /* Do as much as possible off the main thread. + convert username into UUID to avoid depreciation and rate limits (according to https://minotar.net/) */ + URL playerURL = null; + String playerURLString = null; + String playerUUID = null; + + boolean invalidMinecraftName = false; + + try + { + playerURL = new URL("https://api.mojang.com/users/profiles/minecraft/" + finalNameToWhitelist); + } + catch (MalformedURLException e) + { + e.printStackTrace(); + } + + BufferedReader bufferedReader = null; + try + { + bufferedReader = new BufferedReader(new InputStreamReader(playerURL.openStream())); + playerURLString = bufferedReader.readLine(); + } + catch (IOException e) + { + e.printStackTrace(); + } + + if(playerURLString == null) + { + invalidMinecraftName = true; + } + + try + { + if(!invalidMinecraftName) + { + JSONObject playerUUIDObject = (JSONObject) JSONValue.parseWithException(playerURLString); + playerUUID = playerUUIDObject.get("id").toString(); + } + } + catch (ParseException e) + { + e.printStackTrace(); + } + + if(invalidMinecraftName) + { + playerUUID = ""; + } + + String finalPlayerURLString = playerURLString; + + // 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((finalNameToWhitelist + " is now whitelisted!"), (author.getAsMention() + " has added `" + finalNameToWhitelist + "` to the whitelist."), false); + embedBuilderSuccess.setColor(new Color(46, 204, 113)); + embedBuilderSuccess.setThumbnail("https://minotar.net/bust/" + playerUUID + "/100.png"); + + EmbedBuilder embedBuilderFailure = new EmbedBuilder(); + embedBuilderFailure.addField("Failed to Whitelist", (author.getAsMention() + ", failed to add `" + finalNameToWhitelist + "` to the whitelist. This is most likely due to an invalid Minecraft username."), false); + embedBuilderFailure.setColor(new Color(231, 76, 60)); + if(DiscordWhitelister.useEasyWhitelist) { + if(finalPlayerURLString != null) // have to do this else the easy whitelist plugin will add the name regardless of whether it is valid on not + { + DiscordWhitelister.getPlugin().getServer().getScheduler().callSyncMethod(DiscordWhitelister.getPlugin(), () -> DiscordWhitelister.getPlugin().getServer().dispatchCommand(DiscordWhitelister.getPlugin().getServer().getConsoleSender(), + "easywl add " + finalNameToWhitelist)); + } + + // run through the server so that the check doesn't execute before the server has had a chance to run the whitelist command -- unsure if this is the best way of doing this, but it works DiscordWhitelister.getPlugin().getServer().getScheduler().callSyncMethod(DiscordWhitelister.getPlugin(), () -> { try @@ -289,13 +378,14 @@ public class ServerDiscordClient extends ListenerAdapter e.printStackTrace(); } - if(Objects.requireNonNull(tempFileConfiguration.getStringList("whitelisted")).contains(finalNameToWhitelist)) + if(finalPlayerURLString != null && Objects.requireNonNull(tempFileConfiguration.getStringList("whitelisted")).contains(finalNameToWhitelist)) { - channel.sendMessage(author.getAsMention() + ", successfully added `" + finalNameToWhitelist + "` to the whitelist.").queue(); + + channel.sendMessage(embedBuilderSuccess.build()).queue(); } else { - channel.sendMessage(author.getAsMention() + ", failed to add `" + finalNameToWhitelist + "` to the whitelist, this is most likely due to an invalid Minecraft username.").queue(); + channel.sendMessage(embedBuilderFailure.build()).queue(); } return null; }); @@ -306,11 +396,11 @@ public class ServerDiscordClient extends ListenerAdapter { if(checkWhitelistJSON(whitelistJSON, finalNameToWhitelist)) { - channel.sendMessage(author.getAsMention() + ", successfully added `" + finalNameToWhitelist + "` to the whitelist.").queue(); + channel.sendMessage(embedBuilderSuccess.build()).queue(); } else { - channel.sendMessage(author.getAsMention() + ", failed to add `" + finalNameToWhitelist + "` to the whitelist, this is most likely due to an invalid Minecraft username.").queue(); + channel.sendMessage(embedBuilderFailure.build()).queue(); } return null; }); @@ -328,10 +418,13 @@ public class ServerDiscordClient extends ListenerAdapter if(finalNameToWhitelist.isEmpty()) { - channel.sendMessage(author.getAsMention() + ", ```Whitelist Command:" + System.lineSeparator() + - "!whitelist add " + System.lineSeparator() + "Usage: Adds a user to the whitelist" + "```" + System.lineSeparator() - + " You have **" + (whitelistLimit - timesWhitelisted) - + " out of " + DiscordWhitelister.getWhitelisterBotConfig().getString("max-whitelist-amount") + "** whitelists remaining").queue(); + EmbedBuilder embedBuilderInfo = new EmbedBuilder(); + embedBuilderInfo.addField("Whitelist Add Command", ("!whitelist add minecraftUsername" + System.lineSeparator() + System.lineSeparator() + + "If you encounter any issues, please report them here: https://github.com/JoeShimo/DiscordWhitelisterBot-Spigot/issues" + System.lineSeparator()), false); + embedBuilderInfo.addField("Whitelists Remaining", ("You have **" + (whitelistLimit - timesWhitelisted) + + " out of " + DiscordWhitelister.getWhitelisterBotConfig().getString("max-whitelist-amount") + "** whitelists remaining."), false); + embedBuilderInfo.setColor(new Color(104, 109, 224)); + channel.sendMessage(embedBuilderInfo.build()).queue(); } else { @@ -342,9 +435,13 @@ public class ServerDiscordClient extends ListenerAdapter { if(new String(validCharacters).indexOf(finalNameToWhitelistChar[a]) == -1) { - channel.sendMessage(author.getAsMention() + ", the username you have specified contains invalid characters. **Only letters, numbers and underscores are allowed**." - + " You have **" + (whitelistLimit - timesWhitelisted) - + " out of " + DiscordWhitelister.getWhitelisterBotConfig().getString("max-whitelist-amount") + "** whitelists remaining.").queue(); + EmbedBuilder embedBuilderFailure = new EmbedBuilder(); + embedBuilderFailure.addField("Invalid username", (author.getAsMention() + ", the username you have specified contains invalid characters. **Only letters, numbers and underscores are allowed**." + + System.lineSeparator()), false); + embedBuilderFailure.addField("Whitelists Remaining", ("You have **" + (whitelistLimit - timesWhitelisted) + + " out of " + DiscordWhitelister.getWhitelisterBotConfig().getString("max-whitelist-amount") + "** whitelists remaining."), false); + embedBuilderFailure.setColor(new Color(231, 76, 60)); + channel.sendMessage(embedBuilderFailure.build()).queue(); return; } } @@ -352,9 +449,13 @@ public class ServerDiscordClient extends ListenerAdapter // length check if(finalNameToWhitelist.length() < 3 || finalNameToWhitelist.length() > 16) { - channel.sendMessage(author.getAsMention() + ", the username you have specified either contains too few or too many characters. **Usernames can only consist of 3-16 characters**." - + " You have **" + (whitelistLimit - timesWhitelisted) - + " out of " + DiscordWhitelister.getWhitelisterBotConfig().getString("max-whitelist-amount") + "** whitelists remaining.").queue(); + EmbedBuilder embedBuilderFailure = new EmbedBuilder(); + embedBuilderFailure.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**." + + System.lineSeparator()), false); + embedBuilderFailure.addField("Whitelists Remaining", ("You have **" + (whitelistLimit - timesWhitelisted) + + " out of " + DiscordWhitelister.getWhitelisterBotConfig().getString("max-whitelist-amount") + "** whitelists remaining."), false); + embedBuilderFailure.setColor(new Color(231, 76, 60)); + channel.sendMessage(embedBuilderFailure.build()).queue(); return; } } @@ -385,38 +486,112 @@ public class ServerDiscordClient extends ListenerAdapter { if(Objects.requireNonNull(tempFileConfiguration.getStringList("whitelisted")).contains(finalNameToWhitelist)) { - channel.sendMessage(author.getAsMention() + ", user is already on the whitelist!").queue(); onWhitelist = true; + + EmbedBuilder embedBuilderInfo = new EmbedBuilder(); + embedBuilderInfo.addField("This user is already on the whitelist", (author.getAsMention() + ", `" + finalNameToWhitelist + "` is already on the whitelist."), false); + embedBuilderInfo.setColor(new Color(104, 109, 224)); + channel.sendMessage(embedBuilderInfo.build()).queue(); } } else if(checkWhitelistJSON(whitelistJSON, finalNameToWhitelist)) { onWhitelist = true; - channel.sendMessage(author.getAsMention() + ", user is already on the whitelist!").queue(); + + EmbedBuilder embedBuilderInfo = new EmbedBuilder(); + embedBuilderInfo.addField("This user is already on the whitelist", (author.getAsMention() + ", cannot add user as `" + finalNameToWhitelist + "` is already on the whitelist!"), false); + embedBuilderInfo.setColor(new Color(104, 109, 224)); + channel.sendMessage(embedBuilderInfo.build()).queue(); } if(!onWhitelist) { if(DiscordWhitelister.getRemovedList().get(finalNameToWhitelist) != null) { - channel.sendMessage(author.getAsMention() + ", cannot add `" + finalNameToWhitelist + "` as this user was previously removed by a staff member (<@" - + DiscordWhitelister.getRemovedList().get(finalNameToWhitelist) + ">). Please ask a user with higher permissions to add this user." - + " You have **" + (whitelistLimit - timesWhitelisted) - + " out of " + DiscordWhitelister.getWhitelisterBotConfig().getString("max-whitelist-amount") + "** whitelists remaining.").queue(); + EmbedBuilder embedBuilderFailure = new EmbedBuilder(); + embedBuilderFailure.addField("This user was previously removed by a staff member", + (author.getAsMention() + ", this user was previously removed by a staff member (<@" + DiscordWhitelister.getRemovedList().get(finalNameToWhitelist) + ">)." + + System.lineSeparator() + "Please ask a user with higher permissions to add this user." + System.lineSeparator()), false); + embedBuilderFailure.addField("Whitelists Remaining", ("You have **" + (whitelistLimit - timesWhitelisted) + + " out of " + DiscordWhitelister.getWhitelisterBotConfig().getString("max-whitelist-amount") + "** whitelists remaining."), false); + embedBuilderFailure.setColor(new Color(231, 76, 60)); + channel.sendMessage(embedBuilderFailure.build()).queue(); } else { - if(DiscordWhitelister.useEasyWhitelist) - { - DiscordWhitelister.getPlugin().getServer().getScheduler().callSyncMethod(DiscordWhitelister.getPlugin(), () -> DiscordWhitelister.getPlugin().getServer().dispatchCommand(DiscordWhitelister.getPlugin().getServer().getConsoleSender(), - "easywl add " + finalNameToWhitelist)); - } - else + if(!DiscordWhitelister.useEasyWhitelist) { DiscordWhitelister.getPlugin().getServer().getScheduler().callSyncMethod(DiscordWhitelister.getPlugin(), () -> DiscordWhitelister.getPlugin().getServer().dispatchCommand(DiscordWhitelister.getPlugin().getServer().getConsoleSender(), "whitelist add " + finalNameToWhitelist)); } + /* Do as much as possible off the main thread. + convert username into UUID to avoid depreciation and rate limits (according to https://minotar.net/) */ + URL playerURL = null; + String playerURLString = null; + String playerUUID = null; + + boolean invalidMinecraftName = false; + + try + { + playerURL = new URL("https://api.mojang.com/users/profiles/minecraft/" + finalNameToWhitelist); + } + catch (MalformedURLException e) + { + e.printStackTrace(); + } + + BufferedReader bufferedReader = null; + try + { + bufferedReader = new BufferedReader(new InputStreamReader(playerURL.openStream())); + playerURLString = bufferedReader.readLine(); + } + catch (IOException e) + { + e.printStackTrace(); + } + + if(playerURLString == null) + { + invalidMinecraftName = true; + } + + try + { + if(!invalidMinecraftName) + { + JSONObject playerUUIDObject = (JSONObject) JSONValue.parseWithException(playerURLString); + playerUUID = playerUUIDObject.get("id").toString(); + } + } + catch (ParseException e) + { + e.printStackTrace(); + } + + if(invalidMinecraftName) + { + playerUUID = ""; + } + + String finalPlayerURLString = playerURLString; + + // 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((finalNameToWhitelist + " is now whitelisted!"), (author.getAsMention() + " has added `" + finalNameToWhitelist + "` to the whitelist."), false); + embedBuilderSuccess.addField("Whitelists Remaining", ("You have **" + (whitelistLimit - (timesWhitelisted + 1)) + + " out of " + DiscordWhitelister.getWhitelisterBotConfig().getString("max-whitelist-amount") + "** whitelists remaining."), false); + embedBuilderSuccess.setColor(new Color(46, 204, 113)); + embedBuilderSuccess.setThumbnail("https://minotar.net/bust/" + playerUUID + "/100.png"); + + EmbedBuilder embedBuilderFailure = new EmbedBuilder(); + embedBuilderFailure.addField("Failed to Whitelist", (author.getAsMention() + ", failed to add `" + finalNameToWhitelist + "` to the whitelist. This is most likely due to an invalid Minecraft username."), false); + embedBuilderFailure.addField("Whitelists Remaining", ("You have **" + (whitelistLimit - timesWhitelisted) + + " out of " + DiscordWhitelister.getWhitelisterBotConfig().getString("max-whitelist-amount") + "** whitelists remaining."), false); + embedBuilderFailure.setColor(new Color(231, 76, 60)); + int tempFinal = timesWhitelisted; if(tempFinal < 3) @@ -429,9 +604,15 @@ public class ServerDiscordClient extends ListenerAdapter int successfulFinalTimesWhitelisted = whitelistLimit - finalTimesWhitelistedInc; int failedFinalTimesWhitelisted = whitelistLimit - timesWhitelisted; - // run through the server so that the check doesn't execute before the server has had a chance to run the whitelist command -- unsure if this is the best way of doing this, but it works if(DiscordWhitelister.useEasyWhitelist) { + if(finalPlayerURLString != null) // have to do this else the easy whitelist plugin will add the name regardless of whether it is valid on not + { + DiscordWhitelister.getPlugin().getServer().getScheduler().callSyncMethod(DiscordWhitelister.getPlugin(), () -> DiscordWhitelister.getPlugin().getServer().dispatchCommand(DiscordWhitelister.getPlugin().getServer().getConsoleSender(), + "easywl add " + finalNameToWhitelist)); + } + + // run through the server so that the check doesn't execute before the server has had a chance to run the whitelist command -- unsure if this is the best way of doing this, but it works DiscordWhitelister.getPlugin().getServer().getScheduler().callSyncMethod(DiscordWhitelister.getPlugin(), () -> { try @@ -443,13 +624,11 @@ public class ServerDiscordClient extends ListenerAdapter e.printStackTrace(); } - if(Objects.requireNonNull(tempFileConfiguration.getStringList("whitelisted")).contains(finalNameToWhitelist)) + if(finalPlayerURLString != null && Objects.requireNonNull(tempFileConfiguration.getStringList("whitelisted")).contains(finalNameToWhitelist)) { - channel.sendMessage(author.getAsMention() + ", successfully added `" + finalNameToWhitelist + "` to the whitelist." - + " You have **" + successfulFinalTimesWhitelisted - + " out of " + DiscordWhitelister.getWhitelisterBotConfig().getString("max-whitelist-amount") + "** whitelists remaining").queue(); + //DiscordWhitelister.getPlugin().getLogger().info(author.getName() + "(" + author.getId() + ") successfully added " + finalNameToWhitelist); - DiscordWhitelister.getUserList().set(author.getId(), finalTimesWhitelistedInc); + channel.sendMessage(embedBuilderSuccess.build()).queue(); try { @@ -460,14 +639,14 @@ public class ServerDiscordClient extends ListenerAdapter e.printStackTrace(); } + DiscordWhitelister.getUserList().set(author.getId(), finalTimesWhitelistedInc); + DiscordWhitelister.getPlugin().getLogger().info(author.getName() + "(" + author.getId() + ") successfully added " + finalNameToWhitelist + " to the whitelist, " + successfulFinalTimesWhitelisted + " whitelists remaining."); } else { - channel.sendMessage(author.getAsMention() + ", failed to add `" + finalNameToWhitelist + "` to the whitelist, this is most likely due to an invalid Minecraft username." - + " You have **" + failedFinalTimesWhitelisted - + " out of " + DiscordWhitelister.getWhitelisterBotConfig().getString("max-whitelist-amount") + "** whitelists remaining.").queue(); + channel.sendMessage(embedBuilderFailure.build()).queue(); } return null; }); @@ -478,9 +657,7 @@ public class ServerDiscordClient extends ListenerAdapter { if(checkWhitelistJSON(whitelistJSON, finalNameToWhitelist)) { - channel.sendMessage(author.getAsMention() + ", successfully added `" + finalNameToWhitelist + "` to the whitelist." - + " You have **" + successfulFinalTimesWhitelisted - + " out of " + DiscordWhitelister.getWhitelisterBotConfig().getString("max-whitelist-amount") + "** whitelists remaining").queue(); + channel.sendMessage(embedBuilderSuccess.build()).queue(); DiscordWhitelister.getUserList().set(author.getId(), finalTimesWhitelistedInc); @@ -498,9 +675,7 @@ public class ServerDiscordClient extends ListenerAdapter } else { - channel.sendMessage(author.getAsMention() + ", failed to add `" + finalNameToWhitelist + "` to the whitelist, this is most likely due to an invalid Minecraft username." - + " You have **" + failedFinalTimesWhitelisted - + " out of " + DiscordWhitelister.getWhitelisterBotConfig().getString("max-whitelist-amount") + "** whitelists remaining.").queue(); + channel.sendMessage(embedBuilderFailure.build()).queue(); } return null; }); @@ -512,8 +687,11 @@ public class ServerDiscordClient extends ListenerAdapter } else if(userHasLimitedAdd && usedAllWhitelists) { - channel.sendMessage(author.getAsMention() + ", unable to whitelist. You have **" + (DiscordWhitelister.getWhitelisterBotConfig().getInt("max-whitelist-amount") - Integer.parseInt(DiscordWhitelister.getUserList().getString(author.getId()))) - + " out of " + DiscordWhitelister.getWhitelisterBotConfig().getInt("max-whitelist-amount") + "** whitelists remaining.").queue(); + EmbedBuilder embedBuilderInfo = new EmbedBuilder(); + embedBuilderInfo.addField("No Whitelists Remaining", (author.getAsMention() + ", unable to whitelist. You have **" + (DiscordWhitelister.getWhitelisterBotConfig().getInt("max-whitelist-amount") - Integer.parseInt(DiscordWhitelister.getUserList().getString(author.getId()))) + + " out of " + DiscordWhitelister.getWhitelisterBotConfig().getInt("max-whitelist-amount") + "** whitelists remaining."), false); + embedBuilderInfo.setColor(new Color(104, 109, 224)); + channel.sendMessage(embedBuilderInfo.build()).queue(); } } @@ -528,7 +706,12 @@ public class ServerDiscordClient extends ListenerAdapter // if the user doesn't have any allowed roles if(!hasPerms) { - channel.sendMessage(author.getAsMention() + ", you do not have permission to use this command").queue(); + //channel.sendMessage(author.getAsMention() + ", you do not have permission to use this command").queue(); + + 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(); } } } @@ -546,9 +729,11 @@ public class ServerDiscordClient extends ListenerAdapter if(finalNameToRemove.isEmpty()) { - channel.sendMessage(author.getAsMention() + ", ```Whitelist Remove Command:" + System.lineSeparator() + - "!whitelist remove " + System.lineSeparator() + "Usage: Removes the target user from the whitelist" + System.lineSeparator() + - "If you encounter an issue, please report it here: https://github.com/JoeShimo/DiscordWhitelisterBot-Spigot/issues```").queue(); + EmbedBuilder embedBuilderInfo = new EmbedBuilder(); + embedBuilderInfo.addField("Whitelist Remove Command", ("!whitelist remove minecraftUsername" + System.lineSeparator() + System.lineSeparator() + + "If you encounter any issues, please report them here: https://github.com/JoeShimo/DiscordWhitelisterBot-Spigot/issues"), false); + embedBuilderInfo.setColor(new Color(104, 109, 224)); + channel.sendMessage(embedBuilderInfo.build()).queue(); } else { @@ -577,15 +762,23 @@ public class ServerDiscordClient extends ListenerAdapter { if(!tempFileConfiguration.getStringList("whitelisted").contains(finalNameToRemove)) { - channel.sendMessage(author.getAsMention() + ", `" + finalNameToRemove + "` is not on the whitelist!").queue(); 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)); + channel.sendMessage(embedBuilderInfo.build()).queue(); } } if(!DiscordWhitelister.useEasyWhitelist && !checkWhitelistJSON(whitelistJSON, finalNameToRemove)) { notOnWhitelist = true; - channel.sendMessage(author.getAsMention() + ", `" + finalNameToRemove + "` is not on the whitelist!").queue(); + + 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)); + channel.sendMessage(embedBuilderInfo.build()).queue(); } if(!notOnWhitelist) @@ -610,6 +803,16 @@ public class ServerDiscordClient extends ListenerAdapter "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)); + + EmbedBuilder embedBuilderFailure = new EmbedBuilder(); + 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(), () -> @@ -625,7 +828,7 @@ public class ServerDiscordClient extends ListenerAdapter if(!tempFileConfiguration.getStringList("whitelisted").contains(finalNameToRemove)) { - channel.sendMessage(author.getAsMention() + ", successfully removed `" + finalNameToRemove + "` from the whitelist.").queue(); + channel.sendMessage(embedBuilderSuccess.build()).queue(); // if the name is not on the list if(DiscordWhitelister.getRemovedList().get(finalNameToRemove) == null) @@ -636,7 +839,7 @@ public class ServerDiscordClient extends ListenerAdapter } else { - channel.sendMessage(author.getAsMention() + ", failed to remove `" + finalNameToRemove + "` from the whitelist, this should never really happen, you may have to remove the player manually and report the issue.").queue(); + channel.sendMessage(embedBuilderFailure.build()).queue(); } return null; }); @@ -647,7 +850,7 @@ public class ServerDiscordClient extends ListenerAdapter { if(!checkWhitelistJSON(whitelistJSON, finalNameToRemove)) { - channel.sendMessage(author.getAsMention() + ", successfully removed `" + finalNameToRemove + "` from the whitelist.").queue(); + channel.sendMessage(embedBuilderSuccess.build()).queue(); // if the name is not on the list if(DiscordWhitelister.getRemovedList().get(finalNameToRemove) == null) @@ -658,7 +861,7 @@ public class ServerDiscordClient extends ListenerAdapter } else { - channel.sendMessage(author.getAsMention() + ", failed to remove `" + finalNameToRemove + "` from the whitelist, this should never really happen, you may have to remove the player manually and report the issue.").queue(); + channel.sendMessage(embedBuilderFailure.build()).queue(); } return null; }); @@ -670,9 +873,19 @@ public class ServerDiscordClient extends ListenerAdapter if(userCanAdd && !userCanAddRemove) { - channel.sendMessage(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: " - + DiscordWhitelister.getWhitelisterBotConfig().getList("add-remove-roles").toString() + "; or get the owner to move your role to 'add-remove-roles' in the config.").queue(); +// channel.sendMessage(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: " +// + DiscordWhitelister.getWhitelisterBotConfig().getList("add-remove-roles").toString() + "; or get the owner to move your role to 'add-remove-roles' in the config.").queue(); + + 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)); + channel.sendMessage(embedBuilderInfo.build()).queue(); } if(messageContents.toLowerCase().contains("!whitelist remove") && !author.isBot()) @@ -686,7 +899,10 @@ public class ServerDiscordClient extends ListenerAdapter // if the user doesn't have any allowed roles if(!hasPerms) { - channel.sendMessage(author.getAsMention() + ", you do not have permission to use this command").queue(); + 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(); } } }