Skip to content

Commit

Permalink
refactor: clean up error handling
Browse files Browse the repository at this point in the history
Co-authored-by: Antonio Román <kyradiscord@gmail.com>
Co-authored-by: Vlad Frangu <kingdgrizzle@gmail.com>
  • Loading branch information
3 people committed Jun 25, 2021
1 parent fed3595 commit 3fd430d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 21 deletions.
41 changes: 23 additions & 18 deletions src/managers/ApplicationCommandPermissionsManager.js
Expand Up @@ -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
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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
*/

/**
Expand Down Expand Up @@ -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 = [];
Expand Down Expand Up @@ -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 });
Expand Down Expand Up @@ -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') {
Expand All @@ -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);
}

Expand Down
4 changes: 2 additions & 2 deletions src/structures/ApplicationCommand.js
Expand Up @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion typings/index.d.ts
Expand Up @@ -2357,11 +2357,12 @@ declare module 'discord.js' {
}

export class ApplicationCommandPermissionsManager<BaseOptions, FetchSingleOptions, GuildType, CommandIDType> {
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<ApplicationCommandPermissions[]>;
Expand Down

0 comments on commit 3fd430d

Please sign in to comment.