From 36a138becc7129b1fcd08f82f5c317876ddbc5a4 Mon Sep 17 00:00:00 2001 From: ChomeNS <95471003+ChomeNS@users.noreply.github.com> Date: Sat, 30 Nov 2024 15:03:02 +0700 Subject: [PATCH] feat,fix: add admin trust level and some other fixies --- build-number.txt | 2 +- .../chayapak1/chomens_bot/Configuration.java | 4 +- .../java/me/chayapak1/chomens_bot/Main.java | 1 - .../chomens_bot/command/TrustLevel.java | 1 + .../chomens_bot/commands/FilterCommand.java | 2 +- .../chomens_bot/commands/HelpCommand.java | 7 ++- .../chomens_bot/commands/IPFilterCommand.java | 2 +- .../commands/ServerEvalCommand.java | 2 + .../chomens_bot/commands/ValidateCommand.java | 5 +- .../commands/WhitelistCommand.java | 2 +- .../plugins/CommandHandlerPlugin.java | 23 ++++++-- .../chomens_bot/plugins/HashingPlugin.java | 52 +++++++------------ src/main/resources/default-config.yml | 8 +-- 13 files changed, 62 insertions(+), 49 deletions(-) diff --git a/build-number.txt b/build-number.txt index 2d0ce9f..5f798c1 100644 --- a/build-number.txt +++ b/build-number.txt @@ -1 +1 @@ -1131 \ No newline at end of file +1136 \ No newline at end of file diff --git a/src/main/java/me/chayapak1/chomens_bot/Configuration.java b/src/main/java/me/chayapak1/chomens_bot/Configuration.java index d73f939..1ab18bd 100644 --- a/src/main/java/me/chayapak1/chomens_bot/Configuration.java +++ b/src/main/java/me/chayapak1/chomens_bot/Configuration.java @@ -57,7 +57,8 @@ public class Configuration { } public static class Keys { - public String normalKey; + public String trustedKey; + public String adminKey; public String ownerKey; } @@ -93,6 +94,7 @@ public class Configuration { public EmbedColors embedColors = new EmbedColors(); public String trustedRoleName = "Trusted"; public String adminRoleName = "Admin"; + public String ownerRoleName = "Owner"; public String statusMessage = "Oh hi!"; public String inviteLink = "https://discord.gg/xdgCkUyaA4"; } diff --git a/src/main/java/me/chayapak1/chomens_bot/Main.java b/src/main/java/me/chayapak1/chomens_bot/Main.java index f93f803..3589be7 100644 --- a/src/main/java/me/chayapak1/chomens_bot/Main.java +++ b/src/main/java/me/chayapak1/chomens_bot/Main.java @@ -125,7 +125,6 @@ public class Main { // initialize util classes and plugins PersistentDataUtilities.init(); - ComponentUtilities.init(); new ConsolePlugin(); LoggerPlugin.init(); diff --git a/src/main/java/me/chayapak1/chomens_bot/command/TrustLevel.java b/src/main/java/me/chayapak1/chomens_bot/command/TrustLevel.java index d9c4c5a..1404734 100644 --- a/src/main/java/me/chayapak1/chomens_bot/command/TrustLevel.java +++ b/src/main/java/me/chayapak1/chomens_bot/command/TrustLevel.java @@ -3,5 +3,6 @@ package me.chayapak1.chomens_bot.command; public enum TrustLevel { PUBLIC, TRUSTED, + ADMIN, OWNER } diff --git a/src/main/java/me/chayapak1/chomens_bot/commands/FilterCommand.java b/src/main/java/me/chayapak1/chomens_bot/commands/FilterCommand.java index fc597b2..74bf256 100644 --- a/src/main/java/me/chayapak1/chomens_bot/commands/FilterCommand.java +++ b/src/main/java/me/chayapak1/chomens_bot/commands/FilterCommand.java @@ -32,7 +32,7 @@ public class FilterCommand extends Command { "list" }, new String[] { "filterplayer", "ban", "blacklist" }, - TrustLevel.OWNER, + TrustLevel.ADMIN, false ); } diff --git a/src/main/java/me/chayapak1/chomens_bot/commands/HelpCommand.java b/src/main/java/me/chayapak1/chomens_bot/commands/HelpCommand.java index d18fc11..7f05565 100644 --- a/src/main/java/me/chayapak1/chomens_bot/commands/HelpCommand.java +++ b/src/main/java/me/chayapak1/chomens_bot/commands/HelpCommand.java @@ -50,6 +50,7 @@ public class HelpCommand extends Command { final List list = new ArrayList<>(); list.addAll(getCommandListByTrustLevel(TrustLevel.PUBLIC)); list.addAll(getCommandListByTrustLevel(TrustLevel.TRUSTED)); + list.addAll(getCommandListByTrustLevel(TrustLevel.ADMIN)); list.addAll(getCommandListByTrustLevel(TrustLevel.OWNER)); return Component.empty() @@ -60,7 +61,8 @@ public class HelpCommand extends Command { .append(Component.text("(").color(NamedTextColor.DARK_GRAY)) .append(Component.text("Public ").color(NamedTextColor.GREEN)) .append(Component.text("Trusted ").color(NamedTextColor.RED)) - .append(Component.text("Owner").color(NamedTextColor.DARK_RED)) + .append(Component.text("Admin ").color(NamedTextColor.DARK_RED)) + .append(Component.text("Owner").color(NamedTextColor.LIGHT_PURPLE)) .append(Component.text(") - ").color(NamedTextColor.DARK_GRAY)) .append(Component.join(JoinConfiguration.separator(Component.space()), list)); } @@ -98,7 +100,8 @@ public class HelpCommand extends Command { return switch (trustLevel) { case PUBLIC -> NamedTextColor.GREEN; case TRUSTED -> NamedTextColor.RED; - case OWNER -> NamedTextColor.DARK_RED; + case ADMIN -> NamedTextColor.DARK_RED; + case OWNER -> NamedTextColor.LIGHT_PURPLE; }; } diff --git a/src/main/java/me/chayapak1/chomens_bot/commands/IPFilterCommand.java b/src/main/java/me/chayapak1/chomens_bot/commands/IPFilterCommand.java index b4a88a3..823fe1d 100644 --- a/src/main/java/me/chayapak1/chomens_bot/commands/IPFilterCommand.java +++ b/src/main/java/me/chayapak1/chomens_bot/commands/IPFilterCommand.java @@ -27,7 +27,7 @@ public class IPFilterCommand extends Command { "list" }, new String[] { "filterip", "banip", "ipban" }, - TrustLevel.OWNER, + TrustLevel.ADMIN, false ); } diff --git a/src/main/java/me/chayapak1/chomens_bot/commands/ServerEvalCommand.java b/src/main/java/me/chayapak1/chomens_bot/commands/ServerEvalCommand.java index 64191d2..0d6ea75 100644 --- a/src/main/java/me/chayapak1/chomens_bot/commands/ServerEvalCommand.java +++ b/src/main/java/me/chayapak1/chomens_bot/commands/ServerEvalCommand.java @@ -41,6 +41,8 @@ public class ServerEvalCommand extends Command { final LuaValue output = chunk.call(); context.sendOutput(Component.text(output.toString()).color(NamedTextColor.GREEN)); + } catch (CommandException e) { + context.sendOutput(e.message.color(NamedTextColor.RED)); } catch (Exception e) { context.sendOutput(Component.text(e.toString()).color(NamedTextColor.RED)); } diff --git a/src/main/java/me/chayapak1/chomens_bot/commands/ValidateCommand.java b/src/main/java/me/chayapak1/chomens_bot/commands/ValidateCommand.java index 451e75b..5769145 100644 --- a/src/main/java/me/chayapak1/chomens_bot/commands/ValidateCommand.java +++ b/src/main/java/me/chayapak1/chomens_bot/commands/ValidateCommand.java @@ -30,8 +30,9 @@ public class ValidateCommand extends Command { final String hash = fullArgs[0]; - if (bot.hashing.isCorrectHash(hash, context.userInputCommandName, context.sender)) return Component.text("Valid hash").color(NamedTextColor.GREEN); - else if (bot.hashing.isCorrectOwnerHash(hash, context.userInputCommandName, context.sender)) return Component.text("Valid OwnerHash").color(NamedTextColor.GREEN); + if (bot.hashing.isCorrectHash(hash, context.userInputCommandName, context.sender)) return Component.text("Valid trusted hash").color(NamedTextColor.GREEN); + else if (bot.hashing.isCorrectAdminHash(hash, context.userInputCommandName, context.sender)) return Component.text("Valid admin hash").color(NamedTextColor.GREEN); + else if (bot.hashing.isCorrectOwnerHash(hash, context.userInputCommandName, context.sender)) return Component.text("Valid owner hash").color(NamedTextColor.GREEN); return null; } diff --git a/src/main/java/me/chayapak1/chomens_bot/commands/WhitelistCommand.java b/src/main/java/me/chayapak1/chomens_bot/commands/WhitelistCommand.java index 82fcf63..55b50b5 100644 --- a/src/main/java/me/chayapak1/chomens_bot/commands/WhitelistCommand.java +++ b/src/main/java/me/chayapak1/chomens_bot/commands/WhitelistCommand.java @@ -20,7 +20,7 @@ public class WhitelistCommand extends Command { "Manages whitelist", new String[] { "enable", "disable", "add ", "remove ", "clear", "list" }, new String[] {}, - TrustLevel.OWNER, + TrustLevel.ADMIN, false ); } diff --git a/src/main/java/me/chayapak1/chomens_bot/plugins/CommandHandlerPlugin.java b/src/main/java/me/chayapak1/chomens_bot/plugins/CommandHandlerPlugin.java index 17ae0e5..7ca6fd3 100644 --- a/src/main/java/me/chayapak1/chomens_bot/plugins/CommandHandlerPlugin.java +++ b/src/main/java/me/chayapak1/chomens_bot/plugins/CommandHandlerPlugin.java @@ -125,28 +125,43 @@ public class CommandHandlerPlugin { final String trustedRoleName = bot.config.discord.trustedRoleName; final String adminRoleName = bot.config.discord.adminRoleName; + final String ownerRoleName = bot.config.discord.ownerRoleName; if ( command.trustLevel == TrustLevel.TRUSTED && roles.stream().noneMatch(role -> role.getName().equalsIgnoreCase(trustedRoleName)) && - roles.stream().noneMatch(role -> role.getName().equalsIgnoreCase(adminRoleName)) + roles.stream().noneMatch(role -> role.getName().equalsIgnoreCase(adminRoleName)) && + roles.stream().noneMatch(role -> role.getName().equalsIgnoreCase(ownerRoleName)) ) return Component.text("You're not in the trusted role!").color(NamedTextColor.RED); if ( - command.trustLevel == TrustLevel.OWNER && - roles.stream().noneMatch(role -> role.getName().equalsIgnoreCase(adminRoleName)) + command.trustLevel == TrustLevel.ADMIN && + roles.stream().noneMatch(role -> role.getName().equalsIgnoreCase(adminRoleName)) && + roles.stream().noneMatch(role -> role.getName().equalsIgnoreCase(ownerRoleName)) ) return Component.text("You're not in the admin role!").color(NamedTextColor.RED); + + if ( + command.trustLevel == TrustLevel.OWNER && + roles.stream().noneMatch(role -> role.getName().equalsIgnoreCase(ownerRoleName)) + ) return Component.text("You're not in the owner role!").color(NamedTextColor.RED); } else { if ( command.trustLevel == TrustLevel.TRUSTED && !bot.hashing.isCorrectHash(userHash, splitInput[0], context.sender) && + !bot.hashing.isCorrectAdminHash(userHash, splitInput[0], context.sender) && !bot.hashing.isCorrectOwnerHash(userHash, splitInput[0], context.sender) ) return Component.text("Invalid hash").color(NamedTextColor.RED); + if ( + command.trustLevel == TrustLevel.ADMIN && + !bot.hashing.isCorrectAdminHash(userHash, splitInput[0], context.sender) && + !bot.hashing.isCorrectOwnerHash(userHash, splitInput[0], context.sender) + ) return Component.text("Invalid admin hash").color(NamedTextColor.RED); + if ( command.trustLevel == TrustLevel.OWNER && !bot.hashing.isCorrectOwnerHash(userHash, splitInput[0], context.sender) - ) return Component.text("Invalid OwnerHash").color(NamedTextColor.RED); + ) return Component.text("Invalid owner hash").color(NamedTextColor.RED); } } diff --git a/src/main/java/me/chayapak1/chomens_bot/plugins/HashingPlugin.java b/src/main/java/me/chayapak1/chomens_bot/plugins/HashingPlugin.java index 6cdf5b2..840735e 100644 --- a/src/main/java/me/chayapak1/chomens_bot/plugins/HashingPlugin.java +++ b/src/main/java/me/chayapak1/chomens_bot/plugins/HashingPlugin.java @@ -14,10 +14,13 @@ public class HashingPlugin { this.bot = bot; } - public String getHash (String prefix, PlayerEntry sender, boolean sectionSigns) { - final long time = System.currentTimeMillis() / 5_000; + public String getHash (String prefix, PlayerEntry sender, boolean sectionSigns) { return getGenericHash(bot.config.keys.trustedKey, prefix, sender, sectionSigns); } + public String getAdminHash (String prefix, PlayerEntry sender, boolean sectionSigns) { return getGenericHash(bot.config.keys.adminKey, prefix, sender, sectionSigns); } + public String getOwnerHash (String prefix, PlayerEntry sender, boolean sectionSigns) { return getGenericHash(bot.config.keys.ownerKey, prefix, sender, sectionSigns); } - final String key = bot.config.keys.normalKey; + // should this be public? + public String getGenericHash (String key, String prefix, PlayerEntry sender, boolean sectionSigns) { + final long time = System.currentTimeMillis() / 5_000; final String hashValue = sender.profile.getIdAsString() + prefix + time + key; @@ -35,40 +38,25 @@ public class HashingPlugin { hash; } - public String getOwnerHash (String prefix, PlayerEntry sender, boolean sectionSigns) { - final long time = System.currentTimeMillis() / 5_000; + private boolean checkHash (String hash, String input, String prefix, PlayerEntry sender) { + // removes reset section sign + if (input.length() == (16 * 2 /* <-- don't forget, we have the section signs */) + 2 && input.endsWith("§r")) input = input.substring(0, input.length() - 2); - final String key = bot.config.keys.ownerKey; - - final String value = sender.profile.getIdAsString() + prefix + time + key; - - final String hash = Hashing.sha256() - .hashString(value, StandardCharsets.UTF_8) - .toString() - .substring(0, 16); - - return sectionSigns ? - String.join("", - Arrays.stream(hash.split("")) - .map((letter) -> "§" + letter) - .toArray(String[]::new) - ) : - hash; + return input.equals(hash); } - public boolean isCorrectHash (String hash, String prefix, PlayerEntry sender) { - // removes reset section sign - if (hash.length() == (16 * 2 /* <-- don't forget, we have the section signs */) + 2 && hash.endsWith("§r")) hash = hash.substring(0, hash.length() - 2); - - return hash.equals(getHash(prefix, sender, true)) || - hash.equals(getHash(prefix, sender, false)); + public boolean isCorrectHash (String input, String prefix, PlayerEntry sender) { + return checkHash(getHash(prefix, sender, true), input, prefix, sender) || + checkHash(getHash(prefix, sender, false), input, prefix, sender); } - public boolean isCorrectOwnerHash (String hash, String prefix, PlayerEntry sender) { - // removes reset section sign - if (hash.length() == (16 * 2 /* <-- don't forget, we have the section signs */) + 2 && hash.endsWith("§r")) hash = hash.substring(0, hash.length() - 2); + public boolean isCorrectAdminHash (String input, String prefix, PlayerEntry sender) { + return checkHash(getAdminHash(prefix, sender, true), input, prefix, sender) || + checkHash(getAdminHash(prefix, sender, false), input, prefix, sender); + } - return hash.equals(getOwnerHash(prefix, sender, true)) || - hash.equals(getOwnerHash(prefix, sender, false)); + public boolean isCorrectOwnerHash (String input, String prefix, PlayerEntry sender) { + return checkHash(getOwnerHash(prefix, sender, true), input, prefix, sender) || + checkHash(getOwnerHash(prefix, sender, false), input, prefix, sender); } } diff --git a/src/main/resources/default-config.yml b/src/main/resources/default-config.yml index 6fc1422..2df7f31 100644 --- a/src/main/resources/default-config.yml +++ b/src/main/resources/default-config.yml @@ -28,7 +28,8 @@ discord: prefix: 'default!' token: 'token here' trustedRoleName: 'Trusted' - adminRoleName: 'Admin' # NOTE: admin will be able to access servereval.. + adminRoleName: 'Admin' + ownerRoleName: 'Owner' statusMessage: 'Say Gex' inviteLink: 'https://discord.gg/xdgCkUyaA4' servers: @@ -75,8 +76,9 @@ trusted: - 'player name' keys: - normalKey: 'normal hash key here' - ownerKey: 'OwnerHash™ key here' + trustedKey: 'trusted key here' + adminKey: 'admin key here' + ownerKey: 'owner key here' weatherApiKey: 'key here' # weatherapi.com key