forked from botlabs-gg/yagpdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mergedmessages.go
60 lines (48 loc) · 1.42 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
// 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/discordgo"
"github.com/jonas747/dutil"
"github.com/jonas747/yagpdb/common"
)
var (
// map of channels and their message queue
mergedQueue = make(map[int64][]string)
mergedQueueLock sync.Mutex
)
func QueueMergedMessage(channelID int64, message string) {
mergedQueueLock.Lock()
defer mergedQueueLock.Unlock()
if mergedQueue[channelID] == nil {
mergedQueue[channelID] = []string{message}
} else {
mergedQueue[channelID] = append(mergedQueue[channelID], message)
}
}
func mergedMessageSender() {
for {
mergedQueueLock.Lock()
for c, m := range mergedQueue {
go sendMergedBatch(c, m)
}
mergedQueue = make(map[int64][]string)
mergedQueueLock.Unlock()
time.Sleep(time.Second)
}
}
func sendMergedBatch(channelID int64, messages []string) {
out := ""
for _, v := range messages {
out += v + "\n"
}
// Strip newline
out = out[:len(out)-1]
_, err := dutil.SplitSendMessage(common.BotSession, channelID, out)
if err != nil && !common.IsDiscordErr(err, discordgo.ErrCodeMissingAccess, discordgo.ErrCodeMissingPermissions) {
logger.WithError(err).WithField("message", out).Error("Error sending messages")
}
}