From 633169a972f163fbed9677974f90d6a7bf2a3968 Mon Sep 17 00:00:00 2001 From: ckohen Date: Wed, 23 Jun 2021 17:05:36 -0700 Subject: [PATCH] refactor: clean up error handling MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Antonio Román Co-authored-by: Vlad Frangu --- .../ApplicationCommandPermissionsManager.js | 41 +++++++++++-------- src/structures/ApplicationCommand.js | 4 +- typings/index.d.ts | 3 +- 3 files changed, 27 insertions(+), 21 deletions(-) diff --git a/src/managers/ApplicationCommandPermissionsManager.js b/src/managers/ApplicationCommandPermissionsManager.js index a3036d0ada3b..bf10454efb31 100644 --- a/src/managers/ApplicationCommandPermissionsManager.js +++ b/src/managers/ApplicationCommandPermissionsManager.js @@ -14,21 +14,25 @@ class ApplicationCommandPermissionsManager { * @type {ApplicationCommandManager|ApplicationCommand} */ this.manager = manager; + /** * The guild that this manager acts on * @type {?Guild} */ this.guild = manager.guild ?? null; + /** * The id of the guild that this manager acts on * @type {?Snowflake} */ this.guildID = manager.guildID ?? manager.guild?.id ?? null; + /** * The id of the command this manager acts on * @type {?Snowflake} */ this.commandID = manager.id ?? null; + /** * The client that instantiated this Manager * @name ApplicationCommandPermissionsManager#client @@ -46,8 +50,7 @@ class ApplicationCommandPermissionsManager { * @private */ permissionsPath(guildID, commandID) { - let path = this.client.api.applications(this.client.application.id).guilds(guildID); - return (commandID ? path.commands(commandID) : path.commands).permissions; + return this.client.api.applications(this.client.application.id).guilds(guildID).commands(commandID).permissions; } /** @@ -113,8 +116,8 @@ class ApplicationCommandPermissionsManager { /** * Data used for overwriting the permissions for all application commands in a guild. * @typedef {Object} GuildApplicationCommandPermissionData - * @prop {Snowflake} id The ID of the command - * @prop {ApplicationCommandPermissionData[]} permissions The permissions for this command + * @property {Snowflake} id The ID of the command + * @property {ApplicationCommandPermissionData[]} permissions The permissions for this command */ /** @@ -273,13 +276,6 @@ class ApplicationCommandPermissionsManager { const { guildID, commandID } = this._validateOptions(guild, command); if (!commandID) throw new TypeError('INVALID_TYPE', 'command', 'ApplicationCommandResolvable'); - let existing = []; - try { - existing = await this.fetch({ guild: guildID, command: commandID }); - } catch (error) { - if (error.code !== APIErrors.UNKNOWN_APPLICATION_COMMAND_PERMISSIONS) throw error; - } - if (!users && !roles) throw new TypeError('INVALID_TYPE', 'users OR roles', 'Array or Resolvable', true); let resolvedIDs = []; @@ -320,6 +316,14 @@ class ApplicationCommandPermissionsManager { resolvedIDs.push(roleID); } } + + let existing = []; + try { + existing = await this.fetch({ guild: guildID, command: commandID }); + } catch (error) { + if (error.code !== APIErrors.UNKNOWN_APPLICATION_COMMAND_PERMISSIONS) throw error; + } + const permissions = existing.filter(perm => !resolvedIDs.includes(perm.id)); return this.set({ guild: guildID, command: commandID, permissions }); @@ -347,13 +351,6 @@ class ApplicationCommandPermissionsManager { const { guildID, commandID } = this._validateOptions(guild, command); if (!commandID) throw new TypeError('INVALID_TYPE', 'command', 'ApplicationCommandResolvable'); - let existing = []; - try { - existing = await this.fetch({ guild: guildID, command: commandID }); - } catch (error) { - if (error.code !== APIErrors.UNKNOWN_APPLICATION_COMMAND_PERMISSIONS) throw error; - } - if (!permissionID) throw new TypeError('INVALID_TYPE', 'permissionsID', 'UserResolvable or RoleResolvable'); let resolvedID = permissionID; if (typeof permissionID !== 'string') { @@ -366,6 +363,14 @@ class ApplicationCommandPermissionsManager { throw new TypeError('INVALID_TYPE', 'permissionID', 'UserResolvable or RoleResolvable'); } } + + let existing = []; + try { + existing = await this.fetch({ guild: guildID, command: commandID }); + } catch (error) { + if (error.code !== APIErrors.UNKNOWN_APPLICATION_COMMAND_PERMISSIONS) throw error; + } + return existing.some(perm => perm.id === resolvedID); } diff --git a/src/structures/ApplicationCommand.js b/src/structures/ApplicationCommand.js index 268488bbf33e..5ffe32de6a23 100644 --- a/src/structures/ApplicationCommand.js +++ b/src/structures/ApplicationCommand.js @@ -127,7 +127,7 @@ class ApplicationCommand extends Base { * .catch(console.error); */ edit(data) { - return this.manager.edit(this, data, this.guildID ?? undefined); + return this.manager.edit(this, data, this.guildID); } /** @@ -140,7 +140,7 @@ class ApplicationCommand extends Base { * .catch(console.error); */ delete() { - return this.manager.delete(this, this.guildID ?? undefined); + return this.manager.delete(this, this.guildID); } /** diff --git a/typings/index.d.ts b/typings/index.d.ts index ae11675f0762..4e6a54781170 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -2324,11 +2324,12 @@ declare module 'discord.js' { } export class ApplicationCommandPermissionsManager { - constructor(public manager: ApplicationCommandManager | GuildApplicationCommandManager | ApplicationCommand); + constructor(manager: ApplicationCommandManager | GuildApplicationCommandManager | ApplicationCommand); public client: Client; public commandID: CommandIDType; public guild: GuildType; public guildID: Snowflake | null; + public manager: ApplicationCommandManager | GuildApplicationCommandManager | ApplicationCommand; public add( options: BaseOptions & { permissions: ApplicationCommandPermissionData[] }, ): Promise;