This repository has been archived by the owner on Feb 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.go
370 lines (335 loc) · 14.3 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
package dhelpers
import (
"io"
"strings"
"gitlab.com/Cacophony/dhelpers/cache"
"github.com/bwmarrin/discordgo"
)
// SendMessage sends a message to a specific channel, takes care of splitting and sanitising the content, the event variable is being set
func (event EventContainer) SendMessage(channelID, content string) (messages []*discordgo.Message, err error) {
return SendMessagefWithBot(event.BotUserID, channelID, content, "event", event)
}
// SendMessageWithBot sends a message to a specific channel, takes care of splitting and sanitising the content
func SendMessageWithBot(botID, channelID, content string) (messages []*discordgo.Message, err error) {
return SendMessagefWithBot(botID, channelID, content)
}
// SendMessagef sends a message to a specific channel, takes care of splitting and sanitising the content, and replacing the fields, the event variable is being set
func (event EventContainer) SendMessagef(channelID, content string, fields ...interface{}) (messages []*discordgo.Message, err error) {
return SendMessagefWithBot(event.BotUserID, channelID, content, append(fields, "event", event)...)
}
// SendMessagefWithBot sends a message to a specific channel, takes care of splitting and sanitising the content, and replacing the fields
func SendMessagefWithBot(botID, channelID, content string, fields ...interface{}) (messages []*discordgo.Message, err error) {
var message *discordgo.Message
content = cleanDiscordContent(Tf(content, fields...))
if len(content) > 2000 {
for _, page := range autoPagify(content) {
message, err = cache.GetEDiscord(botID).ChannelMessageSend(channelID, page)
if err != nil {
return messages, err
}
messages = append(messages, message)
}
} else {
message, err = cache.GetEDiscord(botID).ChannelMessageSend(channelID, content)
if err != nil {
return messages, err
}
messages = append(messages, message)
}
return messages, nil
}
// SendMessagefc sends a message to a specific channel, takes care of splitting and sanitising the content, and replacing the fields, and applying pluralization, the event variable is being set
func (event EventContainer) SendMessagefc(channelID, content string, count int, fields ...interface{}) (messages []*discordgo.Message, err error) {
return SendMessagefcWithBot(event.BotUserID, channelID, content, count, append(fields, "event", event)...)
}
// SendMessagefcWithBot sends a message to a specific channel, takes care of splitting and sanitising the content, and replacing the fields, and applying pluralization
func SendMessagefcWithBot(botID, channelID, content string, count int, fields ...interface{}) (messages []*discordgo.Message, err error) {
var message *discordgo.Message
content = cleanDiscordContent(Tfc(content, count, fields...))
if len(content) > 2000 {
for _, page := range autoPagify(content) {
message, err = cache.GetEDiscord(botID).ChannelMessageSend(channelID, page)
if err != nil {
return messages, err
}
messages = append(messages, message)
}
} else {
message, err = cache.GetEDiscord(botID).ChannelMessageSend(channelID, content)
if err != nil {
return messages, err
}
messages = append(messages, message)
}
return messages, nil
}
// SendMessageBoxed sends a message to a specific channel, will put a box around it, takes care of splitting and sanitising the content, the event variable is being set
func (event EventContainer) SendMessageBoxed(channelID, content string) (messages []*discordgo.Message, err error) {
return SendMessageBoxedfWithBot(event.BotUserID, channelID, content, "event", event)
}
// SendMessageBoxedWithBot sends a message to a specific channel, will put a box around it, takes care of splitting and sanitising the content
func SendMessageBoxedWithBot(botID, channelID, content string) (messages []*discordgo.Message, err error) {
return SendMessageBoxedfWithBot(botID, channelID, content)
}
// SendMessageBoxedfWithBot sends a message to a specific channel, will put a box around it, takes care of splitting and sanitising the content, and replacing the fields
func SendMessageBoxedfWithBot(botID, channelID, content string, fields ...interface{}) (messages []*discordgo.Message, err error) {
var newMessages []*discordgo.Message
content = cleanDiscordContent(Tf(content, fields))
for _, page := range autoPagify(content) {
newMessages, err = SendMessageWithBot(botID, channelID, "```"+page+"```")
if err != nil {
return messages, err
}
messages = append(messages, newMessages...)
}
return messages, nil
}
// SendEmbed sends an embed to a specific channel, takes care of splitting and sanitising the content
func (event EventContainer) SendEmbed(channelID string, embed *discordgo.MessageEmbed) (messages []*discordgo.Message, err error) {
return SendEmbedWithBot(event.BotUserID, channelID, embed)
}
// SendEmbedWithBot sends an embed to a specific channel, takes care of splitting and sanitising the content
func SendEmbedWithBot(botID, channelID string, embed *discordgo.MessageEmbed) (messages []*discordgo.Message, err error) {
var message *discordgo.Message
message, err = cache.GetEDiscord(botID).ChannelMessageSendEmbed(channelID, truncateEmbed(embed))
if err != nil {
return messages, err
}
messages = append(messages, message)
return messages, nil
}
// SendFile sends a file to a specific channel, takes care of splitting and sanitising the content
func (event EventContainer) SendFile(channelID string, filename string, reader io.Reader, message string) (messages []*discordgo.Message, err error) {
return SendFileWithBot(event.BotUserID, channelID, filename, reader, message)
}
// SendFileWithBot sends a file to a specific channel, takes care of splitting and sanitising the content
func SendFileWithBot(botID, channelID string, filename string, reader io.Reader, message string) (messages []*discordgo.Message, err error) {
return SendComplexWithBot(botID, channelID, &discordgo.MessageSend{File: &discordgo.File{Name: filename, Reader: reader}, Content: message})
}
// SendComplex sends a discordgo.MessageSend object to a specific channel, takes care of splitting and sanitising the content
func (event EventContainer) SendComplex(channelID string, data *discordgo.MessageSend) (messages []*discordgo.Message, err error) {
return SendComplexWithBot(event.BotUserID, channelID, data)
}
// SendComplexWithBot sends a discordgo.MessageSend object to a specific channel, takes care of splitting and sanitising the content
func SendComplexWithBot(botID, channelID string, data *discordgo.MessageSend) (messages []*discordgo.Message, err error) {
var message *discordgo.Message
if data.Embed != nil {
data.Embed = truncateEmbed(data.Embed)
}
data.Content = cleanDiscordContent(data.Content)
pages := autoPagify(data.Content)
if len(pages) > 0 {
for i, page := range pages {
if i+1 < len(pages) {
message, err = cache.GetEDiscord(botID).ChannelMessageSend(channelID, page)
} else {
data.Content = page
message, err = cache.GetEDiscord(botID).ChannelMessageSendComplex(channelID, data)
}
if err != nil {
return messages, err
}
messages = append(messages, message)
}
} else {
message, err = cache.GetEDiscord(botID).ChannelMessageSendComplex(channelID, data)
if err != nil {
return messages, err
}
messages = append(messages, message)
}
return messages, nil
}
// EditMessage edits a specific message, takes care of sanitising the content
func (event EventContainer) EditMessage(channelID, messageID, content string) (message *discordgo.Message, err error) {
return EditMessagefWithBot(event.BotUserID, channelID, messageID, content, "event", event)
}
// EditMessageWithBot edits a specific message, takes care of sanitising the content
func EditMessageWithBot(botID, channelID, messageID, content string) (message *discordgo.Message, err error) {
return EditMessagefWithBot(botID, channelID, messageID, content)
}
// EditMessagef edits a specific message, takes care of sanitising the content, and replacing the fields, the event variable is being set
func (event EventContainer) EditMessagef(channelID, messageID, content string, fields ...interface{}) (message *discordgo.Message, err error) {
return EditMessagefWithBot(event.BotUserID, channelID, messageID, content, append(fields, "event", event)...)
}
// EditMessagefWithBot edits a specific message, takes care of sanitising the content, and replacing the fields
func EditMessagefWithBot(botID, channelID, messageID, content string, fields ...interface{}) (message *discordgo.Message, err error) {
content = cleanDiscordContent(Tf(content, fields...))
message, err = cache.GetEDiscord(botID).ChannelMessageEdit(channelID, messageID, content)
if err != nil {
return nil, err
}
return message, err
}
// EditMessagefc edits a specific message, takes care of sanitising the content, and replacing the fields, and applying pluralization, the event variable is being set
func (event EventContainer) EditMessagefc(channelID, messageID, content string, count int, fields ...interface{}) (message *discordgo.Message, err error) {
return EditMessagefcWithBot(event.BotUserID, channelID, messageID, content, count, append(fields, "event", event)...)
}
// EditMessagefcWithBot edits a specific message, takes care of sanitising the content, and replacing the fields, and applying pluralization
func EditMessagefcWithBot(botID, channelID, messageID, content string, count int, fields ...interface{}) (message *discordgo.Message, err error) {
content = cleanDiscordContent(Tfc(content, count, fields...))
message, err = cache.GetEDiscord(botID).ChannelMessageEdit(channelID, messageID, content)
if err != nil {
return nil, err
}
return message, err
}
// EditEmbed edits a specific embed, takes care of sanitising the content
func (event EventContainer) EditEmbed(channelID, messageID string, embed *discordgo.MessageEmbed) (message *discordgo.Message, err error) {
return EditEmbedWithBot(event.BotUserID, channelID, messageID, embed)
}
// EditEmbedWithBot edits a specific embed, takes care of sanitising the content
func EditEmbedWithBot(botID, channelID, messageID string, embed *discordgo.MessageEmbed) (message *discordgo.Message, err error) {
message, err = cache.GetEDiscord(botID).ChannelMessageEditEmbed(channelID, messageID, truncateEmbed(embed))
if err != nil {
return nil, err
}
return message, err
}
// EditComplex edits a specific message using a discordgo.MessageEdit object, takes care of sanitising the content
func (event EventContainer) EditComplex(data *discordgo.MessageEdit) (message *discordgo.Message, err error) {
return EditComplexWithBot(event.BotUserID, data)
}
// EditComplexWithBot edits a specific message using a discordgo.MessageEdit object, takes care of sanitising the content
func EditComplexWithBot(botID string, data *discordgo.MessageEdit) (message *discordgo.Message, err error) {
if data.Embed != nil {
data.Embed = truncateEmbed(data.Embed)
}
if data.Content != nil {
content := cleanDiscordContent(*data.Content)
data.Content = &content
}
message, err = cache.GetEDiscord(botID).ChannelMessageEditComplex(data)
if err != nil {
return nil, err
}
return message, err
}
func strictPagify(text string, delimiter string) []string {
result := make([]string, 0)
textParts := strings.Split(text, delimiter)
currentOutputPart := ""
for _, textPart := range textParts {
if len(currentOutputPart)+len(textPart)+len(delimiter) <= 1992 {
if len(currentOutputPart) > 0 || len(result) > 0 {
currentOutputPart += delimiter + textPart
} else {
currentOutputPart += textPart
}
} else {
result = append(result, currentOutputPart)
currentOutputPart = ""
if len(textPart) <= 1992 {
currentOutputPart = textPart
}
}
}
if currentOutputPart != "" {
result = append(result, currentOutputPart)
}
return result
}
func autoPagify(text string) (pages []string) {
for _, page := range strictPagify(text, "\n") {
if len(page) <= 1992 {
pages = append(pages, page)
} else {
for _, page := range strictPagify(page, ",") {
if len(page) <= 1992 {
pages = append(pages, page)
} else {
for _, page := range strictPagify(page, "-") {
if len(page) <= 1992 {
pages = append(pages, page)
} else {
for _, page := range strictPagify(page, " ") {
if len(page) <= 1992 {
pages = append(pages, page)
} else {
panic("unable to pagify text")
}
}
}
}
}
}
}
}
return pages
}
func cleanDiscordContent(content string) string {
return strings.Replace(
strings.Replace(
content,
"@everyone", "@"+ZeroWidthSpace+"everyone", -1,
),
"@here", "@"+ZeroWidthSpace+"here", -1,
)
}
// Applies Embed Limits to the given Embed
// Source: https://discordapp.com/developers/docs/resources/channel#embed-limits
func truncateEmbed(embed *discordgo.MessageEmbed) (result *discordgo.MessageEmbed) {
if embed == nil || (&discordgo.MessageEmbed{}) == embed {
return nil
}
if embed.Title != "" && len(embed.Title) > 256 {
embed.Title = embed.Title[0:255] + "…"
}
if len(embed.Description) > 2048 {
embed.Description = embed.Description[0:2047] + "…"
}
if embed.Footer != nil && len(embed.Footer.Text) > 2048 {
embed.Footer.Text = embed.Footer.Text[0:2047] + "…"
}
if embed.Author != nil && len(embed.Author.Name) > 256 {
embed.Author.Name = embed.Author.Name[0:255] + "…"
}
newFields := make([]*discordgo.MessageEmbedField, 0)
for _, field := range embed.Fields {
if field.Value == "" {
continue
}
if len(field.Name) > 256 {
field.Name = field.Name[0:255] + "…"
}
// TODO: better cutoff (at commas and stuff)
if len(field.Value) > 1024 {
field.Value = field.Value[0:1023] + "…"
}
newFields = append(newFields, field)
if len(newFields) >= 25 {
break
}
}
embed.Fields = newFields
if calculateFullEmbedLength(embed) > 6000 {
if embed.Footer != nil {
embed.Footer.Text = ""
}
if calculateFullEmbedLength(embed) > 6000 {
if embed.Author != nil {
embed.Author.Name = ""
}
if calculateFullEmbedLength(embed) > 6000 {
embed.Fields = []*discordgo.MessageEmbedField{{}}
}
}
}
result = embed
return result
}
func calculateFullEmbedLength(embed *discordgo.MessageEmbed) (count int) {
count += len(embed.Title)
count += len(embed.Description)
if embed.Footer != nil {
count += len(embed.Footer.Text)
}
if embed.Author != nil {
count += len(embed.Author.Name)
}
for _, field := range embed.Fields {
count += len(field.Name)
count += len(field.Value)
}
return count
}