-
Notifications
You must be signed in to change notification settings - Fork 145
/
GroupChannel.lua
122 lines (105 loc) · 2.9 KB
/
GroupChannel.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
--[=[
@c GroupChannel x TextChannel
@d Represents a Discord group channel. Essentially a private channel that may have
more than one and up to ten recipients. This class should only be relevant to
user-accounts; bots cannot normally join group channels.
]=]
local json = require('json')
local TextChannel = require('containers/abstract/TextChannel')
local SecondaryCache = require('iterables/SecondaryCache')
local Resolver = require('client/Resolver')
local format = string.format
local GroupChannel, get = require('class')('GroupChannel', TextChannel)
function GroupChannel:__init(data, parent)
TextChannel.__init(self, data, parent)
self._recipients = SecondaryCache(data.recipients, self.client._users)
end
--[=[
@m setName
@t http
@p name string
@r boolean
@d Sets the channel's name. This must be between 1 and 100 characters in length.
]=]
function GroupChannel:setName(name)
return self:_modify({name = name or json.null})
end
--[=[
@m setIcon
@t http
@p icon Base64-Resolvable
@r boolean
@d Sets the channel's icon. To remove the icon, pass `nil`.
]=]
function GroupChannel:setIcon(icon)
icon = icon and Resolver.base64(icon)
return self:_modify({icon = icon or json.null})
end
--[=[
@m addRecipient
@t http
@p id User-ID-Resolvable
@r boolean
@d Adds a user to the channel.
]=]
function GroupChannel:addRecipient(id)
id = Resolver.userId(id)
local data, err = self.client._api:groupDMAddRecipient(self._id, id)
if data then
return true
else
return false, err
end
end
--[=[
@m removeRecipient
@t http
@p id User-ID-Resolvable
@r boolean
@d Removes a user from the channel.
]=]
function GroupChannel:removeRecipient(id)
id = Resolver.userId(id)
local data, err = self.client._api:groupDMRemoveRecipient(self._id, id)
if data then
return true
else
return false, err
end
end
--[=[
@m leave
@t http
@r boolean
@d Removes the client's user from the channel. If no users remain, the channel
is destroyed.
]=]
function GroupChannel:leave()
return self:_delete()
end
--[=[@p recipients SecondaryCache A secondary cache of users that are present in the channel.]=]
function get.recipients(self)
return self._recipients
end
--[=[@p name string The name of the channel.]=]
function get.name(self)
return self._name
end
--[=[@p ownerId string The Snowflake ID of the user that owns (created) the channel.]=]
function get.ownerId(self)
return self._owner_id
end
--[=[@p owner User/nil Equivalent to `GroupChannel.recipients:get(GroupChannel.ownerId)`.]=]
function get.owner(self)
return self._recipients:get(self._owner_id)
end
--[=[@p icon string/nil The hash for the channel's custom icon, if one is set.]=]
function get.icon(self)
return self._icon
end
--[=[@p iconURL string/nil The URL that can be used to view the channel's icon, if one is set.]=]
function get.iconURL(self)
local icon = self._icon
return icon and format('https://cdn.discordapp.com/channel-icons/%s/%s.png', self._id, icon)
end
return GroupChannel