Merge pull request #47 from Xyntexx/feature/refractoring

small bugfixes and refactoring
This commit is contained in:
Joe 2021-04-04 17:27:25 +01:00 committed by GitHub
commit 66509e227d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 410 additions and 680 deletions

File diff suppressed because it is too large Load Diff

View File

@ -66,6 +66,7 @@ public class DiscordWhitelister extends JavaPlugin
else if(initSuccess == 1)
{
pluginLogger.severe("Discord Client failed to initialize, please check if your config file is valid");
return;
}
// Check for leavers if enabled
@ -117,8 +118,8 @@ public class DiscordWhitelister extends JavaPlugin
useInGameAddRemoves = MainConfig.getMainConfig().getBoolean("add-in-game-adds-and-removes-to-list");
useOnBanEvents = MainConfig.getMainConfig().getBoolean("use-on-ban-events");
removeUnnecessaryMessages = MainConfig.getMainConfig().getBoolean("remove-unnecessary-messages-from-whitelist-channel");
removeMessageWaitTime = MainConfig.getMainConfig().getInt("seconds-to-remove-message-from-whitelist-channel");
useOnWhitelistCommands = MainConfig.getMainConfig().getBoolean("use-on-whitelist-commands");
// Check for LuckPerms first
if(MainConfig.getMainConfig().getBoolean("assign-perms-with-luck-perms"))
@ -148,9 +149,6 @@ public class DiscordWhitelister extends JavaPlugin
}
}
if(MainConfig.getMainConfig().getBoolean("use-on-whitelist-commands"))
useOnWhitelistCommands = true;
// TODO: remove in favour of split versions
DiscordClient.customWhitelistAddPrefix = CustomPrefixConfig.getCustomPrefixesConfig().getString("whitelist-add-prefix").toLowerCase();
DiscordClient.customWhitelistRemovePrefix = CustomPrefixConfig.getCustomPrefixesConfig().getString("whitelist-remove-prefix").toLowerCase();

View File

@ -54,6 +54,7 @@ public class CommandAdd
// Permission check
if (!(authorPermissions.isUserCanAddRemove() || authorPermissions.isUserCanAdd() || DiscordClient.limitedWhitelistEnabled && authorPermissions.isUserHasLimitedAdd()))
{
//todo: add a message to notify user
DiscordClient.QueueAndRemoveAfterSeconds(channel, DiscordClient.CreateInsufficientPermsMessage(author));
return;
}
@ -223,7 +224,7 @@ public class CommandAdd
|| !DiscordWhitelister.getUseCustomPrefixes() && splitMessageCaseSensitive.length == DiscordClient.whitelistAddPrefix.length
|| finalNameToAdd.isEmpty())
{
if(!MainConfig.getMainConfig().getBoolean("hide-info-command-replies"))
if(!DiscordClient.hideInfoCommandReplies)
DiscordClient.QueueAndRemoveAfterSeconds(channel, DiscordClient.addCommandInfo);
}
else

View File

@ -15,48 +15,36 @@ import uk.co.angrybee.joe.stores.UserList;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.concurrent.TimeUnit;
public class CommandWhoIs
{
public static void ExecuteCommand(MessageReceivedEvent messageReceivedEvent, String[] splitMessage)
{
public class CommandWhoIs {
public static void ExecuteCommand(MessageReceivedEvent messageReceivedEvent, String[] splitMessage) {
AuthorPermissions authorPermissions = new AuthorPermissions(messageReceivedEvent);
User author = messageReceivedEvent.getAuthor();
TextChannel channel = messageReceivedEvent.getTextChannel();
if (!authorPermissions.isUserCanAddRemove() && !authorPermissions.isUserCanAdd())
{
if (!authorPermissions.isUserCanAddRemove() && !authorPermissions.isUserCanAdd()) {
DiscordClient.QueueAndRemoveAfterSeconds(channel, DiscordClient.CreateInsufficientPermsMessage(author));
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))
{
if (DiscordWhitelister.getUseCustomPrefixes() && splitMessage.length > (DiscordClient.customWhoIsPrefix.length + 1)
|| !DiscordWhitelister.getUseCustomPrefixes() && splitMessage.length > (DiscordClient.whitelistWhoIsPrefix.length + 1)) {
int amountOfArgs = 0;
if(DiscordWhitelister.getUseCustomPrefixes())
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++)
{
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++)
{
} else {
for (int i = 0; i < DiscordClient.whitelistWhoIsPrefix.length; i++) {
exampleCommand.append(DiscordClient.whitelistWhoIsPrefix[i]).append(" ");
}
}
@ -71,83 +59,60 @@ public class CommandWhoIs
}
String nameToCheck = "";
if(DiscordWhitelister.getUseCustomPrefixes() && splitMessage.length >= DiscordClient.customWhoIsPrefix.length + 1)
if (DiscordWhitelister.getUseCustomPrefixes() && splitMessage.length >= DiscordClient.customWhoIsPrefix.length + 1)
nameToCheck = splitMessage[DiscordClient.customWhoIsPrefix.length];
if(!DiscordWhitelister.getUseCustomPrefixes() && splitMessage.length >= DiscordClient.whitelistWhoIsPrefix.length + 1)
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())
{
if(!MainConfig.getMainConfig().getBoolean("hide-info-command-replies"))
if (DiscordWhitelister.getUseCustomPrefixes() && splitMessage.length == DiscordClient.customWhoIsPrefix.length
|| !DiscordWhitelister.getUseCustomPrefixes() && splitMessage.length == DiscordClient.whitelistWhoIsPrefix.length || nameToCheck.isEmpty()) {
if (!DiscordClient.hideInfoCommandReplies)
return;
DiscordClient.QueueAndRemoveAfterSeconds(channel, DiscordClient.whoIsInfo);
return;
}
Boolean idFound = false;
String targetDiscordId = "";
List<String> targetWhitelistedPlayers = Collections.emptyList();
boolean idFound = false;
// 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;
Set<String> keys = UserList.getUserList().getKeys(false);
for (String discordId : keys) {
List<?> registeredUsers = UserList.getRegisteredUsers(discordId);
for (Object mc_name : registeredUsers) {
if (mc_name.equals(nameToCheck)) {
String userAsMention = "<@!" + discordId + ">"; // use this in-case the user has left the discord ? over using fetched member
StringBuilder usersWhitelistedPlayers = new StringBuilder();
for (Object targetWhitelistedPlayer : registeredUsers) {
if (targetWhitelistedPlayer instanceof String)
usersWhitelistedPlayers.append("- ").append((String) 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(discordId);
if (fetchedUser != null)
idFoundMessage.setThumbnail(fetchedUser.getAvatarUrl());
else
DiscordWhitelister.getPluginLogger().warning("Failed to fetch avatar linked to Discord ID: " + discordId);
DiscordClient.QueueAndRemoveAfterSeconds(channel, idFoundMessage.build());
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);
DiscordClient.QueueAndRemoveAfterSeconds(channel, idFoundMessage.build());
}
else
{
if (!idFound) {
MessageEmbed messageEmbed = 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();
DiscordClient.EmbedMessageType.FAILURE).build();
DiscordClient.QueueAndRemoveAfterSeconds(channel, messageEmbed);
}
}
}
}

View File

@ -1,27 +1,21 @@
package uk.co.angrybee.joe.events;
import net.dv8tion.jda.api.entities.Guild;
import org.bukkit.OfflinePlayer;
import org.bukkit.Server;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.yaml.snakeyaml.Yaml;
import uk.co.angrybee.joe.configs.MainConfig;
import uk.co.angrybee.joe.configs.PermissionsConfig;
import uk.co.angrybee.joe.DiscordClient;
import uk.co.angrybee.joe.DiscordWhitelister;
import uk.co.angrybee.joe.stores.InGameRemovedList;
import uk.co.angrybee.joe.stores.UserList;
import uk.co.angrybee.joe.stores.WhitelistedPlayers;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.*;
public class OnBanEvent implements Listener
@ -94,62 +88,46 @@ public class OnBanEvent implements Listener
if(!WhitelistedPlayers.usingEasyWhitelist && WhitelistedPlayers.CheckForPlayer(banTarget)
|| WhitelistedPlayers.usingEasyWhitelist && WhitelistedPlayers.CheckForPlayerEasyWhitelist(banTarget))
{
Boolean idFound = false;
String targetDiscordId = "";
List<String> targetWhitelistedPlayers = Collections.emptyList();
boolean idFound = false;
// Find the Discord Id linked to the whitelisted player
Yaml userYaml = new Yaml();
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(banTarget))
Set<String> keys = UserList.getUserList().getKeys(false);
for (String discordId : keys) {
List<?> registeredUsers = UserList.getRegisteredUsers(discordId);
for (Object mc_name : registeredUsers) {
if(mc_name instanceof String && mc_name.equals(banTarget))
{
// Found the ban target, assign the corresponding Discord id
targetDiscordId = entry.getKey();
targetWhitelistedPlayers = entry.getValue();
// Remove whitelisted players associated with the discord id
for (Object targetWhitelistedPlayer : registeredUsers) {
if (targetWhitelistedPlayer instanceof String){
DiscordClient.UnWhitelist((String) mc_name);
DiscordWhitelister.getPluginLogger().info("Removed " + targetWhitelistedPlayer
+ " from the whitelist as they were added by Discord Id: " + discordId);
// Add username to the in-game removed list
InGameRemovedList.AddUserToStore((String) targetWhitelistedPlayer, commandCaller.getDisplayName());
}
}
// Remove the users whitelisted players from the list
UserList.getUserList().set(discordId, null);
UserList.SaveStore();
// Find all servers bot is in, assign & remove roles
for(Guild guild: DiscordClient.javaDiscordAPI.getGuilds())
{
// Remove the whitelisted role(s)
DiscordClient.RemoveRolesFromUser(guild, discordId, Arrays.asList(DiscordClient.whitelistedRoleNames));
// Add the banned role(s)
DiscordClient.AssignRolesToUser(guild, discordId, (List<String>) MainConfig.getMainConfig().get("banned-roles"));
}
idFound = true;
break;
}
}
}
if(idFound)
{
// Remove whitelisted players associated with the discord id
for(int i = 0; i < targetWhitelistedPlayers.size(); i++)
{
DiscordClient.UnWhitelist(targetWhitelistedPlayers.get(i));
DiscordWhitelister.getPluginLogger().info("Removed " + targetWhitelistedPlayers.get(i)
+ " from the whitelist as they were added by Discord Id: " + targetDiscordId);
// Add username to the in-game removed list
InGameRemovedList.AddUserToStore(targetWhitelistedPlayers.get(i), commandCaller.getDisplayName());
}
// Remove the users whitelisted players from the list
UserList.getUserList().set(targetDiscordId, null);
UserList.SaveStore();
// Find all servers bot is in, assign & remove roles
for(int i = 0; i < DiscordClient.javaDiscordAPI.getGuilds().size(); i++)
{
// Remove the whitelisted role(s)
DiscordClient.RemoveRolesFromUser(DiscordClient.javaDiscordAPI.getGuilds().get(i), targetDiscordId, Arrays.asList(DiscordClient.whitelistedRoleNames));
// Add the banned role(s)
DiscordClient.AssignRolesToUser(DiscordClient.javaDiscordAPI.getGuilds().get(i), targetDiscordId, (List<String>) MainConfig.getMainConfig().get("banned-roles"));
}
}
else
{
if (!idFound) {
DiscordWhitelister.getPluginLogger().warning(banTarget + " does not have a linked Discord Id; cannot assign roles!");
}
}