/
channel_message.go
553 lines (482 loc) · 17.3 KB
/
channel_message.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
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
package harmony
import (
"context"
"encoding/json"
"errors"
"net/http"
"net/url"
"strconv"
"time"
"github.com/skwair/harmony/channel"
"github.com/skwair/harmony/embed"
"github.com/skwair/harmony/internal/endpoint"
"github.com/skwair/harmony/message"
)
// Message represents a message sent in a channel within Discord.
// The author object follows the structure of the user object, but is
// only a valid user in the case where the message is generated by a
// user or bot user. If the message is generated by a webhook, the
// author object corresponds to the webhook's id, username, and avatar.
// You can tell if a message is generated by a webhook by checking for
// the webhook_id on the message object.
type Message struct {
ID string `json:"id"`
ChannelID string `json:"channel_id"`
GuildID string `json:"guild_id"`
// Author of this message. Can be a user or a webhook.
// Check the WebhookID field to know.
Author *User `json:"author"`
// Guild member info of the author that sent the message.
// Only set for MESSAGE_CREATE and MESSAGE_UPDATE Gateway
// events.
Member *GuildMember `json:"member"`
Content string `json:"content"`
Timestamp time.Time `json:"timestamp"`
EditedTimestamp time.Time `json:"edited_timestamp"`
TTS bool `json:"tts"`
// MentionEveryone is set to true if '@everyone' or '@here'
// is set in the message's content.
MentionEveryone bool `json:"mention_everyone"`
// Mentions contains an array of users that where mentioned
// in the message's content.
Mentions []User `json:"mentions"`
// MentionRoles contains an array of IDs of te roles that
// were mentioned in the message's content.
MentionRoles []string `json:"mention_roles"`
// Not all channel mentions in a message will appear in mention_channels.
// Only textual channels that are visible to everyone in a public guild
// will ever be included. Only crossposted messages (via Channel Following)
// currently include mention_channels at all. If no mentions in the message
// meet these requirements, this field will not be sent.
MentionChannels []channel.Mention `json:"mention_channels"`
Attachments []message.Attachment `json:"attachments"` // Any attached files.
Embeds []embed.Embed `json:"embeds"` // Any embedded content.
Reactions []Reaction `json:"reactions"`
Nonce string `json:"nonce"` // Used for validating a message was sent.
Pinned bool `json:"pinned"`
WebhookID string `json:"webhook_id"`
Type message.Type `json:"type"`
// Sent with Rich Presence-related chat embeds.
Activity *MessageActivity `json:"activity"`
Application *MessageApplication `json:"application"`
MessageReference *message.Reference `json:"message_reference"`
Flags message.Flag `json:"flags"`
}
// Messages returns messages in the channel. If operating on a guild channel, this
// endpoint requires the 'VIEW_CHANNEL' permission to be present on the current user. If the
// current user is missing the 'READ_MESSAGE_HISTORY' permission in the channel then this will
// return no messages (since they cannot read the message history).
// The query parameter is a message ID prefixed with one of the following character:
// - '>' for fetching messages after
// - '<' for fetching messages before
// - '~' for fetching messages around
// For example, to retrieve 50 messages around (25 before, 25 after) a message having the
// ID 221588207995121520, set query to "~221588207995121520".
// Limit is a positive integer between 1 and 100 that defaults to 50 if set to 0.
func (r *ChannelResource) Messages(ctx context.Context, query string, limit int) ([]Message, error) {
if query == "" {
return nil, errors.New("empty query")
}
q := url.Values{}
switch query[0] {
case '>':
q.Add("after", query[1:])
case '<':
q.Add("before", query[1:])
case '~':
q.Add("around", query[1:])
default:
return nil, errors.New("lll-formatted query: prefix the message ID with '>' (after), '<' (before) or '~' (around)")
}
if limit > 0 {
if limit > 100 {
limit = 100
}
q.Set("limit", strconv.Itoa(limit))
}
e := endpoint.GetChannelMessages(r.channelID, q.Encode())
resp, err := r.client.doReq(ctx, e, nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, apiError(resp)
}
var msgs []Message
if err = json.NewDecoder(resp.Body).Decode(&msgs); err != nil {
return nil, err
}
return msgs, nil
}
// Message returns a specific message in the channel. If operating on a guild channel,
// this endpoints requires the 'READ_MESSAGE_HISTORY' permission to be present on the current user.
func (r *ChannelResource) Message(ctx context.Context, id string) (*Message, error) {
e := endpoint.GetChannelMessage(r.channelID, id)
resp, err := r.client.doReq(ctx, e, nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, apiError(resp)
}
var msg Message
if err = json.NewDecoder(resp.Body).Decode(&msg); err != nil {
return nil, err
}
return &msg, nil
}
// DeleteMessage is like DeleteMessageWithReason but with no particular reason.
func (r *ChannelResource) DeleteMessage(ctx context.Context, messageID string) error {
return r.DeleteMessageWithReason(ctx, messageID, "")
}
// DeleteMessageWithReason deletes a message. If operating on a guild channel and trying to delete a
// message that was not sent by the current user, this endpoint requires the 'MANAGE_MESSAGES'
// permission. Fires a Message Delete Gateway event.
// The given reason will be set in the audit log entry for this action.
func (r *ChannelResource) DeleteMessageWithReason(ctx context.Context, messageID, reason string) error {
e := endpoint.DeleteMessage(r.channelID, messageID)
resp, err := r.client.doReqWithHeader(ctx, e, nil, reasonHeader(reason))
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusNoContent {
return apiError(resp)
}
return nil
}
// DeleteMessageBulk deletes multiple messages in a single request. This endpoint can only be
// used on guild channels and requires the 'MANAGE_MESSAGES' permission. Fires multiple
// Message Delete Gateway events.
// Any message IDs given that do not exist or are invalid will count towards the minimum and
// maximum message count (currently 2 and 100 respectively). Additionally, duplicated IDs will
// only be counted once.
// This endpoint will not delete messages older than 2 weeks, and will fail if any message
// provided is older than that.
func (r *ChannelResource) DeleteMessageBulk(ctx context.Context, messageIDs []string) error {
st := struct {
Messages []string `json:"messages"`
}{
Messages: messageIDs,
}
b, err := json.Marshal(st)
if err != nil {
return err
}
e := endpoint.BulkDeleteMessage(r.channelID)
resp, err := r.client.doReq(ctx, e, jsonPayload(b))
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusNoContent {
return apiError(resp)
}
return nil
}
// MessageOption allows to customize the content of a message.
type MessageOption func(*createMessage)
// WithContent sets the content of a message, up to 2000 characters
func WithContent(text string) MessageOption {
return MessageOption(func(m *createMessage) {
m.Content = text
})
}
// WithEmbed sets the embed of a message.
// See embed sub package for more information about embeds.
func WithEmbed(e *embed.Embed) MessageOption {
return MessageOption(func(m *createMessage) {
m.Embed = e
})
}
// WithFiles attach files to a message.
func WithFiles(files ...*File) MessageOption {
return MessageOption(func(m *createMessage) {
for _, f := range files {
m.files = append(m.files, *f)
}
})
}
// WithTTS enables text to speech for a message.
func WithTTS() MessageOption {
return MessageOption(func(m *createMessage) {
m.TTS = true
})
}
// WithNonce sets the nonce of a message.
// The nonce will be returned in the result and also transmitted to other clients.
func WithNonce(n string) MessageOption {
return MessageOption(func(m *createMessage) {
m.Nonce = n
})
}
// Send sends a message to the channel. If operating on a guild channel,
// this endpoint requires the 'SEND_MESSAGES' permission to be present on the
// current user. If the option WithTTS is set, the 'SEND_TTS_MESSAGES' permission is
// required for the message to be spoken. Returns the message sent.
// Fires a Message Create Gateway event.
// Before using this endpoint, you must connect to the gateway at least once.
func (r *ChannelResource) Send(ctx context.Context, opts ...MessageOption) (*Message, error) {
var msg createMessage
for _, opt := range opts {
opt(&msg)
}
if msg.Content == "" && msg.Embed == nil && len(msg.files) == 0 {
return nil, ErrInvalidSend
}
return r.client.sendMessage(ctx, r.channelID, &msg)
}
// SendMessage is a shorthand for Send(ctx, WithContent(text)).
func (r *ChannelResource) SendMessage(ctx context.Context, text string) (*Message, error) {
return r.Send(ctx, WithContent(text))
}
// createMessage describes a message creation.
type createMessage struct {
Content string `json:"content,omitempty"` // Up to 2000 characters.
Nonce string `json:"nonce,omitempty"`
TTS bool `json:"tts,omitempty"`
Embed *embed.Embed `json:"embed,omitempty"`
files []File
}
// json implements the multipartPayload interface so createMessage can be used as
// a payload with the multipartFromFiles method.
func (cm *createMessage) json() ([]byte, error) {
return json.Marshal(cm)
}
func (c *Client) sendMessage(ctx context.Context, channelID string, msg *createMessage) (*Message, error) {
if msg.Embed != nil && msg.Embed.Type == "" {
msg.Embed.Type = "rich"
}
var payload *requestPayload
if len(msg.files) > 0 {
b, contentType, err := multipartFromFiles(msg, msg.files...)
if err != nil {
return nil, err
}
payload = customPayload(b, contentType)
} else {
b, err := json.Marshal(msg)
if err != nil {
return nil, err
}
payload = jsonPayload(b)
}
e := endpoint.CreateMessage(channelID)
resp, err := c.doReq(ctx, e, payload)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, apiError(resp)
}
var m Message
if err = json.NewDecoder(resp.Body).Decode(&m); err != nil {
return nil, err
}
return &m, nil
}
type editMessage struct {
Content string `json:"content,omitempty"`
Embed *embed.Embed `json:"embed,omitempty"`
}
// EditMessage edits a previously sent message. You can only edit messages that have
// been sent by the current user. Fires a Message Update Gateway event. See EditEmbed
// if you need to edit some emended content.
func (r *ChannelResource) EditMessage(ctx context.Context, messageID, content string) (*Message, error) {
return r.client.editMessage(ctx, r.channelID, messageID, &editMessage{content, nil})
}
// EditEmbed is like EditMessage but with embedded content support.
func (r *ChannelResource) EditEmbed(ctx context.Context, messageID, content string, embed *embed.Embed) (*Message, error) {
return r.client.editMessage(ctx, r.channelID, messageID, &editMessage{content, embed})
}
func (c *Client) editMessage(ctx context.Context, channelID, messageID string, edit *editMessage) (*Message, error) {
b, err := json.Marshal(edit)
if err != nil {
return nil, err
}
e := endpoint.EditMessage(channelID, messageID)
resp, err := c.doReq(ctx, e, jsonPayload(b))
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, apiError(resp)
}
var msg Message
if err = json.NewDecoder(resp.Body).Decode(&msg); err != nil {
return nil, err
}
return &msg, nil
}
// Crosspost a message in a News Channel to following channels.
func (r *ChannelResource) CrossPostMessage(ctx context.Context, messageID string) (*Message, error) {
e := endpoint.CrossPostMessage(r.channelID, messageID)
resp, err := r.client.doReq(ctx, e, nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, apiError(resp)
}
var msg Message
if err = json.NewDecoder(resp.Body).Decode(&msg); err != nil {
return nil, err
}
return &msg, nil
}
// Reaction is a reaction on a Discord message.
type Reaction struct {
Count int `json:"count"`
Me bool `json:"me"`
Emoji *Emoji `json:"emoji"`
}
// Reactions returns a list of users that reacted to a message with the given emoji.
// limit is the number of users to return and can be set to any value ranging from 1 to 100.
// If set to 0, it defaults to 25. If more than 100 users reacted with the given emoji,
// the before and after parameters can be used to fetch more users.
func (r *ChannelResource) Reactions(ctx context.Context, messageID, emoji string, limit int, before, after string) ([]User, error) {
q := url.Values{}
if limit > 0 {
q.Set("limit", strconv.Itoa(limit))
}
if before != "" {
q.Set("before", before)
}
if after != "" {
q.Set("after", after)
}
e := endpoint.GetReactions(r.channelID, messageID, url.PathEscape(emoji), q.Encode())
resp, err := r.client.doReq(ctx, e, nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, apiError(resp)
}
var users []User
if err = json.NewDecoder(resp.Body).Decode(&users); err != nil {
return nil, err
}
return users, nil
}
// AddReaction adds a reaction to a message in the channel. This endpoint requires
// the 'READ_MESSAGE_HISTORY' permission to be present on the current user. Additionally,
// if nobody else has reacted to the message using this emoji, this endpoint requires
// the 'ADD_REACTIONS' permission to be present on the current user.
func (r *ChannelResource) AddReaction(ctx context.Context, messageID, emoji string) error {
e := endpoint.CreateReaction(r.channelID, messageID, url.PathEscape(emoji))
resp, err := r.client.doReq(ctx, e, nil)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusNoContent {
return apiError(resp)
}
return nil
}
// RemoveReaction removes a reaction the current user has made for the message.
func (r *ChannelResource) RemoveReaction(ctx context.Context, messageID, emoji string) error {
e := endpoint.DeleteOwnReaction(r.channelID, messageID, url.PathEscape(emoji))
resp, err := r.client.doReq(ctx, e, nil)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusNoContent {
return apiError(resp)
}
return nil
}
// RemoveUserReaction removes another user's reaction. This endpoint requires the
// 'MANAGE_MESSAGES' permission to be present on the current user.
func (r *ChannelResource) RemoveUserReaction(ctx context.Context, messageID, userID, emoji string) error {
e := endpoint.DeleteUserReaction(r.channelID, messageID, userID, url.PathEscape(emoji))
resp, err := r.client.doReq(ctx, e, nil)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusNoContent {
return apiError(resp)
}
return nil
}
// RemoveAllReactions removes all reactions on a message. This endpoint requires the
// 'MANAGE_MESSAGES' permission to be present on the current user.
func (r *ChannelResource) RemoveAllReactions(ctx context.Context, messageID string) error {
e := endpoint.DeleteAllReactions(r.channelID, messageID)
resp, err := r.client.doReq(ctx, e, nil)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusNoContent {
return apiError(resp)
}
return nil
}
// RemoveAllReactionsForEmoji removes all reactions for the given emoji on a message. This endpoint requires
// the 'MANAGE_MESSAGES' permission to be present on the current user.
func (r *ChannelResource) RemoveAllReactionsForEmoji(ctx context.Context, messageID, emoji string) error {
e := endpoint.DeleteAllReactionsForEmoji(r.channelID, messageID, url.PathEscape(emoji))
resp, err := r.client.doReq(ctx, e, nil)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusNoContent {
return apiError(resp)
}
return nil
}
// Pins returns all pinned messages in the channel as an array of messages.
func (r *ChannelResource) Pins(ctx context.Context) ([]Message, error) {
e := endpoint.GetPinnedMessages(r.channelID)
resp, err := r.client.doReq(ctx, e, nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, apiError(resp)
}
var messages []Message
if err = json.NewDecoder(resp.Body).Decode(&messages); err != nil {
return nil, err
}
return messages, nil
}
// PinMessage pins a message in the channel. Requires the 'MANAGE_MESSAGES' permission.
func (r *ChannelResource) PinMessage(ctx context.Context, id string) error {
e := endpoint.AddPinnedChannelMessage(r.channelID, id)
resp, err := r.client.doReq(ctx, e, nil)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusNoContent {
return apiError(resp)
}
return nil
}
// UnpinMessage deletes a pinned message in the channel. Requires the
// 'MANAGE_MESSAGES' permission.
func (r *ChannelResource) UnpinMessage(ctx context.Context, id string) error {
e := endpoint.DeletePinnedChannelMessage(r.channelID, id)
resp, err := r.client.doReq(ctx, e, nil)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusNoContent {
return apiError(resp)
}
return nil
}