From a11df1edd551241b74fd75e3a0804e27fac7414a Mon Sep 17 00:00:00 2001 From: Suneet Tipirneni Date: Thu, 9 Sep 2021 14:46:58 -0400 Subject: [PATCH 1/3] feat: add initial api command support --- src/managers/ApplicationCommandManager.js | 14 +++++++------ src/structures/ApplicationCommand.js | 25 +++++++++++++++++++++++ typings/index.d.ts | 20 ++++++++++++------ 3 files changed, 47 insertions(+), 12 deletions(-) diff --git a/src/managers/ApplicationCommandManager.js b/src/managers/ApplicationCommandManager.js index dbc83dcb2c0b..da387c197c7e 100644 --- a/src/managers/ApplicationCommandManager.js +++ b/src/managers/ApplicationCommandManager.js @@ -100,7 +100,7 @@ class ApplicationCommandManager extends CachedManager { /** * Creates an application command. - * @param {ApplicationCommandData} command The command + * @param {ApplicationCommandData|RESTPostAPIApplicationCommandsJSONBody} command The command * @param {Snowflake} [guildId] The guild's id to create this command in, * ignored when using a {@link GuildApplicationCommandManager} * @returns {Promise} @@ -115,14 +115,14 @@ class ApplicationCommandManager extends CachedManager { */ async create(command, guildId) { const data = await this.commandPath({ guildId }).post({ - data: this.constructor.transformCommand(command), + data: ApplicationCommand.isAPICommandData(command) ? command : this.constructor.transformCommand(command), }); return this._add(data, !guildId, guildId); } /** * Sets all the commands for this application or guild. - * @param {ApplicationCommandData[]} commands The commands + * @param {ApplicationCommandData[]|RESTPostAPIApplicationCommandsJSONBody[]} commands The commands * @param {Snowflake} [guildId] The guild's id to create the commands in, * ignored when using a {@link GuildApplicationCommandManager} * @returns {Promise>} @@ -144,7 +144,7 @@ class ApplicationCommandManager extends CachedManager { */ async set(commands, guildId) { const data = await this.commandPath({ guildId }).put({ - data: commands.map(c => this.constructor.transformCommand(c)), + data: commands.map(c => (ApplicationCommand.isAPICommandData(c) ? c : this.constructor.transformCommand(c))), }); return data.reduce( (coll, command) => coll.set(command.id, this._add(command, !guildId, guildId)), @@ -155,7 +155,7 @@ class ApplicationCommandManager extends CachedManager { /** * Edits an application command. * @param {ApplicationCommandResolvable} command The command to edit - * @param {ApplicationCommandData} data The data to update the command with + * @param {ApplicationCommandData|RESTPostAPIApplicationCommandsJSONBody} data The data to update the command with * @param {Snowflake} [guildId] The guild's id where the command registered, * ignored when using a {@link GuildApplicationCommandManager} * @returns {Promise} @@ -171,7 +171,9 @@ class ApplicationCommandManager extends CachedManager { const id = this.resolveId(command); if (!id) throw new TypeError('INVALID_TYPE', 'command', 'ApplicationCommandResolvable'); - const patched = await this.commandPath({ id, guildId }).patch({ data: this.constructor.transformCommand(data) }); + const patched = await this.commandPath({ id, guildId }).patch({ + data: ApplicationCommand.isAPICommandData(data) ? data : this.constructor.transformCommand(data), + }); return this._add(patched, !guildId, guildId); } diff --git a/src/structures/ApplicationCommand.js b/src/structures/ApplicationCommand.js index d1e4de84247a..7530ae1eae80 100644 --- a/src/structures/ApplicationCommand.js +++ b/src/structures/ApplicationCommand.js @@ -298,6 +298,31 @@ class ApplicationCommand extends Base { options: option.options?.map(o => this.transformOption(o, received)), }; } + + /** + * Transforms an {@link ApplicationCommandData} object into something that can be used with the API. + * @param {ApplicationCommandData} command The command to transform + * @returns {APIApplicationCommand}] + * @private + */ + static transformCommand(command) { + return { + name: command.name, + description: command.description, + type: typeof command.type === 'number' ? command.type : ApplicationCommandTypes[command.type], + options: command.options?.map(o => ApplicationCommand.transformOption(o)), + default_permission: command.defaultPermission, + }; + } + + /** + * Whether or not the given object is api command data or not. + * @param {Object} command The command object to check. + * @returns {boolean} True if the object is api command data, false otherwise. + */ + static isAPICommandData(command) { + return 'default_permission' in command || 'guild_id' in command; + } } module.exports = ApplicationCommand; diff --git a/typings/index.d.ts b/typings/index.d.ts index 7ab20d8be139..b45ef0987f29 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -44,6 +44,7 @@ import { APIUser, GatewayVoiceServerUpdateDispatchData, GatewayVoiceStateUpdateDispatchData, + RESTPostAPIApplicationCommandsJSONBody, Snowflake, } from 'discord-api-types/v9'; import { EventEmitter } from 'events'; @@ -235,6 +236,8 @@ export class ApplicationCommand extends Base { enforceOptionorder?: boolean, ): boolean; private static transformOption(option: ApplicationCommandOptionData, received?: boolean): unknown; + private static transformCommand(command: ApplicationCommandData): RESTPostAPIApplicationCommandsJSONBody; + private static isAPICommandData(command: object): command is RESTPostAPIApplicationCommandsJSONBody; } export type ApplicationResolvable = Application | Activity | Snowflake; @@ -2383,6 +2386,8 @@ export abstract class CachedManager extends DataManager, PermissionsOptionsExtras = { guild: GuildResolvable }, @@ -2397,13 +2402,16 @@ export class ApplicationCommandManager< null >; private commandPath({ id, guildId }: { id?: Snowflake; guildId?: Snowflake }): unknown; - public create(command: ApplicationCommandData): Promise; - public create(command: ApplicationCommandData, guildId: Snowflake): Promise; + public create(command: ApplicationCommandDataResolvable): Promise; + public create(command: ApplicationCommandDataResolvable, guildId: Snowflake): Promise; public delete(command: ApplicationCommandResolvable, guildId?: Snowflake): Promise; - public edit(command: ApplicationCommandResolvable, data: ApplicationCommandData): Promise; public edit( command: ApplicationCommandResolvable, - data: ApplicationCommandData, + data: ApplicationCommandDataResolvable, + ): Promise; + public edit( + command: ApplicationCommandResolvable, + data: ApplicationCommandDataResolvable, guildId: Snowflake, ): Promise; public fetch( @@ -2415,9 +2423,9 @@ export class ApplicationCommandManager< id?: Snowflake, options?: FetchApplicationCommandOptions, ): Promise>; - public set(commands: ApplicationCommandData[]): Promise>; + public set(commands: ApplicationCommandDataResolvable[]): Promise>; public set( - commands: ApplicationCommandData[], + commands: ApplicationCommandDataResolvable[], guildId: Snowflake, ): Promise>; private static transformCommand( From ea3b6a21eea3ee2601efb5a0a0df9ed669f971d8 Mon Sep 17 00:00:00 2001 From: Suneet Tipirneni Date: Fri, 10 Sep 2021 13:30:37 -0400 Subject: [PATCH 2/3] fix: guild typings --- typings/index.d.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/typings/index.d.ts b/typings/index.d.ts index b45ef0987f29..f04e4ef7baac 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -2493,12 +2493,15 @@ export class ChannelManager extends CachedManager { public constructor(guild: Guild, iterable?: Iterable); public guild: Guild; - public create(command: ApplicationCommandData): Promise; + public create(command: ApplicationCommandDataResolvable): Promise; public delete(command: ApplicationCommandResolvable): Promise; - public edit(command: ApplicationCommandResolvable, data: ApplicationCommandData): Promise; + public edit( + command: ApplicationCommandResolvable, + data: ApplicationCommandDataResolvable, + ): Promise; public fetch(id: Snowflake, options?: BaseFetchOptions): Promise; public fetch(id?: undefined, options?: BaseFetchOptions): Promise>; - public set(commands: ApplicationCommandData[]): Promise>; + public set(commands: ApplicationCommandDataResolvable[]): Promise>; } export class GuildChannelManager extends CachedManager< From bd9e5996848d230c4f0c0778e5ed3ff0215ff8cb Mon Sep 17 00:00:00 2001 From: suneettipirneni Date: Sat, 11 Sep 2021 16:21:47 -0400 Subject: [PATCH 3/3] fix: make requested changes --- src/managers/ApplicationCommandManager.js | 16 +++++++-------- src/structures/ApplicationCommand.js | 25 ----------------------- 2 files changed, 8 insertions(+), 33 deletions(-) diff --git a/src/managers/ApplicationCommandManager.js b/src/managers/ApplicationCommandManager.js index da387c197c7e..c053382f3a9a 100644 --- a/src/managers/ApplicationCommandManager.js +++ b/src/managers/ApplicationCommandManager.js @@ -100,7 +100,7 @@ class ApplicationCommandManager extends CachedManager { /** * Creates an application command. - * @param {ApplicationCommandData|RESTPostAPIApplicationCommandsJSONBody} command The command + * @param {ApplicationCommandData|APIApplicationCommand} command The command * @param {Snowflake} [guildId] The guild's id to create this command in, * ignored when using a {@link GuildApplicationCommandManager} * @returns {Promise} @@ -115,14 +115,14 @@ class ApplicationCommandManager extends CachedManager { */ async create(command, guildId) { const data = await this.commandPath({ guildId }).post({ - data: ApplicationCommand.isAPICommandData(command) ? command : this.constructor.transformCommand(command), + data: this.constructor.transformCommand(command), }); return this._add(data, !guildId, guildId); } /** * Sets all the commands for this application or guild. - * @param {ApplicationCommandData[]|RESTPostAPIApplicationCommandsJSONBody[]} commands The commands + * @param {ApplicationCommandData[]|APIApplicationCommand[]} commands The commands * @param {Snowflake} [guildId] The guild's id to create the commands in, * ignored when using a {@link GuildApplicationCommandManager} * @returns {Promise>} @@ -144,7 +144,7 @@ class ApplicationCommandManager extends CachedManager { */ async set(commands, guildId) { const data = await this.commandPath({ guildId }).put({ - data: commands.map(c => (ApplicationCommand.isAPICommandData(c) ? c : this.constructor.transformCommand(c))), + data: commands.map(c => this.constructor.transformCommand(c)), }); return data.reduce( (coll, command) => coll.set(command.id, this._add(command, !guildId, guildId)), @@ -155,7 +155,7 @@ class ApplicationCommandManager extends CachedManager { /** * Edits an application command. * @param {ApplicationCommandResolvable} command The command to edit - * @param {ApplicationCommandData|RESTPostAPIApplicationCommandsJSONBody} data The data to update the command with + * @param {ApplicationCommandData|APIApplicationCommand} data The data to update the command with * @param {Snowflake} [guildId] The guild's id where the command registered, * ignored when using a {@link GuildApplicationCommandManager} * @returns {Promise} @@ -172,7 +172,7 @@ class ApplicationCommandManager extends CachedManager { if (!id) throw new TypeError('INVALID_TYPE', 'command', 'ApplicationCommandResolvable'); const patched = await this.commandPath({ id, guildId }).patch({ - data: ApplicationCommand.isAPICommandData(data) ? data : this.constructor.transformCommand(data), + data: this.constructor.transformCommand(data), }); return this._add(patched, !guildId, guildId); } @@ -202,7 +202,7 @@ class ApplicationCommandManager extends CachedManager { /** * Transforms an {@link ApplicationCommandData} object into something that can be used with the API. - * @param {ApplicationCommandData} command The command to transform + * @param {ApplicationCommandData|APIApplicationCommand} command The command to transform * @returns {APIApplicationCommand} * @private */ @@ -212,7 +212,7 @@ class ApplicationCommandManager extends CachedManager { description: command.description, type: typeof command.type === 'number' ? command.type : ApplicationCommandTypes[command.type], options: command.options?.map(o => ApplicationCommand.transformOption(o)), - default_permission: command.defaultPermission, + default_permission: command.defaultPermission ?? command.default_permission, }; } } diff --git a/src/structures/ApplicationCommand.js b/src/structures/ApplicationCommand.js index 7530ae1eae80..d1e4de84247a 100644 --- a/src/structures/ApplicationCommand.js +++ b/src/structures/ApplicationCommand.js @@ -298,31 +298,6 @@ class ApplicationCommand extends Base { options: option.options?.map(o => this.transformOption(o, received)), }; } - - /** - * Transforms an {@link ApplicationCommandData} object into something that can be used with the API. - * @param {ApplicationCommandData} command The command to transform - * @returns {APIApplicationCommand}] - * @private - */ - static transformCommand(command) { - return { - name: command.name, - description: command.description, - type: typeof command.type === 'number' ? command.type : ApplicationCommandTypes[command.type], - options: command.options?.map(o => ApplicationCommand.transformOption(o)), - default_permission: command.defaultPermission, - }; - } - - /** - * Whether or not the given object is api command data or not. - * @param {Object} command The command object to check. - * @returns {boolean} True if the object is api command data, false otherwise. - */ - static isAPICommandData(command) { - return 'default_permission' in command || 'guild_id' in command; - } } module.exports = ApplicationCommand;