-
Notifications
You must be signed in to change notification settings - Fork 927
/
mergedmessages.go
126 lines (106 loc) · 2.88 KB
/
mergedmessages.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
// Merged message sender sends all the messages in a queue, meged togheter at a interval
// To save on messages send in cases where there can potantially be many
// messages sent in a short interval (such as leave/join announcements with purges)
package bot
import (
"sync"
"time"
"github.com/jonas747/dcmd"
"github.com/jonas747/discordgo"
"github.com/jonas747/yagpdb/common"
)
var (
// map of channels and their message queue
mergedQueue = make(map[int64][]*QueuedMergedMessage)
mergedQueueLock sync.Mutex
)
type QueuedMergedMessage struct {
Content string
AllowedMentions discordgo.AllowedMentions
}
func QueueMergedMessage(channelID int64, message string, allowedMentions discordgo.AllowedMentions) {
mergedQueueLock.Lock()
defer mergedQueueLock.Unlock()
mergedQueue[channelID] = append(mergedQueue[channelID], &QueuedMergedMessage{Content: message, AllowedMentions: allowedMentions})
}
func mergedMessageSender() {
for {
mergedQueueLock.Lock()
for c, m := range mergedQueue {
go sendMergedBatch(c, m)
}
mergedQueue = make(map[int64][]*QueuedMergedMessage)
mergedQueueLock.Unlock()
time.Sleep(time.Second)
}
}
func sendMergedBatch(channelID int64, messages []*QueuedMergedMessage) {
out := ""
mergedAllowedMentions := discordgo.AllowedMentions{}
for _, v := range messages {
out += v.Content + "\n"
mergedAllowedMentions = mergeAllowedMentions(mergedAllowedMentions, v.AllowedMentions)
}
// Strip newline
out = out[:len(out)-1]
_, err := dcmd.SplitSendMessage(common.BotSession, channelID, out, mergedAllowedMentions)
if err != nil && !common.IsDiscordErr(err, discordgo.ErrCodeMissingAccess, discordgo.ErrCodeMissingPermissions) {
logger.WithError(err).WithField("message", out).Error("Error sending messages")
}
}
// mergeAllowedMentions merges 2 discordgo.AllowedMentions definitions into 1
func mergeAllowedMentions(a, b discordgo.AllowedMentions) discordgo.AllowedMentions {
// merge mention types
OUTER:
for _, v := range b.Parse {
for _, av := range a.Parse {
if v == av {
continue OUTER
}
}
a.Parse = append(a.Parse, v)
switch v {
case discordgo.AllowedMentionTypeUsers:
a.Users = nil
b.Users = nil
case discordgo.AllowedMentionTypeRoles:
a.Roles = nil
b.Roles = nil
}
}
hasParseRoles := false
hasParseUsers := false
for _, p := range a.Parse {
switch p {
case discordgo.AllowedMentionTypeRoles:
hasParseRoles = true
case discordgo.AllowedMentionTypeUsers:
hasParseUsers = true
}
}
// merge mentioned roles
if !hasParseRoles {
OUTER2:
for _, v := range b.Roles {
for _, av := range a.Roles {
if v == av {
continue OUTER2
}
}
a.Roles = append(a.Roles, v)
}
}
// merge mentioned users
if !hasParseUsers {
OUTER3:
for _, v := range b.Users {
for _, av := range a.Users {
if v == av {
continue OUTER3
}
}
a.Users = append(a.Users, v)
}
}
return a
}