From 9cb783980ccfaf7d4c293f2479003df3cc4adb25 Mon Sep 17 00:00:00 2001 From: Olivia Date: Wed, 8 Jan 2025 16:02:33 -0600 Subject: [PATCH] Add "force user permissions" field (#91) * add alwaysRespectUserPermission option to interactions * address review, these names are better --- .../jdautilities/command/ContextMenu.java | 10 +++++ .../command/MessageContextMenu.java | 33 +++++++------- .../jdautilities/command/SlashCommand.java | 43 ++++++++++++------- .../jdautilities/command/UserContextMenu.java | 33 +++++++------- 4 files changed, 74 insertions(+), 45 deletions(-) diff --git a/command/src/main/java/com/jagrosh/jdautilities/command/ContextMenu.java b/command/src/main/java/com/jagrosh/jdautilities/command/ContextMenu.java index 55749354..13afd972 100644 --- a/command/src/main/java/com/jagrosh/jdautilities/command/ContextMenu.java +++ b/command/src/main/java/com/jagrosh/jdautilities/command/ContextMenu.java @@ -63,6 +63,16 @@ public Map getNameLocalization() { return nameLocalization; } + /** + * {@code true} if the command should always respect {@link #userPermissions}, even if the server overrides them, + * {@code false} if the command should ignore {@link #userPermissions} if the server overrides them. + *
+ * This defaults to false because it interferes with the server's options for interactions. + *
+ * This has no effect for text based commands or DMs. + */ + protected boolean forceUserPermissions = false; + /** * Gets the type of context menu. * diff --git a/command/src/main/java/com/jagrosh/jdautilities/command/MessageContextMenu.java b/command/src/main/java/com/jagrosh/jdautilities/command/MessageContextMenu.java index 722ab772..1980ce7f 100644 --- a/command/src/main/java/com/jagrosh/jdautilities/command/MessageContextMenu.java +++ b/command/src/main/java/com/jagrosh/jdautilities/command/MessageContextMenu.java @@ -58,26 +58,29 @@ public final void run(MessageContextMenuEvent event) if(event.isFromGuild()) { //user perms - for(Permission p: userPermissions) + if (forceUserPermissions) { - // Member will never be null because this is only ran in a server (text channel) - if(event.getMember() == null) - continue; - - if(p.isChannel()) + for(Permission p: userPermissions) { - if(!event.getMember().hasPermission(event.getGuildChannel(), p)) + // Member will never be null because this is only ran in a server (text channel) + if(event.getMember() == null) + continue; + + if(p.isChannel()) { - terminate(event, String.format("%s%s%s", event.getClient().getError(), p.getName(), "channel")); - return; + if(!event.getMember().hasPermission(event.getGuildChannel(), p)) + { + terminate(event, String.format("%s%s%s", event.getClient().getError(), p.getName(), "channel")); + return; + } } - } - else - { - if(!event.getMember().hasPermission(p)) + else { - terminate(event, String.format("%s%s%s", event.getClient().getError(), p.getName(), "server")); - return; + if(!event.getMember().hasPermission(p)) + { + terminate(event, String.format("%s%s%s", event.getClient().getError(), p.getName(), "server")); + return; + } } } } diff --git a/command/src/main/java/com/jagrosh/jdautilities/command/SlashCommand.java b/command/src/main/java/com/jagrosh/jdautilities/command/SlashCommand.java index 554ab033..656836ce 100644 --- a/command/src/main/java/com/jagrosh/jdautilities/command/SlashCommand.java +++ b/command/src/main/java/com/jagrosh/jdautilities/command/SlashCommand.java @@ -99,6 +99,16 @@ public abstract class SlashCommand extends Command @Deprecated protected String requiredRole = null; + /** + * {@code true} if the command should always respect {@link #userPermissions}, even if the server overrides them, + * {@code false} if the command should ignore {@link #userPermissions} if the server overrides them. + *
+ * This defaults to false because it interferes with the server's options for interactions. + *
+ * This has no effect for text based commands or DMs. + */ + protected boolean forceUserPermissions = false; + /** * The child commands of the command. These are used in the format {@code / * }. @@ -221,26 +231,29 @@ public final void run(SlashCommandEvent event) if(event.getChannelType() != ChannelType.PRIVATE) { //user perms - for(Permission p: userPermissions) + if (forceUserPermissions) { - // Member will never be null because this is only ran in a server (text channel) - if(event.getMember() == null) - continue; - - if(p.isChannel()) + for(Permission p: userPermissions) { - if(!event.getMember().hasPermission(event.getGuildChannel(), p)) + // Member will never be null because this is only ran in a server (text channel) + if(event.getMember() == null) + continue; + + if(p.isChannel()) { - terminate(event, String.format(userMissingPermMessage, client.getError(), p.getName(), "channel"), client); - return; + if(!event.getMember().hasPermission(event.getGuildChannel(), p)) + { + terminate(event, String.format(userMissingPermMessage, client.getError(), p.getName(), "channel"), client); + return; + } } - } - else - { - if(!event.getMember().hasPermission(p)) + else { - terminate(event, String.format(userMissingPermMessage, client.getError(), p.getName(), "server"), client); - return; + if(!event.getMember().hasPermission(p)) + { + terminate(event, String.format(userMissingPermMessage, client.getError(), p.getName(), "server"), client); + return; + } } } } diff --git a/command/src/main/java/com/jagrosh/jdautilities/command/UserContextMenu.java b/command/src/main/java/com/jagrosh/jdautilities/command/UserContextMenu.java index b9072b78..6db66f74 100644 --- a/command/src/main/java/com/jagrosh/jdautilities/command/UserContextMenu.java +++ b/command/src/main/java/com/jagrosh/jdautilities/command/UserContextMenu.java @@ -95,26 +95,29 @@ public final void run(UserContextMenuEvent event) if(event.isFromGuild()) { //user perms - for(Permission p: userPermissions) + if (forceUserPermissions) { - // Member will never be null because this is only ran in a server - if(event.getMember() == null) - continue; - - if(p.isChannel()) + for(Permission p: userPermissions) { - if(!event.getMember().hasPermission(event.getGuildChannel(), p)) + // Member will never be null because this is only ran in a server + if(event.getMember() == null) + continue; + + if(p.isChannel()) { - terminate(event, String.format("%s%s%s", event.getClient().getError(), p.getName(), "channel")); - return; + if(!event.getMember().hasPermission(event.getGuildChannel(), p)) + { + terminate(event, String.format("%s%s%s", event.getClient().getError(), p.getName(), "channel")); + return; + } } - } - else - { - if(!event.getMember().hasPermission(p)) + else { - terminate(event, String.format("%s%s%s", event.getClient().getError(), p.getName(), "server")); - return; + if(!event.getMember().hasPermission(p)) + { + terminate(event, String.format("%s%s%s", event.getClient().getError(), p.getName(), "server")); + return; + } } } }