forked from abalabahaha/eris
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Role.js
94 lines (84 loc) · 3.42 KB
/
Role.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
"use strict";
const Base = require("./Base");
const Permission = require("./Permission");
/**
* Represents a role
* @prop {String} id The ID of the role
* @prop {Number} createdAt Timestamp of the role's creation
* @prop {Guild} guild The guild that owns the role
* @prop {String} mention A string that mentions the role
* @prop {Number} createdAt Timestamp of role creation
* @prop {String} name The name of the role
* @prop {Boolean} mentionable Whether the role is mentionable or not
* @prop {Boolean} managed Whether a guild integration manages this role or not
* @prop {Boolean} hoist Whether users with this role are hoisted in the user list or not
* @prop {Number} color The hex color of the role in base 10
* @prop {Number} position The position of the role
* @prop {Permission} permissions The permissions representation of the role
*/
class Role extends Base {
constructor(data, guild) {
super(data.id);
this.guild = guild;
this.update(data);
}
update(data) {
if(data.name !== undefined) this.name = data.name;
if(data.mentionable !== undefined) this.mentionable = data.mentionable;
if(data.managed !== undefined) this.managed = data.managed;
if(data.hoist !== undefined) this.hoist = data.hoist;
if(data.color !== undefined) this.color = data.color;
if(data.position !== undefined) this.position = data.position;
if(data.permissions !== undefined) this.permissions = new Permission(data.permissions);
}
/**
* Generates a JSON representation of the role permissions
* @returns {Object}
*/
get json() {
return this.permissions.json;
}
get mention() {
return `<@&${this.id}>`;
}
/**
* Edit the guild role
* @arg {Object} options The properties to edit
* @arg {String} [options.name] The name of the role
* @arg {Number} [options.permissions] The role permissions number
* @arg {Number} [options.color] The hex color of the role, in number form (ex: 0x3da5b3 or 4040115)
* @arg {Boolean} [options.hoist] Whether to hoist the role in the user list or not
* @arg {Boolean} [options.mentionable] Whether the role is mentionable or not
* @arg {String} [reason] The reason to be displayed in audit logs
* @returns {Promise<Role>}
*/
edit(options, reason) {
return this.guild.shard.client.editRole.call(this.guild.shard.client, this.guild.id, this.id, options, reason);
}
/**
* Edit the role's position. Note that role position numbers are highest on top and lowest at the bottom.
* @arg {Number} position The new position of the role
* @returns {Promise}
*/
editPosition(position) {
return this.guild.shard.client.editRolePosition.call(this.guild.shard.client, this.guild.id, this.id, position);
}
/**
* Delete the role
* @arg {String} [reason] The reason to be displayed in audit logs
* @returns {Promise}
*/
delete(reason) {
return this.guild.shard.client.deleteRole.call(this.guild.shard.client, this.guild.id, this.id, reason);
}
toJSON() {
const base = super.toJSON(true);
for(const prop of ["color", "hoist", "managed", "mentionable", "name", "permissions", "position"]) {
if(this[prop] !== undefined) {
base[prop] = this[prop] && this[prop].toJSON ? this[prop].toJSON() : this[prop];
}
}
return base;
}
}
module.exports = Role;