From b2e77438f0e2a649404cbdc8f5e0d14ca2704a05 Mon Sep 17 00:00:00 2001 From: RyanLandDev Date: Tue, 17 Aug 2021 13:24:50 +0200 Subject: [PATCH] Added Permission Overrides + Moved cacheJson method to static block --- src/main/java/com/diamondfire/helpbot/HelpBot.java | 3 +-- .../command/executor/checks/PermissionCheck.java | 4 +++- .../command/impl/other/tag/AddTagSubCommand.java | 3 ++- .../command/impl/other/tag/EditTagSubCommand.java | 3 ++- .../impl/other/tag/RemoveTagSubCommand.java | 3 ++- .../bot/command/permissions/Permission.java | 14 +++++++++++++- .../diamondfire/helpbot/sys/tag/TagHandler.java | 8 ++++++++ 7 files changed, 31 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/diamondfire/helpbot/HelpBot.java b/src/main/java/com/diamondfire/helpbot/HelpBot.java index bba770ef..bd820c9e 100644 --- a/src/main/java/com/diamondfire/helpbot/HelpBot.java +++ b/src/main/java/com/diamondfire/helpbot/HelpBot.java @@ -11,9 +11,8 @@ public class HelpBot { - public static void main(String[] args) throws LoginException, IOException { + public static void main(String[] args) throws LoginException { CodeDatabase.initialize(); - TagHandler.cacheJson(); HelpBotInstance.initialize(); CodeDifferenceHandler.refresh(); } diff --git a/src/main/java/com/diamondfire/helpbot/bot/command/executor/checks/PermissionCheck.java b/src/main/java/com/diamondfire/helpbot/bot/command/executor/checks/PermissionCheck.java index e80f1642..c233a1e7 100644 --- a/src/main/java/com/diamondfire/helpbot/bot/command/executor/checks/PermissionCheck.java +++ b/src/main/java/com/diamondfire/helpbot/bot/command/executor/checks/PermissionCheck.java @@ -1,5 +1,6 @@ package com.diamondfire.helpbot.bot.command.executor.checks; +import com.diamondfire.helpbot.bot.command.permissions.Permission; import com.diamondfire.helpbot.bot.command.reply.PresetBuilder; import com.diamondfire.helpbot.bot.command.reply.feature.informative.*; import com.diamondfire.helpbot.bot.events.CommandEvent; @@ -8,7 +9,8 @@ public class PermissionCheck implements CommandCheck { @Override public boolean check(CommandEvent event) { - return event.getCommand().getPermission().hasPermission(event.getMember()); + return event.getCommand().getPermission().hasPermission(event.getMember()) + || Permission.getOverrides(event.getCommand()).contains(event.getAuthor().getIdLong()); } @Override diff --git a/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/tag/AddTagSubCommand.java b/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/tag/AddTagSubCommand.java index 6597ed6b..e5350dbb 100644 --- a/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/tag/AddTagSubCommand.java +++ b/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/tag/AddTagSubCommand.java @@ -46,7 +46,8 @@ protected ArgumentSet compileArguments() { @Override public Permission getPermission() { - return Permission.EXPERT; + return Permission.EXPERT + .setOverrides(this, 808966728201666620L); } @Override diff --git a/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/tag/EditTagSubCommand.java b/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/tag/EditTagSubCommand.java index 8a615fcb..9d3700bb 100644 --- a/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/tag/EditTagSubCommand.java +++ b/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/tag/EditTagSubCommand.java @@ -42,7 +42,8 @@ protected ArgumentSet compileArguments() { @Override public Permission getPermission() { - return Permission.EXPERT; + return Permission.EXPERT + .setOverrides(this, 808966728201666620L); } @Override diff --git a/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/tag/RemoveTagSubCommand.java b/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/tag/RemoveTagSubCommand.java index 6511d6f9..c59736c3 100644 --- a/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/tag/RemoveTagSubCommand.java +++ b/src/main/java/com/diamondfire/helpbot/bot/command/impl/other/tag/RemoveTagSubCommand.java @@ -38,7 +38,8 @@ protected ArgumentSet compileArguments() { @Override public Permission getPermission() { - return Permission.EXPERT; + return Permission.EXPERT + .setOverrides(this, 808966728201666620L); } @Override diff --git a/src/main/java/com/diamondfire/helpbot/bot/command/permissions/Permission.java b/src/main/java/com/diamondfire/helpbot/bot/command/permissions/Permission.java index bd6cdc5f..8f69f446 100644 --- a/src/main/java/com/diamondfire/helpbot/bot/command/permissions/Permission.java +++ b/src/main/java/com/diamondfire/helpbot/bot/command/permissions/Permission.java @@ -1,8 +1,10 @@ package com.diamondfire.helpbot.bot.command.permissions; +import com.diamondfire.helpbot.bot.command.impl.Command; import net.dv8tion.jda.api.entities.Member; -import java.util.HashMap; +import java.util.*; +import java.util.stream.Collectors; public enum Permission { BOT_DEVELOPER(589238520145510400L, 999), @@ -16,6 +18,7 @@ public enum Permission { USER(349434193517740033L, 1); private static final HashMap roleMap = new HashMap<>(); + private static final HashMap> overrides = new HashMap<>(); static { for (Permission perm : values()) { @@ -39,6 +42,15 @@ public static Permission fromRole(long roleID) { return perm; } + public Permission setOverrides(Command command, Long... userIds) { + overrides.put(command, Arrays.stream(userIds).collect(Collectors.toSet())); + return this; + } + + public static Set getOverrides(Command command) { + return Objects.requireNonNullElse(overrides.get(command), new HashSet<>()); + } + public long getRole() { return role; } diff --git a/src/main/java/com/diamondfire/helpbot/sys/tag/TagHandler.java b/src/main/java/com/diamondfire/helpbot/sys/tag/TagHandler.java index 21a660ee..28c9a4d0 100644 --- a/src/main/java/com/diamondfire/helpbot/sys/tag/TagHandler.java +++ b/src/main/java/com/diamondfire/helpbot/sys/tag/TagHandler.java @@ -13,6 +13,14 @@ public class TagHandler { private static final List tags = new ArrayList<>(); private static final File FILE = ExternalFiles.TAGS; + static { + try { + cacheJson(); + } catch (IOException e) { + e.printStackTrace(); + } + } + public static List getTags() throws IOException { return tags; }