mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-12 09:33:32 +01: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
@@ -15,22 +15,18 @@ class Role {
|
||||
* @type {Client}
|
||||
*/
|
||||
this.client = guild.client;
|
||||
if (data) {
|
||||
this.setup(data);
|
||||
}
|
||||
if (data) this.setup(data);
|
||||
}
|
||||
|
||||
equals(role) {
|
||||
return (
|
||||
role &&
|
||||
return role &&
|
||||
this.id === role.id &&
|
||||
this.name === role.name &&
|
||||
this.color === role.color &&
|
||||
this.hoist === role.hoist &&
|
||||
this.position === role.position &&
|
||||
this.permissions === role.permissions &&
|
||||
this.managed === role.managed
|
||||
);
|
||||
this.managed === role.managed;
|
||||
}
|
||||
|
||||
setup(data) {
|
||||
@@ -73,7 +69,7 @@ class Role {
|
||||
|
||||
/**
|
||||
* Deletes the role
|
||||
* @returns {Promise<Role, Error>}
|
||||
* @returns {Promise<Role>}
|
||||
* @example
|
||||
* // delete a role
|
||||
* role.delete()
|
||||
@@ -86,8 +82,8 @@ class Role {
|
||||
|
||||
/**
|
||||
* Edits the role
|
||||
* @param {RoleData} data the new data for the role
|
||||
* @returns {Promise<Role, Error>}
|
||||
* @param {RoleData} data The new data for the role
|
||||
* @returns {Promise<Role>}
|
||||
* @example
|
||||
* // edit a role
|
||||
* role.edit({name: 'new role'})
|
||||
@@ -100,8 +96,8 @@ class Role {
|
||||
|
||||
/**
|
||||
* Set a new name for the role
|
||||
* @param {string} name the new name of the role
|
||||
* @returns {Promise<Role, Error>}
|
||||
* @param {string} name The new name of the role
|
||||
* @returns {Promise<Role>}
|
||||
* @example
|
||||
* // set the name of the role
|
||||
* role.setName('new role')
|
||||
@@ -114,8 +110,8 @@ class Role {
|
||||
|
||||
/**
|
||||
* Set a new color for the role
|
||||
* @param {number|string} color the new color for the role, either a hex string or a base 10 number
|
||||
* @returns {Promise<Role, Error>}
|
||||
* @param {number|string} color The new color for the role, either a hex string or a base 10 number
|
||||
* @returns {Promise<Role>}
|
||||
* @example
|
||||
* // set the color of a role
|
||||
* role.setColor('#FF0000')
|
||||
@@ -128,8 +124,8 @@ class Role {
|
||||
|
||||
/**
|
||||
* Set whether or not the role should be hoisted
|
||||
* @param {boolean} hoist whether or not to hoist the role
|
||||
* @returns {Promise<Role, Error>}
|
||||
* @param {boolean} hoist Whether or not to hoist the role
|
||||
* @returns {Promise<Role>}
|
||||
* @example
|
||||
* // set the hoist of the role
|
||||
* role.setHoist(true)
|
||||
@@ -142,8 +138,8 @@ class Role {
|
||||
|
||||
/**
|
||||
* Set the position of the role
|
||||
* @param {number} position the position of the role
|
||||
* @returns {Promise<Role, Error>}
|
||||
* @param {number} position The position of the role
|
||||
* @returns {Promise<Role>}
|
||||
* @example
|
||||
* // set the position of the role
|
||||
* role.setPosition(1)
|
||||
@@ -156,8 +152,8 @@ class Role {
|
||||
|
||||
/**
|
||||
* Set the permissions of the role
|
||||
* @param {Array<string>} permissions the permissions of the role
|
||||
* @returns {Promise<Role, Error>}
|
||||
* @param {string[]} permissions The permissions of the role
|
||||
* @returns {Promise<Role>}
|
||||
* @example
|
||||
* // set the permissions of the role
|
||||
* role.setPermissions(['KICK_MEMBERS', 'BAN_MEMBERS'])
|
||||
@@ -180,14 +176,13 @@ class Role {
|
||||
for (const permissionName in Constants.PermissionFlags) {
|
||||
serializedPermissions[permissionName] = this.hasPermission(permissionName);
|
||||
}
|
||||
|
||||
return serializedPermissions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether or not the role includes the given permission
|
||||
* @param {string} permission the name of the permission to test
|
||||
* @param {boolean} [explicit=false] whether or not the inclusion of the permission is explicit
|
||||
* @param {PermissionResolvable} permission The name of the permission to test
|
||||
* @param {boolean} [explicit=false] Whether to require the role to explicitly have the exact permission
|
||||
* @returns {boolean}
|
||||
* @example
|
||||
* // see if a role can ban a member
|
||||
@@ -198,20 +193,8 @@ class Role {
|
||||
* }
|
||||
*/
|
||||
hasPermission(permission, explicit = false) {
|
||||
if (typeof permission === 'string') {
|
||||
permission = Constants.PermissionFlags[permission];
|
||||
}
|
||||
|
||||
if (!permission) {
|
||||
throw Constants.Errors.NOT_A_PERMISSION;
|
||||
}
|
||||
|
||||
if (!explicit) {
|
||||
if ((this.permissions & Constants.PermissionFlags.ADMINISTRATOR) > 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
permission = this.client.resolver.resolvePermission(permission);
|
||||
if (!explicit && (this.permissions & Constants.PermissionFlags.ADMINISTRATOR) > 0) return true;
|
||||
return (this.permissions & permission) > 0;
|
||||
}
|
||||
|
||||
@@ -230,9 +213,7 @@ class Role {
|
||||
*/
|
||||
get hexColor() {
|
||||
let col = this.color.toString(16);
|
||||
while (col.length < 6) {
|
||||
col = `0${col}`;
|
||||
}
|
||||
while (col.length < 6) col = `0${col}`;
|
||||
return `#${col}`;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user