mirror of
https://github.com/discordjs/discord.js.git
synced 2026-06-03 09:30:08 +00:00
Cleanup Part 2: Electric Boogaloo (Reloaded) (#594)
* Cleanup Part 2: Electric Boogaloo (Reloaded) * Moar cleanup * Tweak NOT_A_PERMISSION error
This commit is contained in:
committed by
Amish Shah
parent
5a9c42061f
commit
0b908f5bce
@@ -1,24 +1,10 @@
|
||||
const Channel = require('./Channel');
|
||||
const PermissionOverwrites = require('./PermissionOverwrites');
|
||||
const Role = require('./Role');
|
||||
const PermissionOverwrites = require('./PermissionOverwrites');
|
||||
const EvaluatedPermissions = require('./EvaluatedPermissions');
|
||||
const Constants = require('../util/Constants');
|
||||
const Collection = require('../util/Collection');
|
||||
|
||||
function arraysEqual(a, b) {
|
||||
if (a === b) return true;
|
||||
if (a.length !== b.length) return false;
|
||||
|
||||
for (const itemInd in a) {
|
||||
const item = a[itemInd];
|
||||
const ind = b.indexOf(item);
|
||||
if (ind) {
|
||||
b.splice(ind, 1);
|
||||
}
|
||||
}
|
||||
|
||||
return b.length === 0;
|
||||
}
|
||||
const arraysEqual = require('../util/ArraysEqual');
|
||||
|
||||
/**
|
||||
* Represents a Guild Channel (i.e. Text Channels and Voice Channels)
|
||||
@@ -62,67 +48,57 @@ class GuildChannel extends Channel {
|
||||
/**
|
||||
* Checks if this channel has the same type, topic, position, name, overwrites and ID as another channel.
|
||||
* In most cases, a simple `channel.id === channel2.id` will do, and is much faster too.
|
||||
* @param {GuildChannel} channel the channel to compare this channel to
|
||||
* @param {GuildChannel} channel The channel to compare this channel to
|
||||
* @returns {boolean}
|
||||
*/
|
||||
equals(channel) {
|
||||
let base = channel &&
|
||||
let equal = channel &&
|
||||
this.type === channel.type &&
|
||||
this.topic === channel.topic &&
|
||||
this.position === channel.position &&
|
||||
this.name === channel.name &&
|
||||
this.id === channel.id;
|
||||
|
||||
if (base) {
|
||||
if (equal) {
|
||||
if (channel.permission_overwrites) {
|
||||
const thisIDSet = Array.from(this.permissionOverwrites.keys());
|
||||
const otherIDSet = channel.permission_overwrites.map(overwrite => overwrite.id);
|
||||
if (arraysEqual(thisIDSet, otherIDSet)) {
|
||||
base = true;
|
||||
} else {
|
||||
base = false;
|
||||
}
|
||||
equal = arraysEqual(thisIDSet, otherIDSet);
|
||||
} else {
|
||||
base = false;
|
||||
equal = false;
|
||||
}
|
||||
}
|
||||
|
||||
return base;
|
||||
return equal;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the overall set of permissions for a user in this channel, taking into account roles and permission
|
||||
* overwrites.
|
||||
* @param {GuildMemberResolvable} member the user that you want to obtain the overall permissions for
|
||||
* @param {GuildMemberResolvable} member The user that you want to obtain the overall permissions for
|
||||
* @returns {?EvaluatedPermissions}
|
||||
*/
|
||||
permissionsFor(member) {
|
||||
member = this.client.resolver.resolveGuildMember(this.guild, member);
|
||||
if (member) {
|
||||
if (this.guild.owner.id === member.id) {
|
||||
return new EvaluatedPermissions(member, Constants.ALL_PERMISSIONS);
|
||||
}
|
||||
if (this.guild.owner.id === member.id) return new EvaluatedPermissions(member, Constants.ALL_PERMISSIONS);
|
||||
|
||||
const roles = member.roles;
|
||||
let permissions = 0;
|
||||
const overwrites = this.overwritesFor(member, true);
|
||||
|
||||
for (const role of roles.values()) {
|
||||
permissions |= role.permissions;
|
||||
}
|
||||
|
||||
for (const role of roles.values()) permissions |= role.permissions;
|
||||
for (const overwrite of overwrites.role.concat(overwrites.member)) {
|
||||
permissions &= ~overwrite.denyData;
|
||||
permissions |= overwrite.allowData;
|
||||
}
|
||||
|
||||
const admin = Boolean(permissions & (Constants.PermissionFlags.ADMINISTRATOR));
|
||||
if (admin) {
|
||||
permissions = Constants.ALL_PERMISSIONS;
|
||||
}
|
||||
if (admin) permissions = Constants.ALL_PERMISSIONS;
|
||||
|
||||
return new EvaluatedPermissions(member, permissions);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -165,9 +141,9 @@ class GuildChannel extends Channel {
|
||||
|
||||
/**
|
||||
* Overwrites the permissions for a user or role in this channel.
|
||||
* @param {Role|UserResolvable} userOrRole the user or role to update
|
||||
* @param {PermissionOverwriteOptions} options the configuration for the update
|
||||
* @returns {Promise<null, Error>}
|
||||
* @param {Role|UserResolvable} userOrRole The user or role to update
|
||||
* @param {PermissionOverwriteOptions} options The configuration for the update
|
||||
* @returns {Promise}
|
||||
* @example
|
||||
* // overwrite permissions for a message author
|
||||
* message.channel.overwritePermissions(message.author, {
|
||||
@@ -187,9 +163,7 @@ class GuildChannel extends Channel {
|
||||
} else {
|
||||
userOrRole = this.client.resolver.resolveUser(userOrRole);
|
||||
payload.type = 'member';
|
||||
if (!userOrRole) {
|
||||
return Promise.reject('supplied parameter was neither a user or a role');
|
||||
}
|
||||
if (!userOrRole) return Promise.reject(new TypeError('supplied parameter was neither a user or a role'));
|
||||
}
|
||||
|
||||
payload.id = userOrRole.id;
|
||||
@@ -220,7 +194,7 @@ class GuildChannel extends Channel {
|
||||
|
||||
/**
|
||||
* Set a new name for the Guild Channel
|
||||
* @param {string} name the new name for the guild channel
|
||||
* @param {string} name The new name for the guild channel
|
||||
* @returns {Promise<GuildChannel>}
|
||||
* @example
|
||||
* // set a new channel name
|
||||
@@ -234,7 +208,7 @@ class GuildChannel extends Channel {
|
||||
|
||||
/**
|
||||
* Set a new position for the Guild Channel
|
||||
* @param {number} position the new position for the guild channel
|
||||
* @param {number} position The new position for the guild channel
|
||||
* @returns {Promise<GuildChannel>}
|
||||
* @example
|
||||
* // set a new channel position
|
||||
@@ -248,7 +222,7 @@ class GuildChannel extends Channel {
|
||||
|
||||
/**
|
||||
* Set a new topic for the Guild Channel
|
||||
* @param {string} topic the new topic for the guild channel
|
||||
* @param {string} topic The new topic for the guild channel
|
||||
* @returns {Promise<GuildChannel>}
|
||||
* @example
|
||||
* // set a new channel topic
|
||||
@@ -288,8 +262,8 @@ class GuildChannel extends Channel {
|
||||
|
||||
/**
|
||||
* Create an invite to this Guild Channel
|
||||
* @param {InviteOptions} [options={}] the options to provide when creating the invite
|
||||
* @returns {Promise<Invite, Error>}
|
||||
* @param {InviteOptions} [options={}] The options for the invite
|
||||
* @returns {Promise<Invite>}
|
||||
*/
|
||||
createInvite(options = {}) {
|
||||
return this.client.rest.methods.createChannelInvite(this, options);
|
||||
|
||||
Reference in New Issue
Block a user