-
Notifications
You must be signed in to change notification settings - Fork 145
/
Webhook.lua
147 lines (126 loc) · 3.75 KB
/
Webhook.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
--[=[
@c Webhook x Snowflake
@d Represents a handle used to send webhook messages to a guild text channel in a
one-way fashion. This class defines methods and properties for managing the
webhook, not for sending messages.
]=]
local json = require('json')
local enums = require('enums')
local Snowflake = require('containers/abstract/Snowflake')
local User = require('containers/User')
local Resolver = require('client/Resolver')
local defaultAvatar = assert(enums.defaultAvatar)
local Webhook, get = require('class')('Webhook', Snowflake)
function Webhook:__init(data, parent)
Snowflake.__init(self, data, parent)
self._user = data.user and self.client._users:_insert(data.user) -- DNE if getting by token
end
function Webhook:_modify(payload)
local data, err = self.client._api:modifyWebhook(self._id, payload)
if data then
self:_load(data)
return true
else
return false, err
end
end
--[=[
@m getAvatarURL
@t mem
@op size number
@op ext string
@r string
@d Returns a URL that can be used to view the webhooks's full avatar. If provided,
the size must be a power of 2 while the extension must be a valid image format.
If the webhook does not have a custom avatar, the default URL is returned.
]=]
function Webhook:getAvatarURL(size, ext)
return User.getAvatarURL(self, size, ext)
end
--[=[
@m getDefaultAvatarURL
@t mem
@op size number
@r string
@d Returns a URL that can be used to view the webhooks's default avatar.
]=]
function Webhook:getDefaultAvatarURL(size)
return User.getDefaultAvatarURL(self, size)
end
--[=[
@m setName
@t http
@p name string
@r boolean
@d Sets the webhook's name. This must be between 2 and 32 characters in length.
]=]
function Webhook:setName(name)
return self:_modify({name = name or json.null})
end
--[=[
@m setAvatar
@t http
@p avatar Base64-Resolvable
@r boolean
@d Sets the webhook's avatar. If `nil` is passed, the avatar is removed.
]=]
function Webhook:setAvatar(avatar)
avatar = avatar and Resolver.base64(avatar)
return self:_modify({avatar = avatar or json.null})
end
--[=[
@m delete
@t http
@r boolean
@d Permanently deletes the webhook. This cannot be undone!
]=]
function Webhook:delete()
local data, err = self.client._api:deleteWebhook(self._id)
if data then
return true
else
return false, err
end
end
--[=[@p guildId string The ID of the guild in which this webhook exists.]=]
function get.guildId(self)
return self._guild_id
end
--[=[@p channelId string The ID of the channel in which this webhook exists.]=]
function get.channelId(self)
return self._channel_id
end
--[=[@p user User/nil The user that created this webhook.]=]
function get.user(self)
return self._user
end
--[=[@p token string The token that can be used to access this webhook.]=]
function get.token(self)
return self._token
end
--[=[@p name string The name of the webhook. This should be between 2 and 32 characters in length.]=]
function get.name(self)
return self._name
end
--[=[@p type number The type of the webhook. See the `webhookType` enum for a human-readable representation.]=]
function get.type(self)
return self._type
end
--[=[@p avatar string/nil The hash for the webhook's custom avatar, if one is set.]=]
function get.avatar(self)
return self._avatar
end
--[=[@p avatarURL string Equivalent to the result of calling `Webhook:getAvatarURL()`.]=]
function get.avatarURL(self)
return self:getAvatarURL()
end
--[=[@p defaultAvatar number The default avatar for the webhook. See the `defaultAvatar` enumeration for
a human-readable representation. This should always be `defaultAvatar.blurple`.]=]
function get.defaultAvatar()
return defaultAvatar.blurple
end
--[=[@p defaultAvatarURL string Equivalent to the result of calling `Webhook:getDefaultAvatarURL()`.]=]
function get.defaultAvatarURL(self)
return self:getDefaultAvatarURL()
end
return Webhook