-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #546 from aternosorg/new-slash-command-permissions
New slash command permissions
- Loading branch information
Showing
9 changed files
with
427 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import {RESTJSONErrorCodes} from 'discord.js'; | ||
import SlashCommandPermissionOverrides from './SlashCommandPermissionOverrides.js'; | ||
import bot from '../../bot/Bot.js'; | ||
|
||
/** | ||
* Emulate Discord Slash Command Permissions | ||
* @abstract | ||
*/ | ||
export default class SlashCommandPermissionManager { | ||
|
||
/** | ||
* Calculates if a member has the permission to execute a command in a guild | ||
* Uses the older V2 Permission system: https://discord.com/developers/docs/change-log#updated-command-permissions | ||
* @param {import('discord.js').Interaction<"cached">} interaction | ||
* @param {Command} command | ||
* @return {Promise<boolean>} | ||
*/ | ||
async hasPermission(interaction, command) { | ||
throw new Error('Not implemented'); | ||
} | ||
|
||
/** | ||
* fetch the overrides for a command or application | ||
* @param {import('discord.js').Interaction<"cached">} interaction | ||
* @param {import('discord.js').Snowflake} [commandId] leave empty to fetch global permissions | ||
* @return {Promise<SlashCommandPermissionOverrides>} | ||
*/ | ||
async fetchOverrides(interaction, commandId = bot.client.user.id) { | ||
let overrides = []; | ||
try { | ||
overrides = await interaction.guild.commands.permissions.fetch({command: commandId}); | ||
|
||
} | ||
catch (e) { | ||
if (e.code === RESTJSONErrorCodes.UnknownApplicationCommandPermissions) { | ||
overrides = []; | ||
} else { | ||
throw e; | ||
} | ||
} | ||
|
||
return new SlashCommandPermissionOverrides(overrides, interaction.guild, interaction.member, interaction.channel); | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
src/discord/permissions/SlashCommandPermissionManagerV2.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import {PermissionFlagsBits} from 'discord.js'; | ||
import SlashCommandPermissionManager from './SlashCommandPermissionManager.js'; | ||
|
||
export default class SlashCommandPermissionManagerV2 extends SlashCommandPermissionManager { | ||
/** | ||
* Calculates if a member has the permission to execute a command in a guild | ||
* Uses the older V2 Permission system: https://discord.com/developers/docs/change-log#updated-command-permissions | ||
* @param {import('discord.js').Interaction<"cached">} interaction | ||
* @param {Command} command | ||
* @return {Promise<boolean>} | ||
*/ | ||
async hasPermission(interaction, command) { | ||
if (interaction.memberPermissions.has(PermissionFlagsBits.Administrator)) { | ||
return true; | ||
} | ||
|
||
if (!interaction.memberPermissions.has(PermissionFlagsBits.UseApplicationCommands)) { | ||
return false; | ||
} | ||
|
||
// Check permissions for specific command if they exist | ||
const commandPermissions = await this.fetchOverrides(interaction, command.id); | ||
if (commandPermissions.rawOverrides.length) { | ||
return this.hasPermissionInOverrides(commandPermissions); | ||
} | ||
|
||
// Fallback to global permissions if they exist | ||
const globalPermissions = await this.fetchOverrides(interaction); | ||
if (globalPermissions.rawOverrides.length) { | ||
return this.hasPermissionInOverrides(globalPermissions); | ||
} | ||
|
||
// Fallback to default permissions | ||
switch (command.getDefaultMemberPermissions()) { | ||
case null: | ||
return true; | ||
case 0: | ||
return false; | ||
default: | ||
return interaction.memberPermissions.has(command.getDefaultMemberPermissions()); | ||
} | ||
} | ||
|
||
/** | ||
* @param {SlashCommandPermissionOverrides} overrides | ||
* @return {Promise<?boolean>} | ||
*/ | ||
async hasPermissionInOverrides(overrides) { | ||
let permission = null; | ||
|
||
// check channel permissions | ||
if (overrides.channelOverride) { | ||
if (!overrides.channelOverride.permission) { | ||
return false; | ||
} | ||
} | ||
else if (overrides.allChannelsOverride) { | ||
if (!overrides.allChannelsOverride.permission) { | ||
return false; | ||
} | ||
} | ||
|
||
// Apply permissions for the default role (@everyone). | ||
if (overrides.everyoneOverride) { | ||
permission = overrides.everyoneOverride.permission; | ||
} | ||
|
||
// Apply denies for all additional roles the guild member has at once. | ||
if (overrides.memberRoleOverrides.some(override => !override.permission)) { | ||
permission = false; | ||
} | ||
|
||
// Apply allows for all additional roles the guild member has at once. | ||
if (overrides.memberRoleOverrides.some(override => override.permission)) { | ||
permission = true; | ||
} | ||
|
||
// Apply permissions for the specific guild member if they exist. | ||
if (overrides.memberOverride) { | ||
permission = overrides.memberOverride.permission; | ||
} | ||
|
||
return permission; | ||
} | ||
} |
Oops, something went wrong.