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

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

Merged
merged 10 commits into from May 11, 2021
33 changes: 21 additions & 12 deletions src/structures/GuildChannel.js
Expand Up @@ -218,6 +218,7 @@ class GuildChannel extends Channel {
* @param {RoleResolvable|UserResolvable} userOrRole The user or role to update
* @param {PermissionOverwriteOptions} options The options for the update
* @param {string} [reason] Reason for creating/editing this overwrite
* @param {string} [type] The type of overwrite: `member` or `role`
* @returns {Promise<GuildChannel>}
* @example
* // Update or Create permission overwrites for a message author
Expand All @@ -227,15 +228,19 @@ class GuildChannel extends Channel {
* .then(channel => console.log(channel.permissionOverwrites.get(message.author.id)))
* .catch(console.error);
*/
async updateOverwrite(userOrRole, options, reason) {
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'));
async updateOverwrite(userOrRole, options, reason, type) {
iShibi marked this conversation as resolved.
Show resolved Hide resolved
let userOrRoleID = this.guild.roles.resolveID(userOrRole) || this.client.users.resolveID(userOrRole);
if (!type) {
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'));
userOrRoleID = userOrRole.id;
}
iShibi marked this conversation as resolved.
Show resolved Hide resolved

const existing = this.permissionOverwrites.get(userOrRole.id);
const existing = this.permissionOverwrites.get(userOrRoleID);
if (existing) {
await existing.update(options, reason);
} else {
await this.createOverwrite(userOrRole, options, reason);
await this.createOverwrite(userOrRole, options, reason, type);
}
return this;
}
Expand All @@ -245,6 +250,7 @@ class GuildChannel extends Channel {
* @param {RoleResolvable|UserResolvable} userOrRole The user or role to update
* @param {PermissionOverwriteOptions} options The options for the update
* @param {string} [reason] Reason for creating/editing this overwrite
* @param {string} [type] The type of overwrite: `member` or `role`
* @returns {Promise<GuildChannel>}
* @example
* // Create or Replace permission overwrites for a message author
Expand All @@ -254,19 +260,22 @@ class GuildChannel extends Channel {
* .then(channel => console.log(channel.permissionOverwrites.get(message.author.id)))
* .catch(console.error);
*/
createOverwrite(userOrRole, options, reason) {
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'));

const type = userOrRole instanceof Role ? OverwriteTypes.role : OverwriteTypes.member;
createOverwrite(userOrRole, options, reason, type) {
let userOrRoleID = this.guild.roles.resolveID(userOrRole) || this.client.users.resolveID(userOrRole);
if (!type) {
iShibi marked this conversation as resolved.
Show resolved Hide resolved
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'));
userOrRoleID = userOrRole.id;
type = userOrRole instanceof Role ? OverwriteTypes.role : OverwriteTypes.member;
}
const { allow, deny } = PermissionOverwrites.resolveOverwriteOptions(options);

return this.client.api
.channels(this.id)
.permissions(userOrRole.id)
.permissions(userOrRoleID)
.put({
data: {
id: userOrRole.id,
id: userOrRoleID,
type,
allow,
deny,
Expand Down