forked from andersfylling/disgord
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebhook.go
More file actions
347 lines (296 loc) · 13.3 KB
/
webhook.go
File metadata and controls
347 lines (296 loc) · 13.3 KB
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
package disgord
import (
"context"
"errors"
"github.com/andersfylling/disgord/internal/endpoint"
"github.com/andersfylling/disgord/internal/httd"
)
// Webhook Used to represent a webhook
// https://discord.com/developers/docs/resources/webhook#webhook-object
type Webhook struct {
ID Snowflake `json:"id"` // |
GuildID Snowflake `json:"guild_id,omitempty"` // |?
ChannelID Snowflake `json:"channel_id"` // |
User *User `json:"user,omitempty"` // ?|
Name string `json:"name"` // |?
Avatar string `json:"avatar"` // |?
Token string `json:"token"` // |
}
var _ Copier = (*Webhook)(nil)
var _ DeepCopier = (*Webhook)(nil)
//////////////////////////////////////////////////////
//
// REST Methods
//
//////////////////////////////////////////////////////
type WebhookQueryBuilder interface {
WithContext(ctx context.Context) WebhookQueryBuilder
// GetWebhook Returns the new webhook object for the given id.
Get(flags ...Flag) (*Webhook, error)
// UpdateBuilder Modify a webhook. Requires the 'MANAGE_WEBHOOKS' permission.
// Returns the updated webhook object on success.
UpdateBuilder(flags ...Flag) *updateWebhookBuilder
// Deprecated: use UpdateBuilder
Update(flags ...Flag) *updateWebhookBuilder
// Delete Deletes a webhook permanently. User must be owner. Returns a 204 NO CONTENT response on success.
Delete(flags ...Flag) error
// Execute Trigger a webhook in Discord.
Execute(params *ExecuteWebhookParams, wait bool, URLSuffix string, flags ...Flag) (*Message, error)
// ExecuteSlackWebhook Trigger a webhook in Discord from the Slack app.
ExecuteSlackWebhook(params *ExecuteWebhookParams, wait bool, flags ...Flag) (*Message, error)
// ExecuteGitHubWebhook Trigger a webhook in Discord from the GitHub app.
ExecuteGitHubWebhook(params *ExecuteWebhookParams, wait bool, flags ...Flag) (*Message, error)
WithToken(token string) WebhookWithTokenQueryBuilder
}
func (c clientQueryBuilder) Webhook(id Snowflake) WebhookQueryBuilder {
return &webhookQueryBuilder{client: c.client, webhookID: id}
}
type webhookQueryBuilder struct {
ctx context.Context
client *Client
cid Snowflake
webhookID Snowflake
}
func (w webhookQueryBuilder) WithContext(ctx context.Context) WebhookQueryBuilder {
w.ctx = ctx
return &w
}
// GetWebhook [REST] Returns the new webhook object for the given id.
// Method GET
// Endpoint /webhooks/{webhook.id}
// Discord documentation https://discord.com/developers/docs/resources/webhook#get-webhook
// Reviewed 2018-08-14
// Comment -
func (w webhookQueryBuilder) Get(flags ...Flag) (ret *Webhook, err error) {
r := w.client.newRESTRequest(&httd.Request{
Endpoint: endpoint.Webhook(w.webhookID),
Ctx: w.ctx,
}, flags)
r.factory = func() interface{} {
return &Webhook{}
}
return getWebhook(r.Execute)
}
// UpdateWebhook [REST] Modify a webhook. Requires the 'MANAGE_WEBHOOKS' permission.
// Returns the updated webhook object on success.
// Method PATCH
// Endpoint /webhooks/{webhook.id}
// Discord documentation https://discord.com/developers/docs/resources/webhook#modify-webhook
// Reviewed 2018-08-14
// Comment All parameters to this endpoint.
func (w webhookQueryBuilder) UpdateBuilder(flags ...Flag) (builder *updateWebhookBuilder) {
builder = &updateWebhookBuilder{}
builder.r.itemFactory = func() interface{} {
return &Webhook{}
}
builder.r.flags = flags
builder.r.addPrereq(w.webhookID.IsZero(), "given webhook ID was not set, there is nothing to modify")
builder.r.setup(w.client.req, &httd.Request{
Method: httd.MethodPatch,
Ctx: w.ctx,
Endpoint: endpoint.Webhook(w.webhookID),
ContentType: httd.ContentTypeJSON,
}, nil)
return builder
}
// DeleteWebhook [REST] Delete a webhook permanently. User must be owner. Returns a 204 NO CONTENT response on success.
// Method DELETE
// Endpoint /webhooks/{webhook.id}
// Discord documentation https://discord.com/developers/docs/resources/webhook#delete-webhook
// Reviewed 2018-08-14
// Comment -
func (w webhookQueryBuilder) Delete(flags ...Flag) (err error) {
return w.WithToken("").WithContext(w.ctx).Delete(flags...)
}
// ExecuteWebhookParams JSON params for func ExecuteWebhook
type ExecuteWebhookParams struct {
Content string `json:"content"`
Username string `json:"username"`
AvatarURL string `json:"avatar_url"`
TTS bool `json:"tts"`
File interface{} `json:"file"`
Embeds []*Embed `json:"embeds"`
}
type execWebhookParams struct {
Wait bool `urlparam:"wait"`
}
var _ URLQueryStringer = (*execWebhookParams)(nil)
// Execute Trigger a webhook in Discord.
func (w webhookQueryBuilder) Execute(params *ExecuteWebhookParams, wait bool, URLSuffix string, flags ...Flag) (message *Message, err error) {
return w.WithToken("").WithContext(w.ctx).Execute(params, wait, URLSuffix, flags...)
}
// ExecuteSlackWebhook [REST] Trigger a webhook in Discord from the Slack app.
// Method POST
// Endpoint /webhooks/{webhook.id}/{webhook.token}
// Discord documentation https://discord.com/developers/docs/resources/webhook#execute-slackcompatible-webhook
// Reviewed 2020-05-21
// Comment Refer to Slack's documentation for more information. We do not support Slack's channel,
// icon_emoji, mrkdwn, or mrkdwn_in properties.
func (w webhookQueryBuilder) ExecuteSlackWebhook(params *ExecuteWebhookParams, wait bool, flags ...Flag) (message *Message, err error) {
return w.WithToken("").WithContext(w.ctx).Execute(params, wait, endpoint.Slack(), flags...)
}
// ExecuteGitHubWebhook [REST] Trigger a webhook in Discord from the GitHub app.
// Method POST
// Endpoint /webhooks/{webhook.id}/{webhook.token}
// Discord documentation https://discord.com/developers/docs/resources/webhook#execute-githubcompatible-webhook
// Reviewed 2020-05-21
// Comment Add a new webhook to your GitHub repo (in the repo's settings), and use this endpoint.
// as the "Payload URL." You can choose what events your Discord channel receives by
// choosing the "Let me select individual events" option and selecting individual
// events for the new webhook you're configuring.
func (w webhookQueryBuilder) ExecuteGitHubWebhook(params *ExecuteWebhookParams, wait bool, flags ...Flag) (message *Message, err error) {
return w.WithToken("").WithContext(w.ctx).Execute(params, wait, endpoint.GitHub(), flags...)
}
type WebhookWithTokenQueryBuilder interface {
WithContext(ctx context.Context) WebhookWithTokenQueryBuilder
// Get Same as GetWebhook, except this call does not require authentication and
// returns no user in the webhook object.
Get(flags ...Flag) (*Webhook, error)
// UpdateBuilder Same as UpdateWebhook, except this call does not require authentication,
// does _not_ accept a channel_id parameter in the body, and does not return a user in the webhook object.
UpdateBuilder(flags ...Flag) *updateWebhookBuilder
// Deprecated: use UpdateBuilder
Update(flags ...Flag) *updateWebhookBuilder
// Delete Same as DeleteWebhook, except this call does not require authentication.
Delete(flags ...Flag) error
Execute(params *ExecuteWebhookParams, wait bool, URLSuffix string, flags ...Flag) (*Message, error)
}
func (w webhookQueryBuilder) WithToken(token string) WebhookWithTokenQueryBuilder {
return &webhookWithTokenQueryBuilder{client: w.client, webhookID: w.webhookID, token: token}
}
type webhookWithTokenQueryBuilder struct {
ctx context.Context
client *Client
cid Snowflake
webhookID Snowflake
token string
}
func (w webhookWithTokenQueryBuilder) WithContext(ctx context.Context) WebhookWithTokenQueryBuilder {
w.ctx = ctx
return &w
}
// GetWebhookWithToken [REST] Same as GetWebhook, except this call does not require authentication and
// returns no user in the webhook object.
// Method GET
// Endpoint /webhooks/{webhook.id}/{webhook.token}
// Discord documentation https://discord.com/developers/docs/resources/webhook#get-webhook-with-token
// Reviewed 2018-08-14
// Comment -
func (w webhookWithTokenQueryBuilder) Get(flags ...Flag) (*Webhook, error) {
r := w.client.newRESTRequest(&httd.Request{
Endpoint: endpoint.WebhookToken(w.webhookID, w.token),
Ctx: w.ctx,
}, flags)
r.factory = func() interface{} {
return &Webhook{}
}
return getWebhook(r.Execute)
}
// UpdateWebhookWithToken [REST] Same as UpdateWebhook, except this call does not require authentication,
// does _not_ accept a channel_id parameter in the body, and does not return a user in the webhook object.
// Method PATCH
// Endpoint /webhooks/{webhook.id}/{webhook.token}
// Discord documentation https://discord.com/developers/docs/resources/webhook#modify-webhook-with-token
// Reviewed 2018-08-14
// Comment All parameters to this endpoint. are optional.
func (w webhookWithTokenQueryBuilder) UpdateBuilder(flags ...Flag) (builder *updateWebhookBuilder) {
builder = &updateWebhookBuilder{}
builder.r.itemFactory = func() interface{} {
return &Webhook{}
}
builder.r.flags = flags
builder.r.addPrereq(w.webhookID.IsZero(), "given webhook ID was not set, there is nothing to modify")
builder.r.addPrereq(w.token == "", "given webhook token was not set")
builder.r.setup(w.client.req, &httd.Request{
Method: httd.MethodPatch,
Ctx: w.ctx,
Endpoint: endpoint.WebhookToken(w.webhookID, w.token),
ContentType: httd.ContentTypeJSON,
}, nil)
return builder
}
// DeleteWebhookWithToken [REST] Same as DeleteWebhook, except this call does not require authentication.
// Method DELETE
// Endpoint /webhooks/{webhook.id}/{webhook.token}
// Discord documentation https://discord.com/developers/docs/resources/webhook#delete-webhook-with-token
// Reviewed 2018-08-14
// Comment -
func (w webhookWithTokenQueryBuilder) Delete(flags ...Flag) error {
var e string
if w.token != "" {
e = endpoint.WebhookToken(w.webhookID, w.token)
} else {
e = endpoint.Webhook(w.webhookID)
}
r := w.client.newRESTRequest(&httd.Request{
Method: httd.MethodDelete,
Endpoint: e,
Ctx: w.ctx,
}, flags)
_, err := r.Execute()
return err
}
// ExecuteWebhook [REST] Trigger a webhook in Discord.
// Method POST
// Endpoint /webhooks/{webhook.id}/{webhook.token}
// Discord documentation https://discord.com/developers/docs/resources/webhook#execute-webhook
// Reviewed 2020-05-21
// Comment This endpoint. supports both JSON and form data bodies. It does require
// multipart/form-data requests instead of the normal JSON request type when
// uploading files. Make sure you set your Content-Type to multipart/form-data if
// you're doing that. Note that in that case, the embeds field cannot be used, but
// you can pass an url-encoded JSON body as a form value for payload_json.
// Comment#2 For the webhook embed objects, you can set every field except type (it will be
// rich regardless of if you try to set it), provider, video, and any height, width,
// or proxy_url values for images.
func (w webhookWithTokenQueryBuilder) Execute(params *ExecuteWebhookParams, wait bool, URLSuffix string, flags ...Flag) (message *Message, err error) {
if params == nil {
return nil, errors.New("params can not be nil")
}
if w.webhookID.IsZero() {
return nil, errors.New("webhook id is required")
}
if w.token == "" {
return nil, errors.New("webhook token is required")
}
var contentType string
if params.File == nil {
contentType = httd.ContentTypeJSON
} else {
contentType = "multipart/form-data"
}
urlparams := &execWebhookParams{wait}
r := w.client.newRESTRequest(&httd.Request{
Method: httd.MethodPost,
Ctx: w.ctx,
Endpoint: endpoint.WebhookToken(w.webhookID, w.token) + URLSuffix + urlparams.URLQueryString(),
Body: params,
ContentType: contentType,
}, flags)
// Discord only returns the message when wait=true.
if wait {
r.pool = w.client.pool.message
return getMessage(r.Execute)
}
_, err = r.Execute()
return nil, err
}
//////////////////////////////////////////////////////
//
// REST Builders
//
//////////////////////////////////////////////////////
// UpdateWebhookParams https://discord.com/developers/docs/resources/webhook#modify-webhook-json-params
// Allows changing the name of the webhook, avatar and moving it to another channel. It also allows to resetting the
// avatar by providing a nil to SetAvatar.
//
//generate-rest-params: name:string, avatar:string, channel_id:Snowflake,
//generate-rest-basic-execute: webhook:*Webhook,
type updateWebhookBuilder struct {
r RESTBuilder
}
// SetDefaultAvatar will reset the webhook image
func (u *updateWebhookBuilder) SetDefaultAvatar() *updateWebhookBuilder {
u.r.param("avatar", nil)
return u
}