forked from abalabahaha/eris
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Member.js
228 lines (202 loc) · 8.47 KB
/
Member.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
"use strict";
const Base = require("./Base");
const Permission = require("./Permission");
const {Permissions} = require("../Constants");
const User = require("./User");
const VoiceState = require("./VoiceState");
/**
* Represents a server member
* @prop {String} id The ID of the member
* @prop {String} mention A string that mentions the member
* @prop {Guild} guild The guild the member is in
* @prop {Number} joinedAt Timestamp of when the member joined the guild
* @prop {String} status The member's status. Either "online", "idle", "dnd", or "offline"
* @prop {Object?} game The active game the member is playing
* @prop {String} game.name The name of the active game
* @prop {Object} clientStatus The member's per-client status
* @prop {String} clientStatus.web The member's status on web. Either "online", "idle", "dnd", or "offline". Will be "online" for bots
* @prop {String} clientStatus.desktop The member's status on desktop. Either "online", "idle", "dnd", or "offline". Will be "offline" for bots
* @prop {String} clientStatus.mobile The member's status on mobile. Either "online", "idle", "dnd", or "offline". Will be "offline" for bots
* @prop {Object[]} activities The member's current activities
* @prop {Number} game.type The type of the active game (0 is default, 1 is Twitch, 2 is YouTube)
* @prop {String?} game.url The url of the active game
* @prop {VoiceState} voiceState The voice state of the member
* @prop {String?} nick The server nickname of the member
* @prop {String[]} roles An array of role IDs this member is a part of
* @prop {User} user The user object of the member
* @prop {Permission} permission The guild-wide permissions of the member
* @prop {String} defaultAvatar The hash for the default avatar of a user if there is no avatar set
* @prop {Number} createdAt Timestamp of user creation
* @prop {Boolean} bot Whether the user is an OAuth bot or not
* @prop {String} username The username of the user
* @prop {String} discriminator The discriminator of the user
* @prop {String?} avatar The hash of the user's avatar, or null if no avatar
* @prop {String} defaultAvatarURL The URL of the user's default avatar
* @prop {String} avatarURL The URL of the user's avatar which can be either a JPG or GIF
* @prop {String} staticAvatarURL The URL of the user's avatar (always a JPG)
* @prop {Number} premiumSince Timestamp of when the member boosted the guild
*/
class Member extends Base {
constructor(data, guild, client) {
super(data.id || data.user.id);
if((this.guild = guild)) {
this.user = guild.shard.client.users.get(data.id);
if(!this.user && data.user) {
this.user = guild.shard.client.users.add(data.user, guild.shard.client);
}
if(!this.user) {
throw new Error("User associated with Member not found: " + data.id);
}
} else if(data.user) {
this.user = new User(data.user, client);
} else {
this.user = null;
}
this.update(data);
}
update(data) {
this.status = data.status !== undefined ? data.status : this.status || "offline";
this.game = data.game !== undefined ? data.game : this.game || null;
if(data.joined_at !== undefined) this.joinedAt = Date.parse(data.joined_at);
if(data.client_status !== undefined) this.clientStatus = Object.assign({web: "offline", desktop: "offline", mobile: "offline"}, data.client_status);
if(data.activities !== undefined) this.activities = data.activities;
if(data.premium_since !== undefined) this.premiumSince = data.premium_since;
if("mute" in data) {
const state = this.guild.voiceStates.get(this.id);
if(data.channel_id === null && !data.mute && !data.deaf && !data.suppress) {
this.guild.voiceStates.delete(this.id);
} else if(state) {
state.update(data);
} else if(data.channel_id || data.mute || data.deaf || data.suppress) {
this.guild.voiceStates.update(data);
}
}
this.nick = data.nick !== undefined ? data.nick : this.nick || null;
if(data.roles !== undefined) {
this.roles = data.roles;
}
}
get voiceState() {
return this.guild.voiceStates.get(this.id) || new VoiceState({
id: this.id
});
}
get permission() {
if(this.id === this.guild.ownerID) {
return new Permission(Permissions.all);
} else {
let permissions = this.guild.roles.get(this.guild.id).permissions.allow;
for(let role of this.roles) {
role = this.guild.roles.get(role);
if(!role) {
continue;
}
const {allow: perm} = role.permissions;
if(perm & Permissions.administrator) {
permissions = Permissions.all;
break;
} else {
permissions |= perm;
}
}
return new Permission(permissions);
}
}
get username() {
return this.user.username;
}
get discriminator() {
return this.user.discriminator;
}
get avatar() {
return this.user.avatar;
}
get bot() {
return this.user.bot;
}
get createdAt() {
return this.user.createdAt;
}
get defaultAvatar() {
return this.user.defaultAvatar;
}
get defaultAvatarURL() {
return this.user.defaultAvatarURL;
}
get staticAvatarURL(){
return this.user.staticAvatarURL;
}
get avatarURL() {
return this.user.avatarURL;
}
get mention() {
return `<@!${this.id}>`;
}
/**
* Edit the guild member
* @arg {Object} options The properties to edit
* @arg {String[]} [options.roles] The array of role IDs the user should have
* @arg {String} [options.nick] Set the user's server nickname, "" to remove
* @arg {Boolean} [options.mute] Server mute the user
* @arg {Boolean} [options.deaf] Server deafen the user
* @arg {String} [options.channelID] The ID of the voice channel to move the user to (must be in voice)
* @arg {String} [reason] The reason to be displayed in audit logs
* @returns {Promise}
*/
edit(options, reason) {
return this.guild.shard.client.editGuildMember.call(this.guild.shard.client, this.guild.id, this.id, options, reason);
}
/**
* Add a role to the guild member
* @arg {String} roleID The ID of the role
* @arg {String} [reason] The reason to be displayed in audit logs
* @returns {Promise}
*/
addRole(roleID, reason) {
return this.guild.shard.client.addGuildMemberRole.call(this.guild.shard.client, this.guild.id, this.id, roleID, reason);
}
/**
* Remove a role from the guild member
* @arg {String} roleID The ID of the role
* @arg {String} [reason] The reason to be displayed in audit logs
* @returns {Promise}
*/
removeRole(roleID, reason) {
return this.guild.shard.client.removeGuildMemberRole.call(this.guild.shard.client, this.guild.id, this.id, roleID, reason);
}
/**
* Kick the member from the guild
* @arg {String} [reason] The reason to be displayed in audit logs
* @returns {Promise}
*/
kick(reason) {
return this.guild.shard.client.kickGuildMember.call(this.guild.shard.client, this.guild.id, this.id, reason);
}
/**
* Ban the user from the guild
* @arg {Number} [deleteMessageDays=0] Number of days to delete messages for
* @arg {String} [reason] The reason to be displayed in audit logs
* @returns {Promise}
*/
ban(deleteMessageDays, reason) {
return this.guild.shard.client.banGuildMember.call(this.guild.shard.client, this.guild.id, this.id, deleteMessageDays, reason);
}
/**
* Unban the user from the guild
* @arg {String} [reason] The reason to be displayed in audit logs
* @returns {Promise}
*/
unban(reason) {
return this.guild.shard.client.unbanGuildMember.call(this.guild.shard.client, this.guild.id, this.id, reason);
}
toJSON() {
const base = super.toJSON(true);
for(const prop of ["game", "joinedAt", "nick", "roles", "status", "user", "voiceState", "premiumSince"]) {
if(this[prop] !== undefined) {
base[prop] = this[prop] && this[prop].toJSON ? this[prop].toJSON() : this[prop];
}
}
return base;
}
}
module.exports = Member;