refactor: reduce the messy trust level checking by at least a bit
it looks a lot better now, the if statements at the discord roles still look pretty messy though
This commit is contained in:
parent
079c579b0e
commit
0bf8ce54f8
4 changed files with 47 additions and 57 deletions
build-number.txt
src/main/java/me/chayapak1/chomens_bot/plugins
|
@ -1 +1 @@
|
|||
1988
|
||||
1995
|
|
@ -126,63 +126,48 @@ public class CommandHandlerPlugin {
|
|||
|
||||
final List<Role> roles = member.getRoles();
|
||||
|
||||
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));
|
||||
TrustLevel userTrustLevel = TrustLevel.PUBLIC;
|
||||
|
||||
if (
|
||||
command.trustLevel == TrustLevel.TRUSTED &&
|
||||
!hasTrustedRole &&
|
||||
!hasAdminRole &&
|
||||
!hasOwnerRole
|
||||
) return Component.text("You're not in the trusted role!").color(NamedTextColor.RED);
|
||||
for (Role role : roles) {
|
||||
if (role.getName().equalsIgnoreCase(bot.config.discord.ownerRoleName)) {
|
||||
userTrustLevel = TrustLevel.OWNER;
|
||||
break;
|
||||
} else if (role.getName().equalsIgnoreCase(bot.config.discord.adminRoleName)) {
|
||||
userTrustLevel = TrustLevel.ADMIN;
|
||||
break;
|
||||
} else if (role.getName().equalsIgnoreCase(bot.config.discord.trustedRoleName)) {
|
||||
userTrustLevel = TrustLevel.TRUSTED;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
command.trustLevel == TrustLevel.ADMIN &&
|
||||
!hasAdminRole &&
|
||||
!hasOwnerRole
|
||||
) return Component.text("You're not in the admin role!").color(NamedTextColor.RED);
|
||||
if (trustLevel.level > userTrustLevel.level) {
|
||||
return Component
|
||||
.translatable(
|
||||
"Your current roles don't allow you to execute %s commands!",
|
||||
Component.text(trustLevel.name())
|
||||
)
|
||||
.color(NamedTextColor.RED);
|
||||
}
|
||||
|
||||
if (
|
||||
command.trustLevel == TrustLevel.OWNER &&
|
||||
!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;
|
||||
context.trustLevel = userTrustLevel;
|
||||
} 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);
|
||||
final TrustLevel userTrustLevel = bot.hashing.getTrustLevel(userHash, splitInput[0], context.sender);
|
||||
|
||||
if (
|
||||
command.trustLevel == TrustLevel.TRUSTED &&
|
||||
!correctHash &&
|
||||
!correctAdminHash &&
|
||||
!correctOwnerHash
|
||||
) return Component.text("Invalid hash").color(NamedTextColor.RED);
|
||||
if (trustLevel.level > userTrustLevel.level) {
|
||||
return Component
|
||||
.translatable(
|
||||
"Invalid %s hash",
|
||||
Component.text(trustLevel.name())
|
||||
)
|
||||
.color(NamedTextColor.RED);
|
||||
}
|
||||
|
||||
if (
|
||||
command.trustLevel == TrustLevel.ADMIN &&
|
||||
!correctAdminHash &&
|
||||
!correctOwnerHash
|
||||
) return Component.text("Invalid admin hash").color(NamedTextColor.RED);
|
||||
|
||||
if (
|
||||
command.trustLevel == TrustLevel.OWNER &&
|
||||
!correctOwnerHash
|
||||
) return Component.text("Invalid owner hash").color(NamedTextColor.RED);
|
||||
|
||||
context.trustLevel = correctOwnerHash ? TrustLevel.OWNER :
|
||||
correctAdminHash ? TrustLevel.ADMIN :
|
||||
correctHash ? TrustLevel.TRUSTED :
|
||||
TrustLevel.PUBLIC;
|
||||
context.trustLevel = userTrustLevel;
|
||||
}
|
||||
}
|
||||
|
||||
if (!console && command.consoleOnly) return Component.text("This command can only be ran via console").color(NamedTextColor.RED);
|
||||
if (!console && command.consoleOnly) return Component.text("This command can only be run via console").color(NamedTextColor.RED);
|
||||
|
||||
// should these be here?
|
||||
context.fullArgs = fullArgs;
|
||||
|
|
|
@ -33,6 +33,7 @@ import org.jetbrains.annotations.NotNull;
|
|||
import java.awt.*;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
// please ignore my ohio code
|
||||
// also this is one of the classes which has >100 lines or actually >400 LMAO
|
||||
|
@ -81,18 +82,13 @@ public class DiscordPlugin {
|
|||
for (Bot bot : Main.bots) {
|
||||
final String channelId = servers.get(bot.getServerString(true));
|
||||
|
||||
bot.executor.scheduleAtFixedRate(() -> onDiscordTick(channelId), 0, 50, TimeUnit.MILLISECONDS);
|
||||
|
||||
totalConnects.put(channelId, 0); // is this necessary?
|
||||
|
||||
bot.addListener(new Bot.Listener() {
|
||||
@Override
|
||||
public void loadedPlugins (Bot bot) {
|
||||
bot.tick.addListener(new TickPlugin.Listener() {
|
||||
@Override
|
||||
public void onAlwaysTick () {
|
||||
onDiscordTick(channelId);
|
||||
}
|
||||
});
|
||||
|
||||
bot.chat.addListener(new ChatPlugin.Listener() {
|
||||
@Override
|
||||
public boolean systemMessageReceived (Component component, String string, String _ansi) {
|
||||
|
@ -165,7 +161,6 @@ public class DiscordPlugin {
|
|||
jda.addEventListener(new ListenerAdapter() {
|
||||
@Override
|
||||
public void onMessageReceived(@NotNull MessageReceivedEvent event) {
|
||||
// TODO: IMPROVE this code
|
||||
if (
|
||||
!event.getChannel().getId().equals(channelId) ||
|
||||
event.getAuthor().getId().equals(jda.getSelfUser().getId()) ||
|
||||
|
@ -218,6 +213,8 @@ public class DiscordPlugin {
|
|||
}
|
||||
|
||||
private Component getMessageComponent (Bot bot, Message message) {
|
||||
// TODO: IMPROVE this code
|
||||
|
||||
// ignore my very very ohio code,..,,.
|
||||
|
||||
Component attachmentsComponent = Component.empty();
|
||||
|
|
|
@ -2,6 +2,7 @@ package me.chayapak1.chomens_bot.plugins;
|
|||
|
||||
import com.google.common.hash.Hashing;
|
||||
import me.chayapak1.chomens_bot.Bot;
|
||||
import me.chayapak1.chomens_bot.command.TrustLevel;
|
||||
import me.chayapak1.chomens_bot.data.player.PlayerEntry;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
@ -59,4 +60,11 @@ public class HashingPlugin {
|
|||
return checkHash(getOwnerHash(prefix, sender, true), input) ||
|
||||
checkHash(getOwnerHash(prefix, sender, false), input);
|
||||
}
|
||||
|
||||
public TrustLevel getTrustLevel (String input, String prefix, PlayerEntry sender) {
|
||||
if (isCorrectOwnerHash(input, prefix, sender)) return TrustLevel.OWNER;
|
||||
else if (isCorrectAdminHash(input, prefix, sender)) return TrustLevel.ADMIN;
|
||||
else if (isCorrectHash(input, prefix, sender)) return TrustLevel.TRUSTED;
|
||||
else return TrustLevel.PUBLIC;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue