refactor: make validate return the trust level

This commit is contained in:
ChomeNS
2024-12-18 17:35:31 +07:00
parent c1efc1c3d5
commit e798a9dc18
4 changed files with 45 additions and 35 deletions

View File

@@ -19,6 +19,8 @@ public class CommandContext {
public final boolean inGame;
public TrustLevel trustLevel = TrustLevel.PUBLIC;
public String commandName = null;
public String userInputCommandName = null;

View File

@@ -1,10 +1,6 @@
package me.chayapak1.chomens_bot.commands;
import me.chayapak1.chomens_bot.Bot;
import me.chayapak1.chomens_bot.command.Command;
import me.chayapak1.chomens_bot.command.CommandContext;
import me.chayapak1.chomens_bot.command.CommandException;
import me.chayapak1.chomens_bot.command.TrustLevel;
import me.chayapak1.chomens_bot.command.*;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
@@ -12,7 +8,7 @@ public class ValidateCommand extends Command {
public ValidateCommand () {
super(
"validate",
"Validates a hash",
"Validates/shows your trust level",
new String[] { "" },
new String[] { "checkhash" },
TrustLevel.TRUSTED,
@@ -22,18 +18,16 @@ public class ValidateCommand extends Command {
@Override
public Component execute(CommandContext context) throws CommandException {
final Bot bot = context.bot;
final String[] fullArgs = context.fullArgs;
if (fullArgs.length == 0) return null;
final String hash = fullArgs[0];
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;
if (context instanceof DiscordCommandContext) return Component
.translatable("You are trusted! (%s)")
.arguments(Component.text(context.trustLevel.name()))
.color(NamedTextColor.GREEN);
else if (context instanceof ConsoleCommandContext) return Component
.text("You are the console! You have no trust level")
.color(NamedTextColor.GREEN);
else return Component
.translatable("Valid hash (%s)")
.arguments(Component.text(context.trustLevel.name()))
.color(NamedTextColor.GREEN);
}
}

View File

@@ -124,45 +124,59 @@ public class CommandHandlerPlugin {
final List<Role> roles = member.getRoles();
final String trustedRoleName = bot.config.discord.trustedRoleName;
final String adminRoleName = bot.config.discord.adminRoleName;
final String ownerRoleName = bot.config.discord.ownerRoleName;
final boolean hasTrustedRole = roles.stream().anyMatch(role -> role.getName().equalsIgnoreCase(bot.config.discord.trustedRoleName));
final boolean hasAdminRole = roles.stream().anyMatch(role -> role.getName().equalsIgnoreCase(bot.config.discord.adminRoleName));
final boolean hasOwnerRole = roles.stream().anyMatch(role -> role.getName().equalsIgnoreCase(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(ownerRoleName))
!hasTrustedRole &&
!hasAdminRole &&
!hasOwnerRole
) return Component.text("You're not in the trusted role!").color(NamedTextColor.RED);
if (
command.trustLevel == TrustLevel.ADMIN &&
roles.stream().noneMatch(role -> role.getName().equalsIgnoreCase(adminRoleName)) &&
roles.stream().noneMatch(role -> role.getName().equalsIgnoreCase(ownerRoleName))
!hasAdminRole &&
!hasOwnerRole
) 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))
!hasOwnerRole
) return Component.text("You're not in the owner role!").color(NamedTextColor.RED);
context.trustLevel = hasOwnerRole ? TrustLevel.OWNER :
hasAdminRole ? TrustLevel.ADMIN :
hasTrustedRole ? TrustLevel.TRUSTED :
TrustLevel.PUBLIC;
} else {
final boolean correctHash = bot.hashing.isCorrectHash(userHash, splitInput[0], context.sender);
final boolean correctAdminHash = bot.hashing.isCorrectAdminHash(userHash, splitInput[0], context.sender);
final boolean correctOwnerHash = bot.hashing.isCorrectOwnerHash(userHash, splitInput[0], context.sender);
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)
!correctHash &&
!correctAdminHash &&
!correctOwnerHash
) 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)
!correctAdminHash &&
!correctOwnerHash
) return Component.text("Invalid admin hash").color(NamedTextColor.RED);
if (
command.trustLevel == TrustLevel.OWNER &&
!bot.hashing.isCorrectOwnerHash(userHash, splitInput[0], context.sender)
!correctOwnerHash
) return Component.text("Invalid owner hash").color(NamedTextColor.RED);
context.trustLevel = correctOwnerHash ? TrustLevel.OWNER :
correctAdminHash ? TrustLevel.ADMIN :
correctHash ? TrustLevel.TRUSTED :
TrustLevel.PUBLIC;
}
}