feat,fix: add admin trust level and some other fixies

This commit is contained in:
Chayapak 2024-11-30 15:03:02 +07:00
parent 651db6c8ce
commit 36a138becc
Signed by: ChomeNS
SSH key fingerprint: SHA256:0YoxhdyXsgbc0nfeB2N6FYE60mxMU7DS4uCUMaw2mvA
13 changed files with 62 additions and 49 deletions

View file

@ -1 +1 @@
1131 1136

View file

@ -57,7 +57,8 @@ public class Configuration {
} }
public static class Keys { public static class Keys {
public String normalKey; public String trustedKey;
public String adminKey;
public String ownerKey; public String ownerKey;
} }
@ -93,6 +94,7 @@ public class Configuration {
public EmbedColors embedColors = new EmbedColors(); public EmbedColors embedColors = new EmbedColors();
public String trustedRoleName = "Trusted"; public String trustedRoleName = "Trusted";
public String adminRoleName = "Admin"; public String adminRoleName = "Admin";
public String ownerRoleName = "Owner";
public String statusMessage = "Oh hi!"; public String statusMessage = "Oh hi!";
public String inviteLink = "https://discord.gg/xdgCkUyaA4"; public String inviteLink = "https://discord.gg/xdgCkUyaA4";
} }

View file

@ -125,7 +125,6 @@ public class Main {
// initialize util classes and plugins // initialize util classes and plugins
PersistentDataUtilities.init(); PersistentDataUtilities.init();
ComponentUtilities.init();
new ConsolePlugin(); new ConsolePlugin();
LoggerPlugin.init(); LoggerPlugin.init();

View file

@ -3,5 +3,6 @@ package me.chayapak1.chomens_bot.command;
public enum TrustLevel { public enum TrustLevel {
PUBLIC, PUBLIC,
TRUSTED, TRUSTED,
ADMIN,
OWNER OWNER
} }

View file

@ -32,7 +32,7 @@ public class FilterCommand extends Command {
"list" "list"
}, },
new String[] { "filterplayer", "ban", "blacklist" }, new String[] { "filterplayer", "ban", "blacklist" },
TrustLevel.OWNER, TrustLevel.ADMIN,
false false
); );
} }

View file

@ -50,6 +50,7 @@ public class HelpCommand extends Command {
final List<Component> list = new ArrayList<>(); final List<Component> list = new ArrayList<>();
list.addAll(getCommandListByTrustLevel(TrustLevel.PUBLIC)); list.addAll(getCommandListByTrustLevel(TrustLevel.PUBLIC));
list.addAll(getCommandListByTrustLevel(TrustLevel.TRUSTED)); list.addAll(getCommandListByTrustLevel(TrustLevel.TRUSTED));
list.addAll(getCommandListByTrustLevel(TrustLevel.ADMIN));
list.addAll(getCommandListByTrustLevel(TrustLevel.OWNER)); list.addAll(getCommandListByTrustLevel(TrustLevel.OWNER));
return Component.empty() return Component.empty()
@ -60,7 +61,8 @@ public class HelpCommand extends Command {
.append(Component.text("(").color(NamedTextColor.DARK_GRAY)) .append(Component.text("(").color(NamedTextColor.DARK_GRAY))
.append(Component.text("Public ").color(NamedTextColor.GREEN)) .append(Component.text("Public ").color(NamedTextColor.GREEN))
.append(Component.text("Trusted ").color(NamedTextColor.RED)) .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.text(") - ").color(NamedTextColor.DARK_GRAY))
.append(Component.join(JoinConfiguration.separator(Component.space()), list)); .append(Component.join(JoinConfiguration.separator(Component.space()), list));
} }
@ -98,7 +100,8 @@ public class HelpCommand extends Command {
return switch (trustLevel) { return switch (trustLevel) {
case PUBLIC -> NamedTextColor.GREEN; case PUBLIC -> NamedTextColor.GREEN;
case TRUSTED -> NamedTextColor.RED; case TRUSTED -> NamedTextColor.RED;
case OWNER -> NamedTextColor.DARK_RED; case ADMIN -> NamedTextColor.DARK_RED;
case OWNER -> NamedTextColor.LIGHT_PURPLE;
}; };
} }

View file

@ -27,7 +27,7 @@ public class IPFilterCommand extends Command {
"list" "list"
}, },
new String[] { "filterip", "banip", "ipban" }, new String[] { "filterip", "banip", "ipban" },
TrustLevel.OWNER, TrustLevel.ADMIN,
false false
); );
} }

View file

@ -41,6 +41,8 @@ public class ServerEvalCommand extends Command {
final LuaValue output = chunk.call(); final LuaValue output = chunk.call();
context.sendOutput(Component.text(output.toString()).color(NamedTextColor.GREEN)); context.sendOutput(Component.text(output.toString()).color(NamedTextColor.GREEN));
} catch (CommandException e) {
context.sendOutput(e.message.color(NamedTextColor.RED));
} catch (Exception e) { } catch (Exception e) {
context.sendOutput(Component.text(e.toString()).color(NamedTextColor.RED)); context.sendOutput(Component.text(e.toString()).color(NamedTextColor.RED));
} }

View file

@ -30,8 +30,9 @@ public class ValidateCommand extends Command {
final String hash = fullArgs[0]; final String hash = fullArgs[0];
if (bot.hashing.isCorrectHash(hash, context.userInputCommandName, context.sender)) return Component.text("Valid hash").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.isCorrectOwnerHash(hash, context.userInputCommandName, context.sender)) return Component.text("Valid OwnerHash").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; return null;
} }

View file

@ -20,7 +20,7 @@ public class WhitelistCommand extends Command {
"Manages whitelist", "Manages whitelist",
new String[] { "enable", "disable", "add <player>", "remove <index>", "clear", "list" }, new String[] { "enable", "disable", "add <player>", "remove <index>", "clear", "list" },
new String[] {}, new String[] {},
TrustLevel.OWNER, TrustLevel.ADMIN,
false false
); );
} }

View file

@ -125,28 +125,43 @@ public class CommandHandlerPlugin {
final String trustedRoleName = bot.config.discord.trustedRoleName; final String trustedRoleName = bot.config.discord.trustedRoleName;
final String adminRoleName = bot.config.discord.adminRoleName; final String adminRoleName = bot.config.discord.adminRoleName;
final String ownerRoleName = bot.config.discord.ownerRoleName;
if ( if (
command.trustLevel == TrustLevel.TRUSTED && command.trustLevel == TrustLevel.TRUSTED &&
roles.stream().noneMatch(role -> role.getName().equalsIgnoreCase(trustedRoleName)) && 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); ) return Component.text("You're not in the trusted role!").color(NamedTextColor.RED);
if ( if (
command.trustLevel == TrustLevel.OWNER && command.trustLevel == TrustLevel.ADMIN &&
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 admin role!").color(NamedTextColor.RED); ) 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 { } else {
if ( if (
command.trustLevel == TrustLevel.TRUSTED && command.trustLevel == TrustLevel.TRUSTED &&
!bot.hashing.isCorrectHash(userHash, splitInput[0], context.sender) && !bot.hashing.isCorrectHash(userHash, splitInput[0], context.sender) &&
!bot.hashing.isCorrectAdminHash(userHash, splitInput[0], context.sender) &&
!bot.hashing.isCorrectOwnerHash(userHash, splitInput[0], context.sender) !bot.hashing.isCorrectOwnerHash(userHash, splitInput[0], context.sender)
) return Component.text("Invalid hash").color(NamedTextColor.RED); ) 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 ( if (
command.trustLevel == TrustLevel.OWNER && command.trustLevel == TrustLevel.OWNER &&
!bot.hashing.isCorrectOwnerHash(userHash, splitInput[0], context.sender) !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);
} }
} }

View file

@ -14,10 +14,13 @@ public class HashingPlugin {
this.bot = bot; this.bot = bot;
} }
public String getHash (String prefix, PlayerEntry sender, boolean sectionSigns) { public String getHash (String prefix, PlayerEntry sender, boolean sectionSigns) { return getGenericHash(bot.config.keys.trustedKey, prefix, sender, sectionSigns); }
final long time = System.currentTimeMillis() / 5_000; 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; final String hashValue = sender.profile.getIdAsString() + prefix + time + key;
@ -35,40 +38,25 @@ public class HashingPlugin {
hash; hash;
} }
public String getOwnerHash (String prefix, PlayerEntry sender, boolean sectionSigns) { private boolean checkHash (String hash, String input, String prefix, PlayerEntry sender) {
final long time = System.currentTimeMillis() / 5_000; // 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; return input.equals(hash);
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;
} }
public boolean isCorrectHash (String hash, String prefix, PlayerEntry sender) { public boolean isCorrectHash (String input, String prefix, PlayerEntry sender) {
// removes reset section sign return checkHash(getHash(prefix, sender, true), input, prefix, sender) ||
if (hash.length() == (16 * 2 /* <-- don't forget, we have the section signs */) + 2 && hash.endsWith("§r")) hash = hash.substring(0, hash.length() - 2); checkHash(getHash(prefix, sender, false), input, prefix, sender);
return hash.equals(getHash(prefix, sender, true)) ||
hash.equals(getHash(prefix, sender, false));
} }
public boolean isCorrectOwnerHash (String hash, String prefix, PlayerEntry sender) { public boolean isCorrectAdminHash (String input, String prefix, PlayerEntry sender) {
// removes reset section sign return checkHash(getAdminHash(prefix, sender, true), input, prefix, sender) ||
if (hash.length() == (16 * 2 /* <-- don't forget, we have the section signs */) + 2 && hash.endsWith("§r")) hash = hash.substring(0, hash.length() - 2); checkHash(getAdminHash(prefix, sender, false), input, prefix, sender);
}
return hash.equals(getOwnerHash(prefix, sender, true)) || public boolean isCorrectOwnerHash (String input, String prefix, PlayerEntry sender) {
hash.equals(getOwnerHash(prefix, sender, false)); return checkHash(getOwnerHash(prefix, sender, true), input, prefix, sender) ||
checkHash(getOwnerHash(prefix, sender, false), input, prefix, sender);
} }
} }

View file

@ -28,7 +28,8 @@ discord:
prefix: 'default!' prefix: 'default!'
token: 'token here' token: 'token here'
trustedRoleName: 'Trusted' trustedRoleName: 'Trusted'
adminRoleName: 'Admin' # NOTE: admin will be able to access servereval.. adminRoleName: 'Admin'
ownerRoleName: 'Owner'
statusMessage: 'Say Gex' statusMessage: 'Say Gex'
inviteLink: 'https://discord.gg/xdgCkUyaA4' inviteLink: 'https://discord.gg/xdgCkUyaA4'
servers: servers:
@ -75,8 +76,9 @@ trusted:
- 'player name' - 'player name'
keys: keys:
normalKey: 'normal hash key here' trustedKey: 'trusted key here'
ownerKey: 'OwnerHash™ key here' adminKey: 'admin key here'
ownerKey: 'owner key here'
weatherApiKey: 'key here' # weatherapi.com key weatherApiKey: 'key here' # weatherapi.com key