Skip to content
This repository has been archived by the owner on Aug 26, 2024. It is now read-only.

Commit

Permalink
added a remove command and info command
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeShimo committed Aug 25, 2019
1 parent 158c5b5 commit 905a377
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 8 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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 <MinecraftUsername>' in a valid channel to whitelist people on your minecraft server
- use '!whitelist add <MinecraftUsername>' in a valid channel to whitelist people on your minecraft server
- use '!whitelist remove <MinecraftUsername>' 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
Expand Down
2 changes: 1 addition & 1 deletion plugin.yml
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
77 changes: 71 additions & 6 deletions src/main/java/uk/co/angrybee/joe/ServerDiscordClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 <MinecraftUsername> -- Usage: Adds a user to the whitelist" + System.lineSeparator() +
"Remove:" + System.lineSeparator() + "!whitelistremove <MinecraftUsername> -- 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 <MinecraftUsername>" + System.lineSeparator() +
channel.sendMessage(author.getAsMention() + ", ```Whitelist Command:" + System.lineSeparator() +
"!whitelist add <MinecraftUsername>" + 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))
{
Expand All @@ -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 <MinecraftUsername>" + 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();
}
Expand Down

0 comments on commit 905a377

Please sign in to comment.