-
Notifications
You must be signed in to change notification settings - Fork 35
/
message.go
478 lines (424 loc) · 16.6 KB
/
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
package discord
import (
"fmt"
"time"
"github.com/disgoorg/json"
"github.com/disgoorg/snowflake/v2"
"github.com/disgoorg/disgo/internal/flags"
)
// The MessageType indicates the Message type
type MessageType int
// Constants for the MessageType
const (
MessageTypeDefault MessageType = iota
MessageTypeRecipientAdd
MessageTypeRecipientRemove
MessageTypeCall
MessageTypeChannelNameChange
MessageTypeChannelIconChange
ChannelPinnedMessage
MessageTypeUserJoin
MessageTypeGuildBoost
MessageTypeGuildBoostTier1
MessageTypeGuildBoostTier2
MessageTypeGuildBoostTier3
MessageTypeChannelFollowAdd
_
MessageTypeGuildDiscoveryDisqualified
MessageTypeGuildDiscoveryRequalified
MessageTypeGuildDiscoveryGracePeriodInitialWarning
MessageTypeGuildDiscoveryGracePeriodFinalWarning
MessageTypeThreadCreated
MessageTypeReply
MessageTypeSlashCommand
MessageTypeThreadStarterMessage
MessageTypeGuildInviteReminder
MessageTypeContextMenuCommand
MessageTypeAutoModerationAction
MessageTypeRoleSubscriptionPurchase
MessageTypeInteractionPremiumUpsell
MessageTypeStageStart
MessageTypeStageEnd
MessageTypeStageSpeaker
_
MessageTypeStageTopic
MessageTypeGuildApplicationPremiumSubscription
)
func (t MessageType) System() bool {
switch t {
case MessageTypeDefault, MessageTypeReply, MessageTypeSlashCommand, MessageTypeThreadStarterMessage, MessageTypeContextMenuCommand:
return false
default:
return true
}
}
func (t MessageType) Deleteable() bool {
switch t {
case MessageTypeRecipientAdd, MessageTypeRecipientRemove, MessageTypeCall,
MessageTypeChannelNameChange, MessageTypeChannelIconChange, MessageTypeGuildDiscoveryDisqualified,
MessageTypeGuildDiscoveryRequalified, MessageTypeGuildDiscoveryGracePeriodInitialWarning,
MessageTypeGuildDiscoveryGracePeriodFinalWarning, MessageTypeThreadStarterMessage:
return false
default:
return true
}
}
const MessageURLFmt = "https://discord.com/channels/%s/%d/%d"
func MessageURL(guildID snowflake.ID, channelID snowflake.ID, messageID snowflake.ID) string {
return fmt.Sprintf(MessageURLFmt, guildID, channelID, messageID)
}
// Message is a struct for messages sent in discord text-based channels
type Message struct {
ID snowflake.ID `json:"id"`
GuildID *snowflake.ID `json:"guild_id"`
Reactions []MessageReaction `json:"reactions"`
Attachments []Attachment `json:"attachments"`
TTS bool `json:"tts"`
Embeds []Embed `json:"embeds,omitempty"`
Components []ContainerComponent `json:"components,omitempty"`
CreatedAt time.Time `json:"timestamp"`
Mentions []User `json:"mentions"`
MentionEveryone bool `json:"mention_everyone"`
MentionRoles []snowflake.ID `json:"mention_roles"`
MentionChannels []MentionChannel `json:"mention_channels"`
Pinned bool `json:"pinned"`
EditedTimestamp *time.Time `json:"edited_timestamp"`
Author User `json:"author"`
Member *Member `json:"member"`
Content string `json:"content,omitempty"`
ChannelID snowflake.ID `json:"channel_id"`
Type MessageType `json:"type"`
Flags MessageFlags `json:"flags"`
MessageReference *MessageReference `json:"message_reference,omitempty"`
Interaction *MessageInteraction `json:"interaction,omitempty"`
WebhookID *snowflake.ID `json:"webhook_id,omitempty"`
Activity *MessageActivity `json:"activity,omitempty"`
Application *MessageApplication `json:"application,omitempty"`
ApplicationID *snowflake.ID `json:"application_id,omitempty"`
StickerItems []MessageSticker `json:"sticker_items,omitempty"`
ReferencedMessage *Message `json:"referenced_message,omitempty"`
LastUpdated *time.Time `json:"last_updated,omitempty"`
Thread *MessageThread `json:"thread,omitempty"`
Position *int `json:"position,omitempty"`
RoleSubscriptionData *RoleSubscriptionData `json:"role_subscription_data,omitempty"`
InteractionMetadata *InteractionMetadata `json:"interaction_metadata,omitempty"`
Resolved *ResolvedData `json:"resolved,omitempty"`
Poll *Poll `json:"poll,omitempty"`
}
func (m *Message) UnmarshalJSON(data []byte) error {
type message Message
var v struct {
Components []UnmarshalComponent `json:"components"`
message
}
if err := json.Unmarshal(data, &v); err != nil {
return err
}
*m = Message(v.message)
if len(v.Components) > 0 {
m.Components = make([]ContainerComponent, len(v.Components))
for i := range v.Components {
m.Components[i] = v.Components[i].Component.(ContainerComponent)
}
}
if m.Member != nil && m.GuildID != nil {
m.Member.GuildID = *m.GuildID
}
return nil
}
// ActionRows returns all ActionRowComponent(s) from this Message
func (m Message) ActionRows() []ActionRowComponent {
var actionRows []ActionRowComponent
for i := range m.Components {
if actionRow, ok := m.Components[i].(ActionRowComponent); ok {
actionRows = append(actionRows, actionRow)
}
}
return actionRows
}
// InteractiveComponents returns the InteractiveComponent(s) from this Message
func (m Message) InteractiveComponents() []InteractiveComponent {
var interactiveComponents []InteractiveComponent
for i := range m.Components {
for ii := range m.Components[i].Components() {
interactiveComponents = append(interactiveComponents, m.Components[i].Components()[ii])
}
}
return interactiveComponents
}
// ComponentByID returns the Component with the specific CustomID
func (m Message) ComponentByID(customID string) InteractiveComponent {
for i := range m.Components {
for ii := range m.Components[i].Components() {
if m.Components[i].Components()[ii].ID() == customID {
return m.Components[i].Components()[ii]
}
}
}
return nil
}
// Buttons returns all ButtonComponent(s) from this Message
func (m Message) Buttons() []ButtonComponent {
var buttons []ButtonComponent
for i := range m.Components {
for ii := range m.Components[i].Components() {
if button, ok := m.Components[i].Components()[ii].(ButtonComponent); ok {
buttons = append(buttons, button)
}
}
}
return buttons
}
// ButtonByID returns a ButtonComponent with the specific customID from this Message
func (m Message) ButtonByID(customID string) (ButtonComponent, bool) {
for i := range m.Components {
for ii := range m.Components[i].Components() {
if button, ok := m.Components[i].Components()[ii].(ButtonComponent); ok && button.ID() == customID {
return button, true
}
}
}
return ButtonComponent{}, false
}
// SelectMenus returns all SelectMenuComponent(s) from this Message
func (m Message) SelectMenus() []SelectMenuComponent {
var selectMenus []SelectMenuComponent
for i := range m.Components {
for ii := range m.Components[i].Components() {
if selectMenu, ok := m.Components[i].Components()[ii].(SelectMenuComponent); ok {
selectMenus = append(selectMenus, selectMenu)
}
}
}
return selectMenus
}
// SelectMenuByID returns a SelectMenuComponent with the specific customID from this Message
func (m Message) SelectMenuByID(customID string) (SelectMenuComponent, bool) {
for i := range m.Components {
for ii := range m.Components[i].Components() {
if selectMenu, ok := m.Components[i].Components()[ii].(SelectMenuComponent); ok && selectMenu.ID() == customID {
return selectMenu, true
}
}
}
return nil, false
}
// UserSelectMenus returns all UserSelectMenuComponent(s) from this Message
func (m Message) UserSelectMenus() []UserSelectMenuComponent {
var userSelectMenus []UserSelectMenuComponent
for i := range m.Components {
for ii := range m.Components[i].Components() {
if userSelectMenu, ok := m.Components[i].Components()[ii].(UserSelectMenuComponent); ok {
userSelectMenus = append(userSelectMenus, userSelectMenu)
}
}
}
return userSelectMenus
}
// UserSelectMenuByID returns a UserSelectMenuComponent with the specific customID from this Message
func (m Message) UserSelectMenuByID(customID string) (UserSelectMenuComponent, bool) {
for i := range m.Components {
for ii := range m.Components[i].Components() {
if userSelectMenu, ok := m.Components[i].Components()[ii].(UserSelectMenuComponent); ok && userSelectMenu.ID() == customID {
return userSelectMenu, true
}
}
}
return UserSelectMenuComponent{}, false
}
// RoleSelectMenus returns all RoleSelectMenuComponent(s) from this Message
func (m Message) RoleSelectMenus() []RoleSelectMenuComponent {
var roleSelectMenus []RoleSelectMenuComponent
for i := range m.Components {
for ii := range m.Components[i].Components() {
if roleSelectMenu, ok := m.Components[i].Components()[ii].(RoleSelectMenuComponent); ok {
roleSelectMenus = append(roleSelectMenus, roleSelectMenu)
}
}
}
return roleSelectMenus
}
// RoleSelectMenuByID returns a RoleSelectMenuComponent with the specific customID from this Message
func (m Message) RoleSelectMenuByID(customID string) (RoleSelectMenuComponent, bool) {
for i := range m.Components {
for ii := range m.Components[i].Components() {
if roleSelectMenu, ok := m.Components[i].Components()[ii].(RoleSelectMenuComponent); ok && roleSelectMenu.ID() == customID {
return roleSelectMenu, true
}
}
}
return RoleSelectMenuComponent{}, false
}
// MentionableSelectMenus returns all MentionableSelectMenuComponent(s) from this Message
func (m Message) MentionableSelectMenus() []MentionableSelectMenuComponent {
var mentionableSelectMenus []MentionableSelectMenuComponent
for i := range m.Components {
for ii := range m.Components[i].Components() {
if mentionableSelectMenu, ok := m.Components[i].Components()[ii].(MentionableSelectMenuComponent); ok {
mentionableSelectMenus = append(mentionableSelectMenus, mentionableSelectMenu)
}
}
}
return mentionableSelectMenus
}
// MentionableSelectMenuByID returns a MentionableSelectMenuComponent with the specific customID from this Message
func (m Message) MentionableSelectMenuByID(customID string) (MentionableSelectMenuComponent, bool) {
for i := range m.Components {
for ii := range m.Components[i].Components() {
if mentionableSelectMenu, ok := m.Components[i].Components()[ii].(MentionableSelectMenuComponent); ok && mentionableSelectMenu.ID() == customID {
return mentionableSelectMenu, true
}
}
}
return MentionableSelectMenuComponent{}, false
}
// ChannelSelectMenus returns all ChannelSelectMenuComponent(s) from this Message
func (m Message) ChannelSelectMenus() []ChannelSelectMenuComponent {
var channelSelectMenus []ChannelSelectMenuComponent
for i := range m.Components {
for ii := range m.Components[i].Components() {
if channelSelectMenu, ok := m.Components[i].Components()[ii].(ChannelSelectMenuComponent); ok {
channelSelectMenus = append(channelSelectMenus, channelSelectMenu)
}
}
}
return channelSelectMenus
}
// ChannelSelectMenuByID returns a ChannelSelectMenuComponent with the specific customID from this Message
func (m Message) ChannelSelectMenuByID(customID string) (ChannelSelectMenuComponent, bool) {
for i := range m.Components {
for ii := range m.Components[i].Components() {
if channelSelectMenu, ok := m.Components[i].Components()[ii].(ChannelSelectMenuComponent); ok && channelSelectMenu.ID() == customID {
return channelSelectMenu, true
}
}
}
return ChannelSelectMenuComponent{}, false
}
func (m Message) JumpURL() string {
guildID := "@me"
if m.GuildID != nil {
guildID = m.GuildID.String()
}
return fmt.Sprintf(MessageURLFmt, guildID, m.ChannelID, m.ID) // duplicate code, but there isn't a better way without sacrificing user convenience
}
type MentionChannel struct {
ID snowflake.ID `json:"id"`
GuildID snowflake.ID `json:"guild_id"`
Type ChannelType `json:"type"`
Name string `json:"name"`
}
type MessageThread struct {
GuildThread
Member ThreadMember `json:"member"`
}
type MessageSticker struct {
ID snowflake.ID `json:"id"`
Name string `json:"name"`
FormatType StickerFormatType `json:"format_type"`
}
// MessageReaction contains information about the reactions of a message
type MessageReaction struct {
Count int `json:"count"`
CountDetails ReactionCountDetails `json:"count_details"`
Me bool `json:"me"`
MeBurst bool `json:"me_burst"`
Emoji Emoji `json:"emoji"`
BurstColors []string `json:"burst_colors"`
}
type ReactionCountDetails struct {
Burst int `json:"burst"`
Normal int `json:"normal"`
}
// MessageActivityType is the type of MessageActivity https://com/developers/docs/resources/channel#message-object-message-activity-types
type MessageActivityType int
// Constants for MessageActivityType
const (
MessageActivityTypeJoin MessageActivityType = iota + 1
MessageActivityTypeSpectate
MessageActivityTypeListen
_
MessageActivityTypeJoinRequest
)
// MessageActivity is used for rich presence-related chat embeds in a Message
type MessageActivity struct {
Type MessageActivityType `json:"type"`
PartyID *string `json:"party_id,omitempty"`
}
// MessageApplication is used for rich presence-related chat embeds in a Message
type MessageApplication struct {
ID snowflake.ID `json:"id"`
CoverImage *string `json:"cover_image,omitempty"`
Description string `json:"description"`
Icon *string `json:"icon,omitempty"`
Name string `json:"name"`
}
// MessageReference is a reference to another message
type MessageReference struct {
MessageID *snowflake.ID `json:"message_id"`
ChannelID *snowflake.ID `json:"channel_id,omitempty"`
GuildID *snowflake.ID `json:"guild_id,omitempty"`
FailIfNotExists bool `json:"fail_if_not_exists,omitempty"`
}
// MessageInteraction is sent on the Message object when the message is a response to an interaction
type MessageInteraction struct {
ID snowflake.ID `json:"id"`
Type InteractionType `json:"type"`
Name string `json:"name"`
User User `json:"user"`
}
type MessageBulkDelete struct {
Messages []snowflake.ID `json:"messages"`
}
// The MessageFlags of a Message
type MessageFlags int
// Constants for MessageFlags
const (
MessageFlagCrossposted MessageFlags = 1 << iota
MessageFlagIsCrosspost
MessageFlagSuppressEmbeds
MessageFlagSourceMessageDeleted
MessageFlagUrgent
MessageFlagHasThread
MessageFlagEphemeral
MessageFlagLoading // Message is an interaction of type 5, awaiting further response
MessageFlagFailedToMentionSomeRolesInThread
_
_
_
MessageFlagSuppressNotifications
MessageFlagIsVoiceMessage
MessageFlagsNone MessageFlags = 0
)
// Add allows you to add multiple bits together, producing a new bit
func (f MessageFlags) Add(bits ...MessageFlags) MessageFlags {
return flags.Add(f, bits...)
}
// Remove allows you to subtract multiple bits from the first, producing a new bit
func (f MessageFlags) Remove(bits ...MessageFlags) MessageFlags {
return flags.Remove(f, bits...)
}
// Has will ensure that the bit includes all the bits entered
func (f MessageFlags) Has(bits ...MessageFlags) bool {
return flags.Has(f, bits...)
}
// Missing will check whether the bit is missing any one of the bits
func (f MessageFlags) Missing(bits ...MessageFlags) bool {
return flags.Missing(f, bits...)
}
type RoleSubscriptionData struct {
RoleSubscriptionListingID snowflake.ID `json:"role_subscription_listing_id"`
TierName string `json:"tier_name"`
TotalMonthsSubscribed int `json:"total_months_subscribed"`
IsRenewal bool `json:"is_renewal"`
}
type InteractionMetadata struct {
ID snowflake.ID `json:"id"`
Type InteractionType `json:"type"`
UserID snowflake.ID `json:"user_id"`
AuthorizingIntegrationOwners map[ApplicationIntegrationType]snowflake.ID `json:"authorizing_integration_owners"`
OriginalResponseMessageID *snowflake.ID `json:"original_response_message_id"`
Name *string `json:"name"`
InteractedMessageID *snowflake.ID `json:"interacted_message_id"`
TriggeringInteractionMetadata *InteractionMetadata `json:"triggering_interaction_metadata"`
}