added !whitelist whois command

!whitelist whois minecraftUsername will check the userlist for any occurrences and display the linked ID/Account plus their whitelisted users.
This commit is contained in:
Joe Shimell 2020-12-22 16:15:53 +00:00
parent 1d26a922b6
commit eb2d033b7b
4 changed files with 168 additions and 2 deletions

View File

@ -16,6 +16,7 @@ import org.json.simple.JSONValue;
import org.json.simple.parser.ParseException;
import org.yaml.snakeyaml.Yaml;
import uk.co.angrybee.joe.commands.discord.CommandAdd;
import uk.co.angrybee.joe.commands.discord.CommandWhoIs;
import uk.co.angrybee.joe.configs.*;
import uk.co.angrybee.joe.commands.discord.CommandInfo;
import uk.co.angrybee.joe.events.ShutdownEvents;
@ -53,18 +54,21 @@ public class DiscordClient extends ListenerAdapter
public static String[] customClearNamePrefixSplit;
public static String[] customLimitedWhitelistClearPrefixSplit;
public static String[] customClearBanPrefixSplit;
public static String[] customWhoIsPrefix;
// TODO: move to own class, references to non custom prefix
public static final String[] whitelistInfoPrefix = {"!whitelist"};
public static final String[] whitelistAddPrefix = {"!whitelist", "add"};
public static final String[] whitelistRemovePrefix = {"!whitelist", "remove"};
public static final String[] whitelistClearPrefix = {"!whitelist", "clear"};
public static final String[] whitelistWhoIsPrefix = {"!whitelist", "whois"};
public static final String[] clearNamePrefix = {"!clearname"};
public static final String[] clearBanPrefix = {"!clearban"};
public static MessageEmbed botInfo;
public static MessageEmbed addCommandInfo;
public static MessageEmbed removeCommandInfo;
public static MessageEmbed whoIsInfo;
public static int maxWhitelistAmount;
@ -128,7 +132,8 @@ public class DiscordClient extends ListenerAdapter
{
// assign vars here instead of every time a message is received, as they do not change
targetTextChannels = new String[MainConfig.getMainConfig().getList("target-text-channels").size()];
for (int i = 0; i < targetTextChannels.length; ++i) {
for (int i = 0; i < targetTextChannels.length; ++i)
{
targetTextChannels[i] = MainConfig.getMainConfig().getList("target-text-channels").get(i).toString();
}
@ -167,6 +172,10 @@ public class DiscordClient extends ListenerAdapter
removeCommandInfo = CreateEmbeddedMessage("Whitelist Remove Command",
"!whitelist remove minecraftUsername\n\nIf you encounter any issues, please report them here: https://github.com/JoeShimell/DiscordWhitelisterSpigot/issues",
EmbedMessageType.INFO).build();
whoIsInfo = CreateEmbeddedMessage("Whitelist WhoIs Command",
"!whitelist whois minecraftUsername\n\nIf you encounter any issues, please report them here: https://github.com/JoeShimell/DiscordWhitelisterSpigot/issues",
EmbedMessageType.INFO).build();
}
public static String getOnlineStatus()
@ -480,7 +489,7 @@ public class DiscordClient extends ListenerAdapter
{
namesRemainingAfterRemoval = true;
DiscordWhitelister.getPluginLogger().info("The Discord ID (" + targetDiscordId + ") linked to " + finalNameToRemove + " contains "
+ (targetWhitelistedPlayers.size() - 1) + "more whitelisted user(s), not removing whitelisted roles...");
+ (targetWhitelistedPlayers.size() - 1) + " more whitelisted user(s), not removing whitelisted roles...");
}
// Find all servers bot is in, remove whitelisted roles
@ -934,6 +943,13 @@ public class DiscordClient extends ListenerAdapter
return;
}
}
if(!DiscordWhitelister.getUseCustomPrefixes() && splitMessage.length >= whitelistWhoIsPrefix.length && CheckForPrefix(whitelistWhoIsPrefix, splitMessage)
|| DiscordWhitelister.getUseCustomPrefixes() && splitMessage.length >= customWhoIsPrefix.length && CheckForPrefix(customWhoIsPrefix, splitMessage))
{
CommandWhoIs.ExecuteCommand(messageReceivedEvent, splitMessage);
return;
}
}
}

View File

@ -153,6 +153,7 @@ public class DiscordWhitelister extends JavaPlugin
DiscordClient.customClearNamePrefixSplit = CustomPrefixConfig.getCustomPrefixesConfig().getString("clear-name-prefix").toLowerCase().trim().split(" ");
DiscordClient.customLimitedWhitelistClearPrefixSplit = CustomPrefixConfig.getCustomPrefixesConfig().getString("limited-whitelist-clear-prefix").toLowerCase().trim().split(" ");
DiscordClient.customClearBanPrefixSplit = CustomPrefixConfig.getCustomPrefixesConfig().getString("clear-ban-prefix").toLowerCase().trim().split(" ");
DiscordClient.customWhoIsPrefix = CustomPrefixConfig.getCustomPrefixesConfig().getString("whitelist-whois-prefix").toLowerCase().trim().split(" ");
if(!botEnabled)
{

View File

@ -0,0 +1,147 @@
package uk.co.angrybee.joe.commands.discord;
import com.sun.org.apache.xpath.internal.operations.Bool;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.exceptions.AccountTypeException;
import org.yaml.snakeyaml.Yaml;
import uk.co.angrybee.joe.AuthorPermissions;
import uk.co.angrybee.joe.DiscordClient;
import uk.co.angrybee.joe.DiscordWhitelister;
import uk.co.angrybee.joe.stores.UserList;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Collections;
import java.util.List;
import java.util.Map;
public class CommandWhoIs
{
public static void ExecuteCommand(MessageReceivedEvent messageReceivedEvent, String[] splitMessage)
{
AuthorPermissions authorPermissions = new AuthorPermissions(messageReceivedEvent);
User author = messageReceivedEvent.getAuthor();
TextChannel channel = messageReceivedEvent.getTextChannel();
Member member = messageReceivedEvent.getMember();
if (!authorPermissions.isUserCanAddRemove() && !authorPermissions.isUserCanAdd())
{
channel.sendMessage(DiscordClient.CreateInsufficientPermsMessage(author)).queue();
return;
}
// TODO make 1 function like this that multiple commands can call on
if(DiscordWhitelister.getUseCustomPrefixes() && splitMessage.length > (DiscordClient.customWhoIsPrefix.length + 1)
|| !DiscordWhitelister.getUseCustomPrefixes() && splitMessage.length > (DiscordClient.whitelistWhoIsPrefix.length + 1))
{
int amountOfArgs = 0;
if(DiscordWhitelister.getUseCustomPrefixes())
amountOfArgs = splitMessage.length - (DiscordClient.customWhoIsPrefix.length);
else
amountOfArgs = splitMessage.length - (DiscordClient.whitelistWhoIsPrefix.length);
StringBuilder exampleCommand = new StringBuilder();
if(DiscordWhitelister.getUseCustomPrefixes())
{
for(int i = 0; i < DiscordClient.customWhoIsPrefix.length; i++)
{
exampleCommand.append(DiscordClient.customWhoIsPrefix[i]).append(" ");
}
}
else
{
for(int i = 0; i < DiscordClient.whitelistWhoIsPrefix.length; i++)
{
exampleCommand.append(DiscordClient.whitelistWhoIsPrefix[i]).append(" ");
}
}
exampleCommand.append("<minecraftUsername>");
channel.sendMessage(DiscordClient.CreateEmbeddedMessage("Too many arguments",
(author.getAsMention() + ", expected 1 argument but found " + amountOfArgs + ".\n" +
"Example: " + exampleCommand.toString()), DiscordClient.EmbedMessageType.FAILURE).build()).queue();
return;
}
String nameToCheck = "";
if(DiscordWhitelister.getUseCustomPrefixes() && splitMessage.length >= DiscordClient.customWhoIsPrefix.length + 1)
nameToCheck = splitMessage[DiscordClient.customWhoIsPrefix.length];
if(!DiscordWhitelister.getUseCustomPrefixes() && splitMessage.length >= DiscordClient.whitelistWhoIsPrefix.length + 1)
nameToCheck = splitMessage[DiscordClient.whitelistWhoIsPrefix.length];
if(DiscordWhitelister.getUseCustomPrefixes() && splitMessage.length == DiscordClient.customWhoIsPrefix.length
|| !DiscordWhitelister.getUseCustomPrefixes() && splitMessage.length == DiscordClient.whitelistWhoIsPrefix.length || nameToCheck.isEmpty())
{
channel.sendMessage(DiscordClient.whoIsInfo).queue();
return;
}
Boolean idFound = false;
String targetDiscordId = "";
List<String> targetWhitelistedPlayers = Collections.emptyList();
// Find the Discord Id linked to the whitelisted player
Yaml userYaml = new Yaml();
try
{
InputStream inputStream = new FileInputStream(UserList.getUserListFile());
Map<String, List<String>> userListObject = userYaml.load(inputStream);
for(Map.Entry<String, List<String>> entry : userListObject.entrySet())
{
for(int i = 0; i < entry.getValue().size(); i++)
{
if(entry.getValue().get(i).equals(nameToCheck))
{
targetDiscordId = entry.getKey();
targetWhitelistedPlayers = entry.getValue();
idFound = true;
break;
}
}
}
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
if(idFound)
{
String userAsMention = "<@!" + targetDiscordId + ">"; // use this in-case the user has left the discord ? over using fetched member
StringBuilder usersWhitelistedPlayers = new StringBuilder();
for (String targetWhitelistedPlayer : targetWhitelistedPlayers)
{
usersWhitelistedPlayers.append("- ").append(targetWhitelistedPlayer).append("\n");
}
EmbedBuilder idFoundMessage = DiscordClient.CreateEmbeddedMessage(("Found account linked to `" + nameToCheck + "`"),
(author.getAsMention() + ", the Minecraft username: `" + nameToCheck + "` is linked to " + userAsMention +
".\n\n Here is a list of their whitelisted players:\n" + usersWhitelistedPlayers),
DiscordClient.EmbedMessageType.SUCCESS);
User fetchedUser = DiscordClient.javaDiscordAPI.getUserById(targetDiscordId);
if(fetchedUser != null)
idFoundMessage.setThumbnail(fetchedUser.getAvatarUrl());
else
DiscordWhitelister.getPluginLogger().warning("Failed to fetch avatar linked to Discord ID: " + targetDiscordId);
channel.sendMessage(idFoundMessage.build()).queue();
}
else
{
channel.sendMessage(DiscordClient.CreateEmbeddedMessage(("Could not find an account linked to `" + nameToCheck + "`"),
(author.getAsMention() + ", the name: `" + nameToCheck +
"` could not be found in the users list. Please make sure that the Minecraft name is valid and whitelisted + linked to an ID before."),
DiscordClient.EmbedMessageType.FAILURE).build()).queue();
}
}
}

View File

@ -71,6 +71,8 @@ public class CustomPrefixConfig
CheckEntry("limited-whitelist-clear-prefix", "!whitelist clear");
CheckEntry("clear-ban-prefix", "!clearban");
CheckEntry("whitelist-whois-prefix", "!whitelist whois");
}
}