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

refactor(PermissionOverwrites)!: cache-independent resolve #10528

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 6 additions & 0 deletions packages/discord.js/src/errors/ErrorCodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@
* @property {'BulkBanUsersOptionEmpty'} BulkBanUsersOptionEmpty

* @property {'PollAlreadyExpired'} PollAlreadyExpired

* @property {'PermissionOverwritesTypeMandatory'} PermissionOverwritesTypeMandatory
* @property {'PermissionOverwritesTypeMismatch'} PermissionOverwritesTypeMismatch
*/

const keys = [
Expand Down Expand Up @@ -337,6 +340,9 @@ const keys = [
'BulkBanUsersOptionEmpty',

'PollAlreadyExpired',

'PermissionOverwritesTypeMandatory',
'PermissionOverwritesTypeMismatch',
];

// JSDoc for IntelliSense purposes
Expand Down
5 changes: 5 additions & 0 deletions packages/discord.js/src/errors/Messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@ const Messages = {
[DjsErrorCodes.BulkBanUsersOptionEmpty]: 'Option "users" array or collection is empty',

[DjsErrorCodes.PollAlreadyExpired]: 'This poll has already expired.',

[DjsErrorCodes.PermissionOverwritesTypeMandatory]: '"overwrite.type" is mandatory if "overwrite.id" is a Snowflake',
[DjsErrorCodes.PermissionOverwritesTypeMismatch]: expected =>
`"overwrite.id" is a ${expected.toLowerCase()} object, ` +
`but "overwrite.type" is defined and not equal to OverwriteType.${expected}`,
};

module.exports = Messages;
35 changes: 23 additions & 12 deletions packages/discord.js/src/structures/PermissionOverwrites.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ class PermissionOverwrites extends Base {
* @property {GuildMemberResolvable|RoleResolvable} id Member or role this overwrite is for
* @property {PermissionResolvable} [allow] The permissions to allow
* @property {PermissionResolvable} [deny] The permissions to deny
* @property {OverwriteType} [type] The type of this OverwriteData
* @property {OverwriteType} [type] The type of this OverwriteData (mandatory if `id` is a Snowflake)
*/

/**
Expand All @@ -171,21 +171,32 @@ class PermissionOverwrites extends Base {
*/
static resolve(overwrite, guild) {
if (overwrite instanceof this) return overwrite.toJSON();
if (typeof overwrite.id === 'string' && overwrite.type in OverwriteType) {
return {
id: overwrite.id,
type: overwrite.type,
allow: PermissionsBitField.resolve(overwrite.allow ?? PermissionsBitField.DefaultBit).toString(),
deny: PermissionsBitField.resolve(overwrite.deny ?? PermissionsBitField.DefaultBit).toString(),
};

const id = guild.roles.resolveId(overwrite.id) ?? guild.client.users.resolveId(overwrite.id);
if (!id) {
throw new DiscordjsTypeError(ErrorCodes.InvalidType, 'overwrite.id', 'GuildMemberResolvable or RoleResolvable');
}

if (overwrite.type !== undefined && (typeof overwrite.type !== 'number' || !(overwrite.type in OverwriteType))) {
throw new DiscordjsTypeError(ErrorCodes.InvalidType, 'overwrite.type', 'OverwriteType', true);
}

const userOrRole = guild.roles.resolve(overwrite.id) ?? guild.client.users.resolve(overwrite.id);
if (!userOrRole) throw new DiscordjsTypeError(ErrorCodes.InvalidType, 'parameter', 'User nor a Role');
const type = userOrRole instanceof Role ? OverwriteType.Role : OverwriteType.Member;
let type;
if (typeof overwrite.id === 'string') {
if (overwrite.type !== undefined) {
type = overwrite.type;
} else {
throw new DiscordjsTypeError(ErrorCodes.PermissionOverwritesTypeMandatory);
}
} else {
type = overwrite.id instanceof Role ? OverwriteType.Role : OverwriteType.Member;
if (overwrite.type !== undefined && type !== overwrite.type) {
throw new DiscordjsTypeError(ErrorCodes.PermissionOverwritesTypeMismatch, OverwriteType[type]);
}
}

return {
id: userOrRole.id,
id,
type,
allow: PermissionsBitField.resolve(overwrite.allow ?? PermissionsBitField.DefaultBit).toString(),
deny: PermissionsBitField.resolve(overwrite.deny ?? PermissionsBitField.DefaultBit).toString(),
Expand Down
12 changes: 11 additions & 1 deletion packages/discord.js/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6590,13 +6590,23 @@ export interface MultipleShardSpawnOptions {
timeout?: number;
}

export interface OverwriteData {
export interface BaseOverwriteData {
allow?: PermissionResolvable;
deny?: PermissionResolvable;
id: GuildMemberResolvable | RoleResolvable;
type?: OverwriteType;
}

export interface OverwriteDataWithMandatoryType extends BaseOverwriteData {
type: OverwriteType;
}

export interface OverwriteDataWithOptionalType extends BaseOverwriteData {
id: Exclude<GuildMemberResolvable | RoleResolvable, Snowflake>;
}

export type OverwriteData = OverwriteDataWithMandatoryType | OverwriteDataWithOptionalType;

export type OverwriteResolvable = PermissionOverwrites | OverwriteData;

export type PermissionFlags = Record<keyof typeof PermissionFlagsBits, bigint>;
Expand Down