refactor: remove transformPermissions (#7303)

This commit is contained in:
Jan
2022-01-19 22:05:54 +01:00
committed by GitHub
parent 857bba4480
commit b4ed8fd3ed
2 changed files with 6 additions and 52 deletions

View File

@@ -96,18 +96,11 @@ class ApplicationCommandPermissionsManager extends BaseManager {
const { guildId, commandId } = this._validateOptions(guild, command); const { guildId, commandId } = this._validateOptions(guild, command);
if (commandId) { if (commandId) {
const data = await this.permissionsPath(guildId, commandId).get(); const data = await this.permissionsPath(guildId, commandId).get();
return data.permissions.map(perm => this.constructor.transformPermissions(perm)); return data.permissions;
} }
const data = await this.permissionsPath(guildId).get(); const data = await this.permissionsPath(guildId).get();
return data.reduce( return data.reduce((coll, perm) => coll.set(perm.id, perm.permissions), new Collection());
(coll, perm) =>
coll.set(
perm.id,
perm.permissions.map(p => this.constructor.transformPermissions(p)),
),
new Collection(),
);
} }
/** /**
@@ -165,35 +158,16 @@ class ApplicationCommandPermissionsManager extends BaseManager {
if (!Array.isArray(permissions)) { if (!Array.isArray(permissions)) {
throw new TypeError('INVALID_TYPE', 'permissions', 'Array of ApplicationCommandPermissionData', true); throw new TypeError('INVALID_TYPE', 'permissions', 'Array of ApplicationCommandPermissionData', true);
} }
const data = await this.permissionsPath(guildId, commandId).put({ const data = await this.permissionsPath(guildId, commandId).put({ data: { permissions } });
data: { permissions: permissions.map(perm => this.constructor.transformPermissions(perm)) }, return data.permissions;
});
return data.permissions.map(perm => this.constructor.transformPermissions(perm));
} }
if (!Array.isArray(fullPermissions)) { if (!Array.isArray(fullPermissions)) {
throw new TypeError('INVALID_TYPE', 'fullPermissions', 'Array of GuildApplicationCommandPermissionData', true); throw new TypeError('INVALID_TYPE', 'fullPermissions', 'Array of GuildApplicationCommandPermissionData', true);
} }
const APIPermissions = []; const data = await this.permissionsPath(guildId).put({ data: fullPermissions });
for (const perm of fullPermissions) { return data.reduce((coll, perm) => coll.set(perm.id, perm.permissions), new Collection());
if (!Array.isArray(perm.permissions)) throw new TypeError('INVALID_ELEMENT', 'Array', 'fullPermissions', perm);
APIPermissions.push({
id: perm.id,
permissions: perm.permissions.map(p => this.constructor.transformPermissions(p)),
});
}
const data = await this.permissionsPath(guildId).put({
data: APIPermissions,
});
return data.reduce(
(coll, perm) =>
coll.set(
perm.id,
perm.permissions.map(p => this.constructor.transformPermissions(p)),
),
new Collection(),
);
} }
/** /**
@@ -388,21 +362,6 @@ class ApplicationCommandPermissionsManager extends BaseManager {
} }
return { guildId, commandId }; return { guildId, commandId };
} }
/**
* Transforms an {@link ApplicationCommandPermissionData} object into something that can be used with the API.
* @param {ApplicationCommandPermissionData} permissions The permissions to transform
* @param {boolean} [received] Whether these permissions have been received from Discord
* @returns {APIApplicationCommandPermissions}
* @private
*/
static transformPermissions(permissions) {
return {
id: permissions.id,
permission: permissions.permission,
type: permissions.type,
};
}
} }
module.exports = ApplicationCommandPermissionsManager; module.exports = ApplicationCommandPermissionsManager;

View File

@@ -2795,11 +2795,6 @@ export class ApplicationCommandPermissionsManager<
}, },
): Promise<Collection<Snowflake, ApplicationCommandPermissions[]>>; ): Promise<Collection<Snowflake, ApplicationCommandPermissions[]>>;
private permissionsPath(guildId: Snowflake, commandId?: Snowflake): unknown; private permissionsPath(guildId: Snowflake, commandId?: Snowflake): unknown;
private static transformPermissions(
permissions: ApplicationCommandPermissionData,
received: true,
): Omit<APIApplicationCommandPermission, 'type'> & { type: keyof typeof ApplicationCommandPermissionType };
private static transformPermissions(permissions: ApplicationCommandPermissionData): APIApplicationCommandPermission;
} }
export class BaseGuildEmojiManager extends CachedManager<Snowflake, GuildEmoji, EmojiResolvable> { export class BaseGuildEmojiManager extends CachedManager<Snowflake, GuildEmoji, EmojiResolvable> {