feat(GuildChannel): make createOverwrite and updateOverwrite not dependent on cache (#5489)

Co-authored-by: SpaceEEC <spaceeec@yahoo.com>
Co-authored-by: Antonio Román <kyradiscord@gmail.com>
This commit is contained in:
Shubham Parihar
2021-05-12 01:53:09 +05:30
committed by GitHub
parent 74e97ef91b
commit 58763b0e91
2 changed files with 33 additions and 17 deletions

View File

@@ -214,11 +214,19 @@ class GuildChannel extends Channel {
return this; return this;
} }
/**
* Extra information about the overwrite
* @typedef {Object} GuildChannelOverwriteOptions
* @property {string} [reason] Reason for creating/editing this overwrite
* @property {number} [type] The type of overwrite, either `0` for a role or `1` for a member. Use this to bypass
* automatic resolution of type that results in an error for uncached structure
*/
/** /**
* Updates permission overwrites for a user or role in this channel, or creates an entry if not already present. * Updates permission overwrites for a user or role in this channel, or creates an entry if not already present.
* @param {RoleResolvable|UserResolvable} userOrRole The user or role to update * @param {RoleResolvable|UserResolvable} userOrRole The user or role to update
* @param {PermissionOverwriteOptions} options The options for the update * @param {PermissionOverwriteOptions} options The options for the update
* @param {string} [reason] Reason for creating/editing this overwrite * @param {GuildChannelOverwriteOptions} [overwriteOptions] The extra information for the update
* @returns {Promise<GuildChannel>} * @returns {Promise<GuildChannel>}
* @example * @example
* // Update or Create permission overwrites for a message author * // Update or Create permission overwrites for a message author
@@ -228,15 +236,14 @@ class GuildChannel extends Channel {
* .then(channel => console.log(channel.permissionOverwrites.get(message.author.id))) * .then(channel => console.log(channel.permissionOverwrites.get(message.author.id)))
* .catch(console.error); * .catch(console.error);
*/ */
async updateOverwrite(userOrRole, options, reason) { async updateOverwrite(userOrRole, options, overwriteOptions = {}) {
userOrRole = this.guild.roles.resolve(userOrRole) || this.client.users.resolve(userOrRole); const userOrRoleID = this.guild.roles.resolveID(userOrRole) || this.client.users.resolveID(userOrRole);
if (!userOrRole) return Promise.reject(new TypeError('INVALID_TYPE', 'parameter', 'User nor a Role')); const { reason } = overwriteOptions;
const existing = this.permissionOverwrites.get(userOrRoleID);
const existing = this.permissionOverwrites.get(userOrRole.id);
if (existing) { if (existing) {
await existing.update(options, reason); await existing.update(options, reason);
} else { } else {
await this.createOverwrite(userOrRole, options, reason); await this.createOverwrite(userOrRole, options, overwriteOptions);
} }
return this; return this;
} }
@@ -245,7 +252,7 @@ class GuildChannel extends Channel {
* Creates permission overwrites for a user or role in this channel, or replaces them if already present. * Creates permission overwrites for a user or role in this channel, or replaces them if already present.
* @param {RoleResolvable|UserResolvable} userOrRole The user or role to update * @param {RoleResolvable|UserResolvable} userOrRole The user or role to update
* @param {PermissionOverwriteOptions} options The options for the update * @param {PermissionOverwriteOptions} options The options for the update
* @param {string} [reason] Reason for creating/editing this overwrite * @param {GuildChannelOverwriteOptions} [overwriteOptions] The extra information for the update
* @returns {Promise<GuildChannel>} * @returns {Promise<GuildChannel>}
* @example * @example
* // Create or Replace permission overwrites for a message author * // Create or Replace permission overwrites for a message author
@@ -255,19 +262,23 @@ class GuildChannel extends Channel {
* .then(channel => console.log(channel.permissionOverwrites.get(message.author.id))) * .then(channel => console.log(channel.permissionOverwrites.get(message.author.id)))
* .catch(console.error); * .catch(console.error);
*/ */
createOverwrite(userOrRole, options, reason) { createOverwrite(userOrRole, options, overwriteOptions = {}) {
let userOrRoleID = this.guild.roles.resolveID(userOrRole) || this.client.users.resolveID(userOrRole);
let { type, reason } = overwriteOptions;
if (typeof type !== 'number') {
userOrRole = this.guild.roles.resolve(userOrRole) || this.client.users.resolve(userOrRole); userOrRole = this.guild.roles.resolve(userOrRole) || this.client.users.resolve(userOrRole);
if (!userOrRole) return Promise.reject(new TypeError('INVALID_TYPE', 'parameter', 'User nor a Role')); if (!userOrRole) return Promise.reject(new TypeError('INVALID_TYPE', 'parameter', 'User nor a Role'));
userOrRoleID = userOrRole.id;
const type = userOrRole instanceof Role ? OverwriteTypes.role : OverwriteTypes.member; type = userOrRole instanceof Role ? OverwriteTypes.role : OverwriteTypes.member;
}
const { allow, deny } = PermissionOverwrites.resolveOverwriteOptions(options); const { allow, deny } = PermissionOverwrites.resolveOverwriteOptions(options);
return this.client.api return this.client.api
.channels(this.id) .channels(this.id)
.permissions(userOrRole.id) .permissions(userOrRoleID)
.put({ .put({
data: { data: {
id: userOrRole.id, id: userOrRoleID,
type, type,
allow, allow,
deny, deny,

9
typings/index.d.ts vendored
View File

@@ -876,7 +876,7 @@ declare module 'discord.js' {
public createOverwrite( public createOverwrite(
userOrRole: RoleResolvable | UserResolvable, userOrRole: RoleResolvable | UserResolvable,
options: PermissionOverwriteOption, options: PermissionOverwriteOption,
reason?: string, overwriteOptions?: GuildChannelOverwriteOptions,
): Promise<this>; ): Promise<this>;
public edit(data: ChannelData, reason?: string): Promise<this>; public edit(data: ChannelData, reason?: string): Promise<this>;
public equals(channel: GuildChannel): boolean; public equals(channel: GuildChannel): boolean;
@@ -898,7 +898,7 @@ declare module 'discord.js' {
public updateOverwrite( public updateOverwrite(
userOrRole: RoleResolvable | UserResolvable, userOrRole: RoleResolvable | UserResolvable,
options: PermissionOverwriteOption, options: PermissionOverwriteOption,
reason?: string, overwriteOptions?: GuildChannelOverwriteOptions,
): Promise<this>; ): Promise<this>;
public isText(): this is TextChannel | NewsChannel; public isText(): this is TextChannel | NewsChannel;
} }
@@ -2797,6 +2797,11 @@ declare module 'discord.js' {
cache: boolean; cache: boolean;
} }
interface GuildChannelOverwriteOptions {
reason?: string;
type?: number;
}
interface FetchMemberOptions { interface FetchMemberOptions {
user: UserResolvable; user: UserResolvable;
cache?: boolean; cache?: boolean;