-
Notifications
You must be signed in to change notification settings - Fork 145
/
Member.lua
610 lines (541 loc) · 14.8 KB
/
Member.lua
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
--[=[
@c Member x UserPresence
@d Represents a Discord guild member. Though one user may be a member in more than
one guild, each presence is represented by a different member object associated
with that guild. Note that any method or property that exists for the User class is
also available in the Member class.
]=]
local json = require('json')
local enums = require('enums')
local class = require('class')
local UserPresence = require('containers/abstract/UserPresence')
local ArrayIterable = require('iterables/ArrayIterable')
local Color = require('utils/Color')
local Resolver = require('client/Resolver')
local GuildChannel = require('containers/abstract/GuildChannel')
local Permissions = require('utils/Permissions')
local Date = require('utils/Date')
local Time = require('utils/Time')
local insert, remove, sort = table.insert, table.remove, table.sort
local isInstance = class.isInstance
local permission = assert(enums.permission)
local Member, get = class('Member', UserPresence)
function Member:__init(data, parent)
UserPresence.__init(self, data, parent)
return self:_loadMore(data)
end
function Member:_load(data)
UserPresence._load(self, data)
return self:_loadMore(data)
end
function Member:_loadMore(data)
if data.roles then
local roles = #data.roles > 0 and data.roles or nil
if self._roles then
self._roles._array = roles
else
self._roles_raw = roles
end
end
end
local function sorter(a, b)
if a._position == b._position then
return tonumber(a._id) < tonumber(b._id)
else
return a._position > b._position
end
end
local function predicate(role)
return role._color > 0
end
--[=[
@m getColor
@t mem
@r Color
@d Returns a color object that represents the member's color as determined by
its highest colored role. If the member has no colored roles, then the default
color with a value of 0 is returned.
]=]
function Member:getColor()
local roles = {}
for role in self.roles:findAll(predicate) do
insert(roles, role)
end
sort(roles, sorter)
return roles[1] and roles[1]:getColor() or Color()
end
--[=[
@m hasPermission
@t mem
@op channel GuildChannel
@p perm Permissions-Resolvable
@r boolean
@d Checks whether the member has a specific permission. If `channel` is omitted,
then only guild-level permissions are checked. This is a relatively expensive
operation. If you need to check multiple permissions at once, use the
`getPermissions` method and check the resulting object.
]=]
function Member:hasPermission(channel, perm)
if not perm then
perm = channel
channel = nil
end
local guild = self.guild
if channel then
if not isInstance(channel, GuildChannel) or channel.guild ~= guild then
return error('Invalid GuildChannel: ' .. tostring(channel), 2)
end
end
local n = Resolver.permission(perm)
if not n then
return error('Invalid permission: ' .. tostring(perm), 2)
end
if self.id == guild.ownerId then
return true
end
local rolePermissions = guild.defaultRole:getPermissions()
for role in self.roles:iter() do
if role.id ~= guild.id then -- just in case
rolePermissions = rolePermissions:union(role:getPermissions())
end
end
if rolePermissions:has(permission.administrator) then
return true
end
if channel then
local overwrites = channel.permissionOverwrites
local overwrite = overwrites:get(self.id)
if overwrite then
if overwrite:getAllowedPermissions():has(n) then
return true
end
if overwrite:getDeniedPermissions():has(n) then
return false
end
end
local allow, deny = Permissions(), Permissions()
for role in self.roles:iter() do
if role.id ~= guild.id then -- just in case
overwrite = overwrites:get(role.id)
if overwrite then
allow = allow:union(overwrite:getAllowedPermissions())
deny = deny:union(overwrite:getDeniedPermissions())
end
end
end
if allow:has(n) then
return true
end
if deny:has(n) then
return false
end
local everyone = overwrites:get(guild.id)
if everyone then
if everyone:getAllowedPermissions():has(n) then
return true
end
if everyone:getDeniedPermissions():has(n) then
return false
end
end
end
return rolePermissions:has(n)
end
--[=[
@m getPermissions
@t mem
@op channel GuildChannel
@r Permissions
@d Returns a permissions object that represents the member's total permissions for
the guild, or for a specific channel if one is provided. If you just need to
check one permission, use the `hasPermission` method.
]=]
function Member:getPermissions(channel)
local guild = self.guild
if channel then
if not isInstance(channel, GuildChannel) or channel.guild ~= guild then
return error('Invalid GuildChannel: ' .. tostring(channel), 2)
end
end
if self.id == guild.ownerId then
return Permissions.all()
end
local ret = guild.defaultRole:getPermissions()
for role in self.roles:iter() do
if role.id ~= guild.id then -- just in case
ret = ret:union(role:getPermissions())
end
end
if ret:has(permission.administrator) then
return Permissions.all()
end
if channel then
local overwrites = channel.permissionOverwrites
local everyone = overwrites:get(guild.id)
if everyone then
ret = everyone:getDeniedPermissions():complement(ret)
ret = ret:union(everyone:getAllowedPermissions())
end
local allow, deny = Permissions(), Permissions()
for role in self.roles:iter() do
if role.id ~= guild.id then -- just in case
local overwrite = overwrites:get(role.id)
if overwrite then
deny = deny:union(overwrite:getDeniedPermissions())
allow = allow:union(overwrite:getAllowedPermissions())
end
end
end
ret = deny:complement(ret)
ret = ret:union(allow)
local overwrite = overwrites:get(self.id)
if overwrite then
ret = overwrite:getDeniedPermissions():complement(ret)
ret = ret:union(overwrite:getAllowedPermissions())
end
end
return ret
end
--[=[
@m addRole
@t http?
@p id Role-ID-Resolvable
@r boolean
@d Adds a role to the member. If the member already has the role, then no action is
taken. Note that the everyone role cannot be explicitly added.
]=]
function Member:addRole(id)
if self:hasRole(id) then return true end
id = Resolver.roleId(id)
local data, err = self.client._api:addGuildMemberRole(self._parent._id, self.id, id)
if data then
local roles = self._roles and self._roles._array or self._roles_raw
if roles then
insert(roles, id)
else
self._roles_raw = {id}
end
return true
else
return false, err
end
end
--[=[
@m removeRole
@t http?
@p id Role-ID-Resolvable
@r boolean
@d Removes a role from the member. If the member does not have the role, then no
action is taken. Note that the everyone role cannot be removed.
]=]
function Member:removeRole(id)
if not self:hasRole(id) then return true end
id = Resolver.roleId(id)
local data, err = self.client._api:removeGuildMemberRole(self._parent._id, self.id, id)
if data then
local roles = self._roles and self._roles._array or self._roles_raw
if roles then
for i, v in ipairs(roles) do
if v == id then
remove(roles, i)
break
end
end
if #roles == 0 then
if self._roles then
self._roles._array = nil
else
self._roles_raw = nil
end
end
end
return true
else
return false, err
end
end
--[=[
@m hasRole
@t mem
@p id Role-ID-Resolvable
@r boolean
@d Checks whether the member has a specific role. This will return true for the
guild's default role in addition to any explicitly assigned roles.
]=]
function Member:hasRole(id)
id = Resolver.roleId(id)
if id == self._parent._id then return true end -- @everyone
local roles = self._roles and self._roles._array or self._roles_raw
if roles then
for _, v in ipairs(roles) do
if v == id then
return true
end
end
end
return false
end
--[=[
@m setNickname
@t http
@p nick string
@r boolean
@d Sets the member's nickname. This must be between 1 and 32 characters in length.
Pass `nil` to remove the nickname.
]=]
function Member:setNickname(nick)
nick = nick or ''
local data, err
if self.id == self.client._user._id then
data, err = self.client._api:modifyCurrentUsersNick(self._parent._id, {nick = nick})
else
data, err = self.client._api:modifyGuildMember(self._parent._id, self.id, {nick = nick})
end
if data then
self._nick = nick ~= '' and nick or nil
return true
else
return false, err
end
end
--[=[
@m setVoiceChannel
@t http
@p id Channel-ID-Resolvable
@r boolean
@d Moves the member to a new voice channel, but only if the member has an active
voice connection in the current guild. Due to complexities in voice state
handling, the member's `voiceChannel` property will update asynchronously via
WebSocket; not as a result of the HTTP request.
Not supplying an ID will result in the member being disconnected from the channel.
]=]
function Member:setVoiceChannel(id)
id = id and Resolver.channelId(id)
local data, err = self.client._api:modifyGuildMember(self._parent._id, self.id, {channel_id = id or json.null})
if data then
return true
else
return false, err
end
end
--[=[
@m mute
@t http
@r boolean
@d Mutes the member in its guild.
]=]
function Member:mute()
local data, err = self.client._api:modifyGuildMember(self._parent._id, self.id, {mute = true})
if data then
self._mute = true
return true
else
return false, err
end
end
--[=[
@m unmute
@t http
@r boolean
@d Unmutes the member in its guild.
]=]
function Member:unmute()
local data, err = self.client._api:modifyGuildMember(self._parent._id, self.id, {mute = false})
if data then
self._mute = false
return true
else
return false, err
end
end
--[=[
@m deafen
@t http
@r boolean
@d Deafens the member in its guild.
]=]
function Member:deafen()
local data, err = self.client._api:modifyGuildMember(self._parent._id, self.id, {deaf = true})
if data then
self._deaf = true
return true
else
return false, err
end
end
--[=[
@m undeafen
@t http
@r boolean
@d Undeafens the member in its guild.
]=]
function Member:undeafen()
local data, err = self.client._api:modifyGuildMember(self._parent._id, self.id, {deaf = false})
if data then
self._deaf = false
return true
else
return false, err
end
end
--[=[
@m kick
@t http
@op reason string
@r boolean
@d Equivalent to `Member.guild:kickUser(Member.user, reason)`
]=]
function Member:kick(reason)
return self._parent:kickUser(self._user, reason)
end
--[=[
@m ban
@t http
@op reason string
@op days number
@r boolean
@d Equivalent to `Member.guild:banUser(Member.user, reason, days)`
]=]
function Member:ban(reason, days)
return self._parent:banUser(self._user, reason, days)
end
--[=[
@m unban
@t http
@op reason string
@r boolean
@d Equivalent to `Member.guild:unbanUser(Member.user, reason)`
]=]
function Member:unban(reason)
return self._parent:unbanUser(self._user, reason)
end
function Member:_timeout(val)
local data, err = self.client._api:modifyGuildMember(self._parent._id, self.id, {communication_disabled_until = val or json.null})
if data then
self._communication_disabled_until = val ~= json.null and val or nil
return true
else
return false, err
end
end
--[=[
@m timeoutFor
@t http
@p duration Time/number
@r boolean
@d Sets a timeout for a guild member.
`duration` is either `Time` object or a `number` of seconds representing how long the timeout lasts.
To set an expiration date, use `timeoutUntil` instead.
]=]
function Member:timeoutFor(duration)
if type(duration) == 'number' then
duration = (Date() + Time.fromSeconds(duration)):toISO()
elseif isInstance(duration, Time) then
duration = (Date() + duration):toISO()
end
return self:_timeout(duration)
end
--[=[
@m timeoutUntil
@t http
@p date Date/number
@r boolean
@d Sets a timeout for a guild member.
`date` is either `Date` object or a UNIX epoch in seconds at which the member's timeout ends.
To set a duration, use `timeoutFor` instead.
]=]
function Member:timeoutUntil(date)
if type(date) == 'number' then
date = Date(date):toISO()
elseif isInstance(date, Date) then
date = date:toISO()
end
return self:_timeout(date)
end
--[=[
@m removeTimeout
@t http
@r boolean
@d Removes the timeout of the member.
]=]
function Member:removeTimeout()
return self:_timeout()
end
--[=[@p roles ArrayIterable An iterable array of guild roles that the member has. This does not explicitly
include the default everyone role. Object order is not guaranteed.]=]
function get.roles(self)
if not self._roles then
local roles = self._parent._roles
self._roles = ArrayIterable(self._roles_raw, function(id)
return roles:get(id)
end)
self._roles_raw = nil
end
return self._roles
end
--[=[@p name string If the member has a nickname, then this will be equivalent to that nickname.
Otherwise, this is equivalent to `Member.user.name`.]=]
function get.name(self)
return self._nick or self._user.name
end
--[=[@p nickname string/nil The member's nickname, if one is set.]=]
function get.nickname(self)
return self._nick
end
--[=[@p joinedAt string/nil The date and time at which the current member joined the guild, represented as
an ISO 8601 string plus microseconds when available. Member objects generated
via presence updates lack this property.]=]
function get.joinedAt(self)
return self._joined_at
end
--[=[@p premiumSince string/nil The date and time at which the current member boosted the guild, represented as
an ISO 8601 string plus microseconds when available.]=]
function get.premiumSince(self)
return self._premium_since
end
--[=[@p voiceChannel GuildVoiceChannel/nil The voice channel to which this member is connected in the current guild.]=]
function get.voiceChannel(self)
local guild = self._parent
local state = guild._voice_states[self:__hash()]
return state and guild._voice_channels:get(state.channel_id)
end
--[=[@p muted boolean Whether the member is voice muted in its guild.]=]
function get.muted(self)
local state = self._parent._voice_states[self:__hash()]
return state and (state.mute or state.self_mute) or self._mute
end
--[=[@p deafened boolean Whether the member is voice deafened in its guild.]=]
function get.deafened(self)
local state = self._parent._voice_states[self:__hash()]
return state and (state.deaf or state.self_deaf) or self._deaf
end
--[=[@p timedOut boolean Whether the member is timed out in its guild.]=]
function get.timedOut(self)
local state = self._communication_disabled_until
if not state then
return false
else
return Date.fromISO(state) > Date()
end
end
--[=[@p timedOutUntil string/nil The raw communication_disabled_until member property.
Note this may be provided even when the member's time out have expired.]=]
function get.timedOutUntil(self)
return self._communication_disabled_until
end
--[=[@p guild Guild The guild in which this member exists.]=]
function get.guild(self)
return self._parent
end
--[=[@p highestRole Role The highest positioned role that the member has. If the member has no
explicit roles, then this is equivalent to `Member.guild.defaultRole`.]=]
function get.highestRole(self)
local ret
for role in self.roles:iter() do
if not ret or sorter(role, ret) then
ret = role
end
end
return ret or self.guild.defaultRole
end
return Member