Skip to content

Commit

Permalink
add alwaysRespectUserPermission option to interactions
Browse files Browse the repository at this point in the history
  • Loading branch information
Chew committed Aug 26, 2024
1 parent cba0a2c commit 5712515
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 51 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 user permissions, even if the server overrides them,
* {@code false} if the command should ignore user permissions 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 alwaysRespectUserPermissions = false;

/**
* Gets the type of context menu.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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 user permissions, even if the server overrides them,
* {@code false} if the command should ignore user permissions 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 alwaysRespectUserPermissions = false;

/**
* The child commands of the command. These are used in the format {@code /<parent name>
* <child name>}.
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 5712515

Please sign in to comment.