Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(permissions): Slash Permissions v2 #1372

Merged
merged 34 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
1f858dd
Remove bulk editing of command permissions as the endpoint has been d…
coolcalcacol May 7, 2022
dfac966
Add missing channel constant, and update formatting
coolcalcacol May 7, 2022
4d33a9b
Fix camelCasing
coolcalcacol May 10, 2022
de732aa
jsDoc fix
coolcalcacol May 10, 2022
c3e076d
Update index.d.ts
coolcalcacol May 12, 2022
2c831fe
Update JSDoc and fix bulkEditCommands/bulkEditGuildCommands
coolcalcacol May 21, 2022
a6a457a
replace accidental removal of char
coolcalcacol May 21, 2022
6aadf10
Merge branch 'abalabahaha:dev' into slash-permissions-v2
coolcalcacol May 21, 2022
bb13698
Update index.d.ts
coolcalcacol May 22, 2022
1fc0e5b
Update lib/Client.js
coolcalcacol May 22, 2022
7e3fb6b
Update lib/Client.js
coolcalcacol May 22, 2022
b94ea81
Merge branch 'dev' into slash-permissions-v2
DonovanDMC Jun 6, 2022
4ec560c
Merge branch 'dev' into slash-permissions-v2
coolcalcacol Jun 7, 2022
a05445a
Merge branch 'dev' into slash-permissions-v2
DonovanDMC Jun 9, 2022
9d03cdc
Merge branch 'dev' into slash-permissions-v2
DonovanDMC Jun 30, 2022
e118276
permission handling
coolcalcacol Aug 26, 2022
b633a37
Merge branch 'dev' into slash-permissions-v2
coolcalcacol Aug 26, 2022
19b73d8
Merge branch 'dev' of https://github.com/abalabahaha/eris into 1372
bsian03 Jan 5, 2024
979a91b
New ApplicationCommand class (props not migrated)
bsian03 Jan 5, 2024
f599b44
Correct jsdoc types
bsian03 Jan 6, 2024
a6d44f7
Lint
bsian03 Jan 6, 2024
d05afff
Typings for ApplicationCommand class
bsian03 Jan 6, 2024
9d3ae5c
Remove bulkEditCommandPermissions
bsian03 Jan 6, 2024
f94cd1c
Nullable permissions, add back defaultPermission [todo]
bsian03 Jan 6, 2024
00a42b4
JSDoc consistency
bsian03 Jan 6, 2024
1a2b9df
Type event properly JSDOC [INCOMPLETE]
bsian03 Jan 6, 2024
aa8485a
Correct options.defaultMemberPermissions jsdoc
bsian03 Jan 6, 2024
fb0ab2d
JSDoc fix
bsian03 Jan 6, 2024
fcd4191
Fix up jsdoc
bsian03 Jan 7, 2024
5bc2fea
Add defaultMember deprecations
bsian03 Jan 7, 2024
3f13c76
Merge branch 'dev' of https://github.com/abalabahaha/eris into 1372
bsian03 Jan 7, 2024
aa08f51
Add audit log reason to edit command perms
bsian03 Jan 7, 2024
41b541d
Actually respond with application command class
bsian03 Jan 7, 2024
fa574bc
Merge branch 'dev' into slash-permissions-v2
bsian03 Jan 9, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ declare namespace Eris {
// Application Commands
interface ApplicationCommand<T extends ApplicationCommandTypes = ApplicationCommandTypes> {
application_id: string;
defaultPermission?: boolean;
bsian03 marked this conversation as resolved.
Show resolved Hide resolved
defaultMemberPermissions?: string;
coolcalcacol marked this conversation as resolved.
Show resolved Hide resolved
dmPermission?: boolean;
description: T extends Constants["ApplicationCommandTypes"]["CHAT_INPUT"] ? string : never;
guild_id?: string;
id: string;
Expand Down Expand Up @@ -682,6 +683,7 @@ declare namespace Eris {
selfVideo: boolean;
}
interface EventListeners {
applicationCommandPermissionsUpdate: [applicationCommandPermissions: GuildApplicationCommandPermissions];
bsian03 marked this conversation as resolved.
Show resolved Hide resolved
callCreate: [call: Call];
callDelete: [call: Call];
callRing: [call: Call];
Expand Down Expand Up @@ -1472,8 +1474,9 @@ declare namespace Eris {
NUMBER: 10;
};
ApplicationCommandPermissionTypes: {
ROLE: 1;
USER: 2;
ROLE: 1;
USER: 2;
CHANNEL: 3;
};
ApplicationCommandTypes: {
CHAT_INPUT: 1;
Expand Down Expand Up @@ -2117,7 +2120,6 @@ declare namespace Eris {
addRelationship(userID: string, block?: boolean): Promise<void>;
addSelfPremiumSubscription(token: string, plan: string): Promise<void>;
banGuildMember(guildID: string, userID: string, deleteMessageDays?: number, reason?: string): Promise<void>;
bulkEditCommandPermissions(guildID: string, permissions: { id: string; permissions: ApplicationCommandPermissions[] }[]): Promise<GuildApplicationCommandPermissions[]>;
bulkEditCommands(commands: ApplicationCommandStructure[]): Promise<ApplicationCommand[]>;
bulkEditGuildCommands(guildID: string, commands: ApplicationCommandStructure[]): Promise<ApplicationCommand[]>;
closeVoiceConnection(guildID: string): void;
Expand Down Expand Up @@ -2234,7 +2236,7 @@ declare namespace Eris {
createCommand(command: ApplicationCommandStructure): Promise<ApplicationCommand>;
createGroupChannel(userIDs: string[]): Promise<GroupChannel>;
createGuild(name: string, options?: CreateGuildOptions): Promise<Guild>;
createGuildCommand(guildID: string, command: ApplicationCommandStructure): Promise<ApplicationCommand>;
createGuildCommand(guildID: string, command: Omit<ApplicationCommandStructure, "dmPermission">): Promise<Omit<ApplicationCommand, "dmPermission">>;
bsian03 marked this conversation as resolved.
Show resolved Hide resolved
createGuildEmoji(guildID: string, options: EmojiOptions, reason?: string): Promise<Emoji>;
createGuildFromTemplate(code: string, name: string, icon?: string): Promise<Guild>;
createGuildSticker(guildID: string, options: CreateStickerOptions, reason?: string): Promise<Sticker>;
Expand Down
54 changes: 23 additions & 31 deletions lib/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,36 +344,23 @@ class Client extends EventEmitter {
});
}

/**
* Edits command permissions for a multiple commands in a guild.
* Note: You can only add up to 10 permission overwrites for a command.
* @arg {String} guildID The guild id
* @arg {Array<Object>} permissions An array of [partial guild command permissions](https://discord.com/developers/docs/interactions/application-commands#application-command-permissions-object-guild-application-command-permissions-structure)
* @returns {Promise<Array<Object>>} Returns an array of [GuildApplicationCommandPermissions](https://discord.com/developers/docs/interactions/application-commands#application-command-permissions-object-guild-application-command-permissions-structure) objects.
*/
bulkEditCommandPermissions(guildID, permissions) {
if(!guildID) {
throw new Error("You must provide an id of the guild whose permissions you want to edit.");
}

return this.requestHandler.request("PUT", Endpoints.GUILD_COMMAND_PERMISSIONS(this.application.id, guildID), true, permissions);
}

/**
* Bulk create/edit global application commands
* @arg {Array<Object>} commands An array of [Command objects](https://discord.com/developers/docs/interactions/application-commands#application-command-object)
* @returns {Promise<Array>} Resolves with an array of commands objects
*/
bulkEditCommands(commands) {
for(const command of commands) {
if(command.name !== undefined){
if(command.name !== undefined) {
if(command.type === 1 || command.type === undefined) {
command.name = command.name.toLowerCase();
if(!command.name.match(/^[\w-]{1,32}$/)) {
throw new Error("Slash Command names must match the regular expression \"^[\\w-]{1,32}$\"");
}
}
}
command.default_member_permissions = command.defaultMemberPermissions;
bsian03 marked this conversation as resolved.
Show resolved Hide resolved
command.dm_permission = command.dmPermission;
}
return this.requestHandler.request("PUT", Endpoints.COMMANDS(this.application.id), true, commands);
}
Expand All @@ -386,14 +373,15 @@ class Client extends EventEmitter {
*/
bulkEditGuildCommands(guildID, commands) {
for(const command of commands) {
if(command.name !== undefined){
if(command.name !== undefined) {
if(command.type === 1 || command.type === undefined) {
command.name = command.name.toLowerCase();
if(!command.name.match(/^[\w-]{1,32}$/)) {
throw new Error("Slash Command names must match the regular expression \"^[\\w-]{1,32}$\"");
}
}
}
command.default_member_permissions = command.defaultMemberPermissions;
}
return this.requestHandler.request("PUT", Endpoints.GUILD_COMMANDS(this.application.id, guildID), true, commands);
}
Expand Down Expand Up @@ -554,19 +542,21 @@ class Client extends EventEmitter {
* @arg {String} [command.description] The command description (Slash Commands Only)
* @arg {Array<Object>} [command.options] An array of [command options](https://discord.com/developers/docs/interactions/application-commands#application-command-object-application-command-option-structure)
* @arg {Number} [type=1] The type of application command, 1 for slash command, 2 for user, and 3 for message
* @arg {Boolean} [command.defaultPermission=true] Whether the command is enabled by default when the app is added to a guild
* @returns {Promise<Object>} Resolves with a commands object
* @arg {String?} [command.defaultMemberPermissions] The default member [permissions](https://discord.com/developers/docs/topics/permissions) represented as a bit set
coolcalcacol marked this conversation as resolved.
Show resolved Hide resolved
* @arg {Boolean?} [command.dmPermission] Indicates whether the command is available in DMs with the app. By default, commands are visible.
* @returns {Promise<Object>} Resolves with a commands object
bsian03 marked this conversation as resolved.
Show resolved Hide resolved
*/
createCommand(command) {
if(command.name !== undefined){
if(command.name !== undefined) {
if(command.type === 1 || command.type === undefined) {
command.name = command.name.toLowerCase();
if(!command.name.match(/^[\w-]{1,32}$/)) {
throw new Error("Slash Command names must match the regular expression \"^[\\w-]{1,32}$\"");
}
}
}
command.default_permission = command.defaultPermission;
command.default_member_permissions = command.defaultMemberPermissions;
command.dm_permission = command.dmPermission;
return this.requestHandler.request("POST", Endpoints.COMMANDS(this.application.id), true, command);
}

Expand Down Expand Up @@ -624,19 +614,19 @@ class Client extends EventEmitter {
* @arg {String} [command.description] The command description (Slash Commands Only)
* @arg {Array<Object>} [command.options] An array of [command options](https://discord.com/developers/docs/interactions/application-commands#application-command-object-application-command-option-structure)
* @arg {Number} [type=1] The type of application command, 1 for slash command, 2 for user, and 3 for message
* @arg {Boolean} [command.defaultPermission] Whether the command is enabled by default when the app is added to a guild
* @arg {String?} [command.defaultMemberPermissions] The default member [permissions](https://discord.com/developers/docs/topics/permissions) represented as a bit set
coolcalcacol marked this conversation as resolved.
Show resolved Hide resolved
* @returns {Promise<Object>} Resolves with a commands object
*/
createGuildCommand(guildID, command) {
if(command.name !== undefined){
if(command.name !== undefined) {
if(command.type === 1 || command.type === undefined) {
command.name = command.name.toLowerCase();
if(!command.name.match(/^[\w-]{1,32}$/)) {
throw new Error("Slash Command names must match the regular expression \"^[\\w-]{1,32}$\"");
}
}
}
command.default_permission = command.defaultPermission;
command.default_member_permissions = command.defaultMemberPermissions;
return this.requestHandler.request("POST", Endpoints.GUILD_COMMANDS(this.application.id, guildID), true, command);
}

Expand Down Expand Up @@ -1341,22 +1331,24 @@ class Client extends EventEmitter {
* @arg {String} command.name The command name
* @arg {String} [command.description] The command description (Slash Commands Only)
* @arg {Array<Object>} [command.options] An array of [command options](https://discord.com/developers/docs/interactions/application-commands#application-command-object-application-command-option-structure)
* @arg {Boolean} [command.defaultPermission] Whether the command is enabled by default when the app is added to a guild
* @returns {Promise<Object>} Resolves with a commands object
* @arg {String?} [command.defaultMemberPermissions] The default member [permissions](https://discord.com/developers/docs/topics/permissions) represented as a bit set
coolcalcacol marked this conversation as resolved.
Show resolved Hide resolved
* @arg {Boolean?} [command.dmPermission] Indicates whether the command is available in DMs with the app. By default, commands are visible.
* @returns {Promise<Object>} Resolves with a commands object
bsian03 marked this conversation as resolved.
Show resolved Hide resolved
*/
editCommand(commandID, command) {
if(!commandID) {
throw new Error("You must provide an id of the command to edit.");
}
if(command.name !== undefined){
if(command.name !== undefined) {
if(command.type === 1 || command.type === undefined) {
command.name = command.name.toLowerCase();
if(!command.name.match(/^[\w-]{1,32}$/)) {
throw new Error("Slash Command names must match the regular expression \"^[\\w-]{1,32}$\"");
}
}
}
command.default_permission = command.defaultPermission;
command.default_member_permissions = command.defaultMemberPermissions;
command.dm_permission = command.dmPermission;
return this.requestHandler.request("PATCH", Endpoints.COMMAND(this.application.id, commandID), true, command);
}

Expand Down Expand Up @@ -1434,22 +1426,22 @@ class Client extends EventEmitter {
* @arg {String} command.name The command name
* @arg {String} [command.description] The command description (Slash Commands Only)
* @arg {Array<Object>} [command.options] An array of [command options](https://discord.com/developers/docs/interactions/application-commands#application-command-object-application-command-option-structure)
* @arg {Boolean} [command.defaultPermission] Whether the command is enabled by default when the app is added to a guild
* @arg {String?} [command.defaultMemberPermissions] The default member [permissions](https://discord.com/developers/docs/topics/permissions) represented as a bit set
coolcalcacol marked this conversation as resolved.
Show resolved Hide resolved
* @returns {Promise<Object>} Resolves with a commands object
*/
editGuildCommand(guildID, commandID, command) {
if(!commandID) {
throw new Error("You must provide an id of the command to edit.");
}
if(command.name !== undefined){
if(command.name !== undefined) {
if(command.type === 1 || command.type === undefined) {
command.name = command.name.toLowerCase();
if(!command.name.match(/^[\w-]{1,32}$/)) {
throw new Error("Slash Command names must match the regular expression \"^[\\w-]{1,32}$\"");
}
}
}
command.default_permission = command.defaultPermission;
command.default_member_permissions = command.defaultMemberPermissions;
return this.requestHandler.request("PATCH", Endpoints.GUILD_COMMAND(this.application.id, guildID, commandID), true, command);
}

Expand Down
5 changes: 3 additions & 2 deletions lib/Constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ module.exports.ApplicationCommandOptionTypes = {
};

module.exports.ApplicationCommandPermissionTypes = {
ROLE: 1,
USER: 2
ROLE: 1,
USER: 2,
CHANNEL: 3
};

module.exports.ApplicationCommandTypes = {
Expand Down
11 changes: 10 additions & 1 deletion lib/gateway/Shard.js
Original file line number Diff line number Diff line change
Expand Up @@ -2336,11 +2336,20 @@ class Shard extends EventEmitter {
/**
* Fired when an interaction is created
* @event Client#interactionCreate
* @prop {PingInteraction | CommandInteraction | ComponentInteraction | AutocompleteInteraction | UnknownInteraction} Interaction The Interaction that was created
* @prop {PingInteraction | CommandInteraction | ComponentInteraction | AutocompleteInteraction | UnknownInteraction} interaction The Interaction that was created
*/
this.emit("interactionCreate", Interaction.from(packet.d, this.client));
break;
}
case "APPLICATION_COMMAND_PERMISSIONS_UPDATE": {
/**
* Fired when an application command's permissions are updated
* @event Client#applicationCommandPermissionsUpdate
* @prop {GuildApplicationCommandPermissions} guildApplicationCommandPermissions The updated command permissions
bsian03 marked this conversation as resolved.
Show resolved Hide resolved
*/
this.emit("applicationCommandPermissionsUpdate", packet.d);
break;
}
default: {
/**
* Fired when the shard encounters an unknown packet
Expand Down
2 changes: 1 addition & 1 deletion lib/rest/Endpoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ module.exports.GUILD_BAN = (guildID, memberID)
module.exports.GUILD_BANS = (guildID) => `/guilds/${guildID}/bans`;
module.exports.GUILD_CHANNELS = (guildID) => `/guilds/${guildID}/channels`;
module.exports.GUILD_COMMAND = (applicationID, guildID, commandID) => `/applications/${applicationID}/guilds/${guildID}/commands/${commandID}`;
module.exports.GUILD_COMMAND_PERMISSIONS = (applicationID, guildID) => `/applications/${applicationID}/guilds/${guildID}/commands/permissions`;
module.exports.GUILD_COMMAND_PERMISSIONS = (applicationID, guildID) => `/applications/${applicationID}/guilds/${guildID}/commands/permissions`;
module.exports.GUILD_COMMANDS = (applicationID, guildID) => `/applications/${applicationID}/guilds/${guildID}/commands`;
module.exports.GUILD_DISCOVERY = (guildID) => `/guilds/${guildID}/discovery-metadata`;
module.exports.GUILD_DISCOVERY_CATEGORY = (guildID, categoryID) => `/guilds/${guildID}/discovery-categories/${categoryID}`;
Expand Down
4 changes: 2 additions & 2 deletions lib/structures/Guild.js
bsian03 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ class Guild extends Base {
* @arg {String} [command.description] The command description (Slash Commands Only)
* @arg {Array<Object>} [command.options] An array of [command options](https://discord.com/developers/docs/interactions/application-commands#application-command-object-application-command-option-structure)
* @arg {Number} [type=1] The type of application command, 1 for slash command, 2 for user, and 3 for message
* @arg {Boolean} [command.defaultPermission] Whether the command is enabled by default when the app is added to a guild
* @arg {String?} [command.defaultMemberPermissions] The default member [permissions](https://discord.com/developers/docs/topics/permissions) represented as a bit set
bsian03 marked this conversation as resolved.
Show resolved Hide resolved
* @returns {Promise<Object>} Resolves with a command object
*/
createCommand(command) {
Expand Down Expand Up @@ -624,7 +624,7 @@ class Guild extends Base {
* @arg {String} command.name The command name
* @arg {String} [command.description] The command description (Slash Commands Only)
* @arg {Array<Object>} [command.options] An array of [command options](https://discord.com/developers/docs/interactions/application-commands#application-command-object-application-command-option-structure)
* @arg {Boolean} [command.defaultPermission] Whether the command is enabled by default when the app is added to a guild
* @arg {String?} [command.defaultMemberPermissions] The default member [permissions](https://discord.com/developers/docs/topics/permissions) represented as a bit set
bsian03 marked this conversation as resolved.
Show resolved Hide resolved
* @returns {Promise<Object>} Resolves with a command object
*/
editCommand(commandID, commands) {
Expand Down