-
Notifications
You must be signed in to change notification settings - Fork 35
/
webhook.go
361 lines (301 loc) · 8.1 KB
/
webhook.go
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
package discord
import (
"fmt"
"time"
"github.com/disgoorg/disgo/json"
"github.com/disgoorg/snowflake/v2"
)
// WebhookType (https: //discord.com/developers/docs/resources/webhook#webhook-object-webhook-types)
type WebhookType int
// All WebhookType(s)
const (
WebhookTypeIncoming WebhookType = iota + 1
WebhookTypeChannelFollower
WebhookTypeApplication
)
// Webhook (https://discord.com/developers/docs/resources/webhook) is a way to post messages to Discord using the Discord API which do not require bot authentication or use.
type Webhook interface {
json.Marshaler
Type() WebhookType
ID() snowflake.ID
Name() string
Avatar() *string
AvatarURL(opts ...CDNOpt) *string
CreatedAt() time.Time
webhook()
}
type UnmarshalWebhook struct {
Webhook
}
func (w *UnmarshalWebhook) UnmarshalJSON(data []byte) error {
var wType struct {
Type WebhookType `json:"type"`
}
if err := json.Unmarshal(data, &wType); err != nil {
return err
}
var (
webhook Webhook
err error
)
switch wType.Type {
case WebhookTypeIncoming:
var v IncomingWebhook
err = json.Unmarshal(data, &v)
webhook = v
case WebhookTypeChannelFollower:
var v ChannelFollowerWebhook
err = json.Unmarshal(data, &v)
webhook = v
case WebhookTypeApplication:
var v ApplicationWebhook
err = json.Unmarshal(data, &v)
webhook = v
default:
err = fmt.Errorf("unkown webhook with type %d received", wType.Type)
}
if err != nil {
return err
}
w.Webhook = webhook
return nil
}
var _ Webhook = (*IncomingWebhook)(nil)
type IncomingWebhook struct {
id snowflake.ID
name string
avatar *string
ChannelID snowflake.ID `json:"channel_id"`
GuildID snowflake.ID `json:"guild_id"`
Token string `json:"token"`
ApplicationID *snowflake.ID `json:"application_id"`
User User `json:"user"`
}
func (w *IncomingWebhook) UnmarshalJSON(data []byte) error {
type incomingWebhook IncomingWebhook
var v struct {
ID snowflake.ID `json:"id"`
Name string `json:"name"`
Avatar *string `json:"avatar"`
incomingWebhook
}
if err := json.Unmarshal(data, &v); err != nil {
return err
}
*w = IncomingWebhook(v.incomingWebhook)
w.id = v.ID
w.name = v.Name
w.avatar = v.Avatar
return nil
}
func (w IncomingWebhook) MarshalJSON() ([]byte, error) {
type incomingWebhook IncomingWebhook
return json.Marshal(struct {
Type WebhookType `json:"type"`
incomingWebhook
}{
Type: w.Type(),
incomingWebhook: incomingWebhook(w),
})
}
func (IncomingWebhook) Type() WebhookType {
return WebhookTypeIncoming
}
func (w IncomingWebhook) ID() snowflake.ID {
return w.id
}
func (w IncomingWebhook) Name() string {
return w.name
}
func (w IncomingWebhook) Avatar() *string {
return w.avatar
}
func (w IncomingWebhook) EffectiveAvatarURL(opts ...CDNOpt) string {
if avatarURL := w.AvatarURL(opts...); avatarURL != nil {
return *avatarURL
}
return w.DefaultAvatarURL(opts...)
}
func (w IncomingWebhook) AvatarURL(opts ...CDNOpt) *string {
if w.Avatar() == nil {
return nil
}
url := formatAssetURL(UserAvatar, opts, w.ID(), *w.Avatar())
return &url
}
func (w IncomingWebhook) DefaultAvatarURL(opts ...CDNOpt) string {
return formatAssetURL(DefaultUserAvatar, opts, 0)
}
func (w IncomingWebhook) URL() string {
return WebhookURL(w.ID(), w.Token)
}
func (w IncomingWebhook) CreatedAt() time.Time {
return w.id.Time()
}
func (IncomingWebhook) webhook() {}
var _ Webhook = (*ChannelFollowerWebhook)(nil)
type ChannelFollowerWebhook struct {
id snowflake.ID
name string
avatar *string
ChannelID snowflake.ID `json:"channel_id"`
GuildID snowflake.ID `json:"guild_id"`
SourceGuild WebhookSourceGuild `json:"source_guild"`
SourceChannel WebhookSourceChannel `json:"source_channel"`
User User `json:"user"`
}
func (w *ChannelFollowerWebhook) UnmarshalJSON(data []byte) error {
type channelFollowerWebhook ChannelFollowerWebhook
var v struct {
ID snowflake.ID `json:"id"`
Name string `json:"name"`
Avatar *string `json:"avatar"`
channelFollowerWebhook
}
if err := json.Unmarshal(data, &v); err != nil {
return err
}
*w = ChannelFollowerWebhook(v.channelFollowerWebhook)
w.id = v.ID
w.name = v.Name
w.avatar = v.Avatar
return nil
}
func (w ChannelFollowerWebhook) MarshalJSON() ([]byte, error) {
type channelFollowerWebhook ChannelFollowerWebhook
return json.Marshal(struct {
Type WebhookType `json:"type"`
channelFollowerWebhook
}{
Type: w.Type(),
channelFollowerWebhook: channelFollowerWebhook(w),
})
}
func (ChannelFollowerWebhook) Type() WebhookType {
return WebhookTypeChannelFollower
}
func (w ChannelFollowerWebhook) ID() snowflake.ID {
return w.id
}
func (w ChannelFollowerWebhook) Name() string {
return w.name
}
func (w ChannelFollowerWebhook) Avatar() *string {
return w.avatar
}
func (w ChannelFollowerWebhook) EffectiveAvatarURL(opts ...CDNOpt) string {
if w.Avatar() == nil {
return w.DefaultAvatarURL(opts...)
}
if avatar := w.AvatarURL(opts...); avatar != nil {
return *avatar
}
return ""
}
func (w ChannelFollowerWebhook) AvatarURL(opts ...CDNOpt) *string {
if w.Avatar() == nil {
return nil
}
url := formatAssetURL(UserAvatar, opts, w.ID(), *w.Avatar())
return &url
}
func (w ChannelFollowerWebhook) DefaultAvatarURL(opts ...CDNOpt) string {
return formatAssetURL(DefaultUserAvatar, opts, 0)
}
func (w ChannelFollowerWebhook) CreatedAt() time.Time {
return w.id.Time()
}
func (ChannelFollowerWebhook) webhook() {}
var _ Webhook = (*ApplicationWebhook)(nil)
type ApplicationWebhook struct {
id snowflake.ID
name string
avatar *string
ApplicationID snowflake.ID
}
func (w *ApplicationWebhook) UnmarshalJSON(data []byte) error {
var v struct {
ID snowflake.ID `json:"id"`
Name string `json:"name"`
Avatar *string `json:"avatar"`
ApplicationID snowflake.ID `json:"application_id"`
}
if err := json.Unmarshal(data, &v); err != nil {
return err
}
w.id = v.ID
w.name = v.Name
w.avatar = v.Avatar
w.ApplicationID = v.ApplicationID
return nil
}
func (w ApplicationWebhook) MarshalJSON() ([]byte, error) {
type applicationWebhook ApplicationWebhook
return json.Marshal(struct {
Type WebhookType `json:"type"`
applicationWebhook
}{
Type: w.Type(),
applicationWebhook: applicationWebhook(w),
})
}
func (ApplicationWebhook) Type() WebhookType {
return WebhookTypeApplication
}
func (w ApplicationWebhook) ID() snowflake.ID {
return w.id
}
func (w ApplicationWebhook) Name() string {
return w.name
}
func (w ApplicationWebhook) Avatar() *string {
return w.avatar
}
func (w ApplicationWebhook) EffectiveAvatarURL(opts ...CDNOpt) string {
if w.Avatar() == nil {
return w.DefaultAvatarURL(opts...)
}
if avatar := w.AvatarURL(opts...); avatar != nil {
return *avatar
}
return ""
}
func (w ApplicationWebhook) AvatarURL(opts ...CDNOpt) *string {
if w.Avatar() == nil {
return nil
}
url := formatAssetURL(UserAvatar, opts, w.ID(), *w.Avatar())
return &url
}
func (w ApplicationWebhook) DefaultAvatarURL(opts ...CDNOpt) string {
return formatAssetURL(DefaultUserAvatar, opts, 0)
}
func (w ApplicationWebhook) CreatedAt() time.Time {
return w.id.Time()
}
func (ApplicationWebhook) webhook() {}
type WebhookSourceGuild struct {
ID snowflake.ID `json:"id"`
Name string `json:"name"`
Icon *json.Nullable[Icon] `json:"icon"`
}
type WebhookSourceChannel struct {
ID snowflake.ID `json:"id"`
Name string `json:"name"`
}
// WebhookCreate is used to create a Webhook
type WebhookCreate struct {
Name string `json:"name"`
Avatar *Icon `json:"avatar,omitempty"`
}
// WebhookUpdate is used to update a Webhook
type WebhookUpdate struct {
Name *string `json:"name,omitempty"`
Avatar *json.Nullable[Icon] `json:"avatar,omitempty"`
ChannelID *snowflake.ID `json:"channel_id"`
}
// WebhookUpdateWithToken is used to update a Webhook with the token
type WebhookUpdateWithToken struct {
Name *string `json:"name,omitempty"`
Avatar *string `json:"avatar,omitempty"`
}