From 5712515e1f1caa135f20f150f391c41c4b401d02 Mon Sep 17 00:00:00 2001 From: Chew Date: Mon, 26 Aug 2024 13:16:39 -0400 Subject: [PATCH 1/2] add alwaysRespectUserPermission option to interactions --- .../jdautilities/command/ContextMenu.java | 10 +++++ .../command/MessageContextMenu.java | 35 ++++++++------- .../jdautilities/command/SlashCommand.java | 45 ++++++++++++------- .../jdautilities/command/UserContextMenu.java | 35 ++++++++------- 4 files changed, 74 insertions(+), 51 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..5b52dbd5 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 user permissions, even if the server overrides them, + * {@code false} if the command should ignore user permissions 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 alwaysRespectUserPermissions = 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..6914ffcc 100644 --- a/command/src/main/java/com/jagrosh/jdautilities/command/MessageContextMenu.java +++ b/command/src/main/java/com/jagrosh/jdautilities/command/MessageContextMenu.java @@ -58,29 +58,30 @@ public final void run(MessageContextMenuEvent event) if(event.isFromGuild()) { //user perms - for(Permission p: userPermissions) - { - // Member will never be null because this is only ran in a server (text channel) - if(event.getMember() == null) - continue; - - if(p.isChannel()) + if (alwaysRespectUserPermissions) + 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; + } } } - } // bot perms for(Permission p: botPermissions) 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..0fdaefda 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 user permissions, even if the server overrides them, + * {@code false} if the command should ignore user permissions 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 alwaysRespectUserPermissions = false; + /** * The child commands of the command. These are used in the format {@code / * }. @@ -221,29 +231,30 @@ public final void run(SlashCommandEvent event) if(event.getChannelType() != ChannelType.PRIVATE) { //user perms - for(Permission p: userPermissions) - { - // Member will never be null because this is only ran in a server (text channel) - if(event.getMember() == null) - continue; - - if(p.isChannel()) + if (alwaysRespectUserPermissions) + 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; + } } } - } // bot perms for(Permission p: botPermissions) 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..8bcd8762 100644 --- a/command/src/main/java/com/jagrosh/jdautilities/command/UserContextMenu.java +++ b/command/src/main/java/com/jagrosh/jdautilities/command/UserContextMenu.java @@ -95,29 +95,30 @@ public final void run(UserContextMenuEvent event) if(event.isFromGuild()) { //user perms - for(Permission p: userPermissions) - { - // Member will never be null because this is only ran in a server - if(event.getMember() == null) - continue; - - if(p.isChannel()) + if (alwaysRespectUserPermissions) + 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; + } } } - } // bot perms for(Permission p: botPermissions) From 62cd50fb3c99b187876f505fa8f51334221a6179 Mon Sep 17 00:00:00 2001 From: Chew Date: Wed, 8 Jan 2025 15:58:37 -0600 Subject: [PATCH 2/2] address review, these names are better --- .../com/jagrosh/jdautilities/command/ContextMenu.java | 6 +++--- .../jdautilities/command/MessageContextMenu.java | 4 +++- .../com/jagrosh/jdautilities/command/SlashCommand.java | 10 ++++++---- .../jagrosh/jdautilities/command/UserContextMenu.java | 4 +++- 4 files changed, 15 insertions(+), 9 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 5b52dbd5..13afd972 100644 --- a/command/src/main/java/com/jagrosh/jdautilities/command/ContextMenu.java +++ b/command/src/main/java/com/jagrosh/jdautilities/command/ContextMenu.java @@ -64,14 +64,14 @@ public Map getNameLocalization() { } /** - * {@code true} if the command should always respect user permissions, even if the server overrides them, - * {@code false} if the command should ignore user permissions if the server overrides them. + * {@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 alwaysRespectUserPermissions = false; + 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 6914ffcc..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,7 +58,8 @@ public final void run(MessageContextMenuEvent event) if(event.isFromGuild()) { //user perms - if (alwaysRespectUserPermissions) + if (forceUserPermissions) + { for(Permission p: userPermissions) { // Member will never be null because this is only ran in a server (text channel) @@ -82,6 +83,7 @@ public final void run(MessageContextMenuEvent event) } } } + } // bot perms for(Permission p: botPermissions) 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 0fdaefda..656836ce 100644 --- a/command/src/main/java/com/jagrosh/jdautilities/command/SlashCommand.java +++ b/command/src/main/java/com/jagrosh/jdautilities/command/SlashCommand.java @@ -100,14 +100,14 @@ public abstract class SlashCommand extends Command protected String requiredRole = null; /** - * {@code true} if the command should always respect user permissions, even if the server overrides them, - * {@code false} if the command should ignore user permissions if the server overrides them. + * {@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 alwaysRespectUserPermissions = false; + protected boolean forceUserPermissions = false; /** * The child commands of the command. These are used in the format {@code / @@ -231,7 +231,8 @@ public final void run(SlashCommandEvent event) if(event.getChannelType() != ChannelType.PRIVATE) { //user perms - if (alwaysRespectUserPermissions) + if (forceUserPermissions) + { for(Permission p: userPermissions) { // Member will never be null because this is only ran in a server (text channel) @@ -255,6 +256,7 @@ public final void run(SlashCommandEvent event) } } } + } // bot perms for(Permission p: botPermissions) 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 8bcd8762..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,7 +95,8 @@ public final void run(UserContextMenuEvent event) if(event.isFromGuild()) { //user perms - if (alwaysRespectUserPermissions) + if (forceUserPermissions) + { for(Permission p: userPermissions) { // Member will never be null because this is only ran in a server @@ -119,6 +120,7 @@ public final void run(UserContextMenuEvent event) } } } + } // bot perms for(Permission p: botPermissions)