diff --git a/README.md b/README.md index fcfd785..7913b6e 100755 --- a/README.md +++ b/README.md @@ -1,9 +1,12 @@ # DiscordWhitelisterBot-Spigot A simple spigot plugin which allows whitelisting through a discord text channel. +This provides an easy way for staff members on a server to whitelist outside the server whilst not having full access to the console. Features: -- use '!whitelist ' in a valid channel to whitelist people on your minecraft server +- use '!whitelist add ' in a valid channel to whitelist people on your minecraft server +- use '!whitelist remove ' in a valid channel to remove people from the whitelist on your minecraft server +- use '!whitelist' in a valid channel to get info about the bot and how to use it - only select Discord roles can whitelist through the bot - bot only listens for messages in select text channels - logs whitelist attempts from valid roles in the console diff --git a/plugin.yml b/plugin.yml index 9a3aad0..aa1def6 100755 --- a/plugin.yml +++ b/plugin.yml @@ -1,5 +1,5 @@ name: DiscordWhitelister -version: 1.0.1 +version: 1.0.2 author: Joe Shimell main: uk.co.angrybee.joe.DiscordWhitelister description: Discord whitelister bot. diff --git a/src/main/java/uk/co/angrybee/joe/ServerDiscordClient.java b/src/main/java/uk/co/angrybee/joe/ServerDiscordClient.java index b5cfbad..24b69c4 100755 --- a/src/main/java/uk/co/angrybee/joe/ServerDiscordClient.java +++ b/src/main/java/uk/co/angrybee/joe/ServerDiscordClient.java @@ -73,26 +73,41 @@ public void onMessageReceived(MessageReceivedEvent messageReceivedEvent) } } - if(messageContents.toLowerCase().contains("!whitelist") && userHasPerms) + if(messageContents.toLowerCase().equals("!whitelist") && userHasPerms) + { + channel.sendMessage("```Discord Whitelister Bot For Spigot" + System.lineSeparator() + + "Version: 1.0.2" + 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() + "!whitelistremove -- 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(); + } + else if(messageContents.toLowerCase().equals("!whitelist") && !userHasPerms && !author.isBot()) + { + channel.sendMessage(author.getAsMention() + ", you do not have permission to use this command").queue(); + } + + if(messageContents.toLowerCase().contains("!whitelist add") && userHasPerms) { String nameToWhitelist = messageContents; nameToWhitelist = nameToWhitelist.toLowerCase(); - nameToWhitelist = nameToWhitelist.substring(nameToWhitelist.indexOf("!whitelist")+10); // get everything after !whitelist + nameToWhitelist = nameToWhitelist.substring(nameToWhitelist.indexOf("!whitelist add")+14); // get everything after !whitelist add nameToWhitelist = nameToWhitelist.replaceAll(" ", ""); final String finalNameToWhitelist = nameToWhitelist; if(finalNameToWhitelist.isEmpty()) { - channel.sendMessage(author.getAsMention() + ", Whitelist command usage:" + System.lineSeparator() + - "```!whitelist " + System.lineSeparator() + + 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(); } else { File whitelistJSON = (new File(".", "whitelist.json")); - DiscordWhitelister.getPlugin().getLogger().info(author.getName() + "(" + author.getId() + ") attempted to whitelist " + nameToWhitelist); + DiscordWhitelister.getPlugin().getLogger().info(author.getName() + "(" + author.getId() + ") attempted to whitelist " + finalNameToWhitelist); if(checkWhitelistJSON(whitelistJSON, finalNameToWhitelist)) { @@ -119,7 +134,57 @@ public void onMessageReceived(MessageReceivedEvent messageReceivedEvent) } } } - else if(messageContents.toLowerCase().contains("!whitelist") && !userHasPerms && !messageReceivedEvent.getAuthor().isBot()) + else if(messageContents.toLowerCase().contains("!whitelist add") && !userHasPerms && !author.isBot()) + { + channel.sendMessage(author.getAsMention() + ", you do not have permission to use this command").queue(); + } + + if(messageContents.toLowerCase().contains("!whitelist remove") && userHasPerms) + { + String nameToRemove = messageContents; + nameToRemove = nameToRemove.toLowerCase(); + nameToRemove = nameToRemove.substring(nameToRemove.indexOf("!whitelist remove")+17); // get everything after !whitelist remove + nameToRemove = nameToRemove.replaceAll(" ", ""); + + final String finalNameToRemove = nameToRemove; + + 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(); + } + else + { + File whitelistJSON = (new File(".", "whitelist.json")); + + DiscordWhitelister.getPlugin().getLogger().info(author.getName() + "(" + author.getId() + ") attempted to remove " + finalNameToRemove + " from the whitelist"); + + if(!checkWhitelistJSON(whitelistJSON, finalNameToRemove)) + { + channel.sendMessage(author.getAsMention() + ", user is not on the whitelist!").queue(); + } + else + { + DiscordWhitelister.getPlugin().getServer().getScheduler().callSyncMethod(DiscordWhitelister.getPlugin(), () -> DiscordWhitelister.getPlugin().getServer().dispatchCommand(DiscordWhitelister.getPlugin().getServer().getConsoleSender(), + "whitelist remove " + finalNameToRemove)); + + DiscordWhitelister.getPlugin().getServer().getScheduler().callSyncMethod(DiscordWhitelister.getPlugin(), () -> + { + if(!checkWhitelistJSON(whitelistJSON, finalNameToRemove)) + { + channel.sendMessage(author.getAsMention() + ", successfully removed **" + finalNameToRemove + "** from the whitelist").queue(); + } + 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(); + } + return null; + }); + } + } + } + else if(messageContents.toLowerCase().contains("!whitelist remove") && !userHasPerms && !author.isBot()) { channel.sendMessage(author.getAsMention() + ", you do not have permission to use this command").queue(); }