diff --git a/velocity/src/main/java/dev/orewaee/discordauth/velocity/discord/Bot.java b/velocity/src/main/java/dev/orewaee/discordauth/velocity/discord/Bot.java index cfc10e6..78d49ba 100644 --- a/velocity/src/main/java/dev/orewaee/discordauth/velocity/discord/Bot.java +++ b/velocity/src/main/java/dev/orewaee/discordauth/velocity/discord/Bot.java @@ -31,6 +31,7 @@ public class Bot { public Bot(Config config) throws IOException { this.config = config; + this.permissionUtils = new PermissionUtils(config); String token = config.getString("discord.token", ""); if (token.isEmpty()) throw new IOException("discord token missing"); @@ -44,8 +45,6 @@ public Bot(Config config) throws IOException { this.jda = builder.build(); addCommands(); - - this.permissionUtils = new PermissionUtils(config); } private void applySettings() { diff --git a/velocity/src/main/java/dev/orewaee/discordauth/velocity/discord/listeners/AddListener.java b/velocity/src/main/java/dev/orewaee/discordauth/velocity/discord/listeners/AddListener.java index 7c0c853..4f79cc3 100644 --- a/velocity/src/main/java/dev/orewaee/discordauth/velocity/discord/listeners/AddListener.java +++ b/velocity/src/main/java/dev/orewaee/discordauth/velocity/discord/listeners/AddListener.java @@ -40,10 +40,7 @@ public AddListener(Config config, PermissionUtils permissionUtils) { @Override public void onSlashCommandInteraction(@NotNull SlashCommandInteractionEvent event) { - Member eventMember = event.getMember(); - if (eventMember == null) return; - - if (permissionUtils.hasPermission(eventMember)) { + if (!permissionUtils.hasPermission(event)) { String content = config .getString(NO_PERMISSION, "You don't have permission to use it"); diff --git a/velocity/src/main/java/dev/orewaee/discordauth/velocity/discord/listeners/ListListener.java b/velocity/src/main/java/dev/orewaee/discordauth/velocity/discord/listeners/ListListener.java index 0681561..84cfbc7 100644 --- a/velocity/src/main/java/dev/orewaee/discordauth/velocity/discord/listeners/ListListener.java +++ b/velocity/src/main/java/dev/orewaee/discordauth/velocity/discord/listeners/ListListener.java @@ -43,10 +43,7 @@ public ListListener(Config config, PermissionUtils permissionUtils) { @Override public void onSlashCommandInteraction(@NotNull SlashCommandInteractionEvent event) { - Member eventMember = event.getMember(); - if (eventMember == null) return; - - if (permissionUtils.hasPermission(eventMember)) { + if (!permissionUtils.hasPermission(event)) { String content = config .getString(NO_PERMISSION, "You don't have permission to use it"); diff --git a/velocity/src/main/java/dev/orewaee/discordauth/velocity/discord/listeners/RemoveByDiscordIdListener.java b/velocity/src/main/java/dev/orewaee/discordauth/velocity/discord/listeners/RemoveByDiscordIdListener.java index 900bad3..07a7cb0 100644 --- a/velocity/src/main/java/dev/orewaee/discordauth/velocity/discord/listeners/RemoveByDiscordIdListener.java +++ b/velocity/src/main/java/dev/orewaee/discordauth/velocity/discord/listeners/RemoveByDiscordIdListener.java @@ -39,10 +39,7 @@ public RemoveByDiscordIdListener(Config config, PermissionUtils permissionUtils) @Override public void onSlashCommandInteraction(@NotNull SlashCommandInteractionEvent event) { - Member member = event.getMember(); - if (member == null) return; - - if (permissionUtils.hasPermission(member)) { + if (!permissionUtils.hasPermission(event)) { String content = config .getString(NO_PERMISSION, "You don't have permission to use it"); diff --git a/velocity/src/main/java/dev/orewaee/discordauth/velocity/discord/listeners/RemoveByNameListener.java b/velocity/src/main/java/dev/orewaee/discordauth/velocity/discord/listeners/RemoveByNameListener.java index 99363f5..5af9e01 100644 --- a/velocity/src/main/java/dev/orewaee/discordauth/velocity/discord/listeners/RemoveByNameListener.java +++ b/velocity/src/main/java/dev/orewaee/discordauth/velocity/discord/listeners/RemoveByNameListener.java @@ -39,10 +39,7 @@ public RemoveByNameListener(Config config, PermissionUtils permissionUtils) { @Override public void onSlashCommandInteraction(@NotNull SlashCommandInteractionEvent event) { - Member eventMember = event.getMember(); - if (eventMember == null) return; - - if (permissionUtils.hasPermission(eventMember)) { + if (!permissionUtils.hasPermission(event)) { String content = config .getString(NO_PERMISSION, "You don't have permission to use it"); diff --git a/velocity/src/main/java/dev/orewaee/discordauth/velocity/discord/listeners/SyncListener.java b/velocity/src/main/java/dev/orewaee/discordauth/velocity/discord/listeners/SyncListener.java index b340dfa..751612a 100644 --- a/velocity/src/main/java/dev/orewaee/discordauth/velocity/discord/listeners/SyncListener.java +++ b/velocity/src/main/java/dev/orewaee/discordauth/velocity/discord/listeners/SyncListener.java @@ -37,10 +37,7 @@ public SyncListener(Config config, PermissionUtils permissionUtils) { @Override public void onSlashCommandInteraction(@NotNull SlashCommandInteractionEvent event) { - Member eventMember = event.getMember(); - if (eventMember == null) return; - - if (permissionUtils.hasPermission(eventMember)) { + if (!permissionUtils.hasPermission(event)) { String content = config .getString(NO_PERMISSION, "You don't have permission to use it"); diff --git a/velocity/src/main/java/dev/orewaee/discordauth/velocity/discord/utils/PermissionUtils.java b/velocity/src/main/java/dev/orewaee/discordauth/velocity/discord/utils/PermissionUtils.java index f70cf14..92bbeab 100644 --- a/velocity/src/main/java/dev/orewaee/discordauth/velocity/discord/utils/PermissionUtils.java +++ b/velocity/src/main/java/dev/orewaee/discordauth/velocity/discord/utils/PermissionUtils.java @@ -3,6 +3,7 @@ import dev.orewaee.discordauth.common.config.Config; import net.dv8tion.jda.api.entities.Member; import net.dv8tion.jda.api.entities.Role; +import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; import java.util.List; @@ -15,18 +16,19 @@ public PermissionUtils(Config config) { this.config = config; } - public boolean hasPermission(Member member) { + public boolean hasPermission(SlashCommandInteractionEvent event) { List ids = config.getList(DISCORD_IDS, List.of()); - String userId = member.getUser().getId(); + String userId = event.getUser().getId(); if (ids.contains(userId)) return true; - for (Role role : member.getRoles()) { - String roleId = role.getId(); + Member eventMember = event.getMember(); + if (eventMember != null) + for (Role role : eventMember.getRoles()) { + String roleId = role.getId(); - if (ids.contains(roleId)) - return true; - } + if (ids.contains(roleId)) return true; + } return false; } diff --git a/velocity/src/main/java/dev/orewaee/discordauth/velocity/discord/utils/Utils.java b/velocity/src/main/java/dev/orewaee/discordauth/velocity/discord/utils/Utils.java deleted file mode 100644 index c1717b0..0000000 --- a/velocity/src/main/java/dev/orewaee/discordauth/velocity/discord/utils/Utils.java +++ /dev/null @@ -1,11 +0,0 @@ -package dev.orewaee.discordauth.velocity.discord.utils; - -import dev.orewaee.discordauth.common.config.Config; -import dev.orewaee.discordauth.velocity.DiscordAuth; -import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent; - -public class Utils { - protected void hasPermission(SlashCommandInteractionEvent event) { - String userId = event.getUser().getId(); - } -}