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

@@ -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;
}
}