Skip to content

Commit

Permalink
Add "force user permissions" field (#91)
Browse files Browse the repository at this point in the history
* add alwaysRespectUserPermission option to interactions

* address review, these names are better
  • Loading branch information
Chew authored Jan 8, 2025
1 parent cba0a2c commit 9cb7839
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@ public Map<DiscordLocale, String> 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.
* <br>
* This defaults to false because it interferes with the server's options for interactions.
* <br>
* This has no effect for text based commands or DMs.
*/
protected boolean forceUserPermissions = false;

/**
* Gets the type of context menu.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
* <br>
* This defaults to false because it interferes with the server's options for interactions.
* <br>
* 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 /<parent name>
* <child name>}.
Expand Down Expand Up @@ -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;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
}
Expand Down

0 comments on commit 9cb7839

Please sign in to comment.