From 2636cb47f8e8d39b93746244364db8f29f575206 Mon Sep 17 00:00:00 2001 From: vacer25 Date: Fri, 24 Apr 2020 20:53:18 -0400 Subject: [PATCH] Made !whitelist add/remove commands required to be at start of message. Made the name be the text up to the first space following the command (everything else is ignored). This allows writing something like "@user please do !whitelist add " without triggering the bot. Fixed error that occurred when users leave guild which did not use the plugin to add to the whitelist. Fixed version not displaying correctly (was still showing 1.2.0). Made GitHub link in /dwabout command green. --- plugin.yml | 6 +- .../angrybee/joe/Commands/CommandAbout.java | 4 +- .../uk/co/angrybee/joe/DiscordClient.java | 55 ++++++++++--------- .../java/uk/co/angrybee/joe/VersionInfo.java | 2 +- 4 files changed, 35 insertions(+), 32 deletions(-) diff --git a/plugin.yml b/plugin.yml index 7ac02b1..c5de53e 100755 --- a/plugin.yml +++ b/plugin.yml @@ -1,14 +1,14 @@ name: DiscordWhitelister -version: 1.3.0 +version: 1.3.1 author: Joe Shimell main: uk.co.angrybee.joe.DiscordWhitelister description: Discord whitelister bot. commands: discordwhitelister: - description: See VersionInfo and Discord status + description: See info about this plugin usage: / aliases: [dw] discordwhitelisterabout: - description: See VersionInfo and Discord status + description: See plugin version and Discord connection status usage: / aliases: [dwabout] \ No newline at end of file diff --git a/src/main/java/uk/co/angrybee/joe/Commands/CommandAbout.java b/src/main/java/uk/co/angrybee/joe/Commands/CommandAbout.java index b3cd694..2db7cce 100644 --- a/src/main/java/uk/co/angrybee/joe/Commands/CommandAbout.java +++ b/src/main/java/uk/co/angrybee/joe/Commands/CommandAbout.java @@ -10,11 +10,11 @@ import uk.co.angrybee.joe.VersionInfo; public class CommandAbout implements CommandExecutor { - // /dw + // /dwabout // about command @Override public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { - sender.sendMessage("[DW] DiscordWhiteLister by JoeShimell\nhttps://github.com/JoeShimell/DiscordWhitelisterSpigot"); + sender.sendMessage("[DW] DiscordWhiteLister by JoeShimell\n" + ChatColor.GREEN + "https://github.com/JoeShimell/DiscordWhitelisterSpigot"); return true; } } diff --git a/src/main/java/uk/co/angrybee/joe/DiscordClient.java b/src/main/java/uk/co/angrybee/joe/DiscordClient.java index 85e3860..8338eb5 100644 --- a/src/main/java/uk/co/angrybee/joe/DiscordClient.java +++ b/src/main/java/uk/co/angrybee/joe/DiscordClient.java @@ -157,7 +157,7 @@ public class DiscordClient extends ListenerAdapter { } } - if (messageContents.toLowerCase().contains("!whitelist add")) { + if (messageContents.toLowerCase().startsWith("!whitelist add")) { // add command // permissions @@ -197,11 +197,10 @@ public class DiscordClient extends ListenerAdapter { } if (authorPermissions.isUserCanAddRemove() || authorPermissions.isUserCanAdd() || limitedWhitelistEnabled && authorPermissions.isUserHasLimitedAdd()) { - String nameToWhitelist = messageContents.toLowerCase(); - nameToWhitelist = nameToWhitelist.substring(nameToWhitelist.indexOf("!whitelist add") + 14); // get everything after !whitelist add - nameToWhitelist = nameToWhitelist.replaceAll(" ", ""); + 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 - final String finalNameToAdd = nameToWhitelist; final char[] finalNameToWhitelistChar = finalNameToAdd.toCharArray(); int timesWhitelisted = 0; @@ -438,13 +437,11 @@ public class DiscordClient extends ListenerAdapter { } } - if (messageContents.toLowerCase().contains("!whitelist remove")) { + if (messageContents.toLowerCase().startsWith("!whitelist remove")) { if (authorPermissions.isUserCanAddRemove()) { - String nameToRemove = messageContents.toLowerCase(); - nameToRemove = nameToRemove.substring(nameToRemove.indexOf("!whitelist remove") + 17); // get everything after !whitelist remove - nameToRemove = nameToRemove.replaceAll(" ", ""); - - final String finalNameToRemove = nameToRemove; + messageContents = messageContents.toLowerCase(); + String messageContentsAfterCommand = messageContents.substring("!whitelist remove".length() + 1); // get everything after !whitelist add[space] + final String finalNameToRemove = messageContentsAfterCommand.replaceAll(" .*", ""); // The name is everything up to the first space if (finalNameToRemove.isEmpty()) { channel.sendMessage(removeCommandInfo).queue(); @@ -596,7 +593,6 @@ public class DiscordClient extends ListenerAdapter { embedBuilderFailure.setColor(new Color(231, 76, 60)); embedBuilderFailure.addField("Insufficient Permissions", (author.getAsMention() + ", you do not have permission to use this command."), false); channel.sendMessage(embedBuilderFailure.build()).queue(); - return; } } } @@ -604,24 +600,31 @@ public class DiscordClient extends ListenerAdapter { @Override public void onGuildMemberLeave(@Nonnull GuildMemberLeaveEvent event) { String discordUserToRemove = event.getMember().getId(); - DiscordWhitelister.getPlugin().getLogger().info(discordUserToRemove + " left. Removing his whitelisted entries."); + DiscordWhitelister.getPlugin().getLogger().info(discordUserToRemove + " left. Removing their whitelisted entries..."); List ls = DiscordWhitelister.getRegisteredUsers(discordUserToRemove); - for (Object minecraftNameToRemove: ls) { - DiscordWhitelister.getPlugin().getLogger().info(minecraftNameToRemove.toString() + " left. Removing his whitelisted entries."); - if (DiscordWhitelister.useEasyWhitelist) { - executeServerCommand("easywl remove " + minecraftNameToRemove.toString()); - } else { - executeServerCommand("whitelist remove " + minecraftNameToRemove.toString()); + + if(ls != null) { + + for (Object minecraftNameToRemove : ls) { + DiscordWhitelister.getPlugin().getLogger().info(minecraftNameToRemove.toString() + " left. Removing their whitelisted entries."); + if (DiscordWhitelister.useEasyWhitelist) { + executeServerCommand("easywl remove " + minecraftNameToRemove.toString()); + } else { + executeServerCommand("whitelist remove " + minecraftNameToRemove.toString()); + } } + try { + DiscordWhitelister.resetRegisteredUsers(discordUserToRemove); + } catch (IOException e) { + e.printStackTrace(); + return; + } + DiscordWhitelister.getPlugin().getLogger().info(discordUserToRemove + " left. Successfully removed their whitelisted entries."); + } - try { - DiscordWhitelister.resetRegisteredUsers(discordUserToRemove); - } catch (IOException e) { - e.printStackTrace(); - return; + else { + DiscordWhitelister.getPlugin().getLogger().warning(discordUserToRemove + " left. Could not removed their whitelisted entries as they did not whitelist through this plugin."); } - DiscordWhitelister.getPlugin().getLogger().info(discordUserToRemove + " left. Successfully removed his whitelisted entries."); - return; } private boolean checkWhitelistJSON(File whitelistFile, String minecraftUsername) { diff --git a/src/main/java/uk/co/angrybee/joe/VersionInfo.java b/src/main/java/uk/co/angrybee/joe/VersionInfo.java index 16705c1..87f7395 100644 --- a/src/main/java/uk/co/angrybee/joe/VersionInfo.java +++ b/src/main/java/uk/co/angrybee/joe/VersionInfo.java @@ -9,5 +9,5 @@ public class VersionInfo { return "v." + getVersion(); } - private static String version = "1.2.0"; + private static String version = "1.3.1"; }