/
builder.go
381 lines (326 loc) · 9.62 KB
/
builder.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
package ui
import (
"fmt"
"regexp"
"strings"
"time"
"github.com/ayntgl/astatine"
"github.com/craftxbox/discordo/discord"
"github.com/rivo/tview"
)
func buildEdit(app *App, e *astatine.MessageUpdate) []byte {
var b strings.Builder
// Define a new region and assign message ID as the region ID.
// Learn more:
// https://pkg.go.dev/github.com/rivo/tview#hdr-Regions_and_Highlights
m := e
b.WriteString("[\"")
b.WriteString(m.ID)
b.WriteString("\"]")
// Build the message associated with crosspost, channel follow add, pin, or a reply.
buildReferencedMessage(&b, e.BeforeUpdate, app.Session.State.User.ID, app)
if app.Config.General.Timestamps {
b.WriteString("[::d]")
b.WriteString(m.Timestamp.Format(time.Stamp))
b.WriteString("[::-]")
b.WriteByte(' ')
}
b.WriteString(tview.Escape("[EDIT] "))
if e.BeforeUpdate == nil {
// Build the author of this message.
if m.Member == nil {
if len(m.GuildID) > 0 {
member := discord.GetMember(app.Session, m.GuildID, m.Author.ID)
buildAuthor(&b, m.Author, app.Session.State.User.ID, member, app)
} else {
c := app.SelectedChannel
if c != nil && len(c.GuildID) > 0 {
member := discord.GetMember(app.Session, c.GuildID, m.Author.ID)
buildAuthor(&b, m.Author, app.Session.State.User.ID, member, app)
} else {
buildAuthor(&b, m.Author, app.Session.State.User.ID, nil, app) // dm channel, probably.
}
}
} else {
buildAuthor(&b, m.Author, app.Session.State.User.ID, m.Member, app)
}
}
// Build the contents of the message.
buildContent(&b, m.Message, app.Session.State.User.ID)
// Build the embeds associated with the message.
buildEmbeds(&b, m.Embeds)
// Build the message attachments (attached files to the message).
buildAttachments(&b, m.Attachments)
// Tags with no region ID ([""]) do not start new regions. They can
// therefore be used to mark the end of a region.
b.WriteString("[\"\"]")
b.WriteByte('\n')
if str := b.String(); str != "" {
b := make([]byte, len(str)+1)
copy(b, str)
return b
}
return nil
}
func buildDelete(app *App, d *astatine.MessageDelete) []byte {
var b strings.Builder
if d.BeforeDelete != nil {
m := d.BeforeDelete
b.WriteString("[\"")
b.WriteString(m.ID)
b.WriteString("\"]")
buildReferencedMessage(&b, m, app.Session.State.User.ID, app)
if app.Config.General.Timestamps {
b.WriteString("[::d]")
b.WriteString(m.Timestamp.Format(time.Stamp))
b.WriteString("[::-]")
b.WriteByte(' ')
}
b.WriteString("Message was Deleted")
b.WriteString("[\"\"]")
b.WriteByte('\n')
}
if str := b.String(); str != "" {
b := make([]byte, len(str)+1)
copy(b, str)
return b
}
return nil
}
func buildMessage(app *App, m *astatine.Message) []byte {
var b strings.Builder
switch m.Type {
case astatine.MessageTypeDefault, astatine.MessageTypeReply:
// Define a new region and assign message ID as the region ID.
// Learn more:
// https://pkg.go.dev/github.com/rivo/tview#hdr-Regions_and_Highlights
b.WriteString("[\"")
b.WriteString(m.ID)
b.WriteString("\"]")
// Build the message associated with crosspost, channel follow add, pin, or a reply.
if m.Type == astatine.MessageTypeReply {
buildReferencedMessage(&b, m.ReferencedMessage, app.Session.State.User.ID, app)
}
if app.Config.General.Timestamps {
b.WriteString("[::d]")
b.WriteString(m.Timestamp.Format(time.Stamp))
b.WriteString("[::-]")
b.WriteByte(' ')
}
// Build the author of this message.
if m.Member == nil {
if len(m.GuildID) > 0 {
member := discord.GetMember(app.Session, m.GuildID, m.Author.ID)
buildAuthor(&b, m.Author, app.Session.State.User.ID, member, app)
} else {
c := app.SelectedChannel
if c != nil && len(c.GuildID) > 0 {
member := discord.GetMember(app.Session, c.GuildID, m.Author.ID)
buildAuthor(&b, m.Author, app.Session.State.User.ID, member, app)
} else {
buildAuthor(&b, m.Author, app.Session.State.User.ID, nil, app) // dm channel, probably.
}
}
} else {
buildAuthor(&b, m.Author, app.Session.State.User.ID, m.Member, app)
}
// Build the contents of the message.
buildContent(&b, m, app.Session.State.User.ID)
if m.EditedTimestamp != nil {
b.WriteString(" [::d](edited)[::-]")
}
// Build the embeds associated with the message.
buildEmbeds(&b, m.Embeds)
// Build the message attachments (attached files to the message).
buildAttachments(&b, m.Attachments)
// Tags with no region ID ([""]) do not start new regions. They can
// therefore be used to mark the end of a region.
b.WriteString("[\"\"]")
b.WriteByte('\n')
case astatine.MessageTypeGuildMemberJoin:
b.WriteString("[#5865F2]")
b.WriteString(m.Author.Username)
b.WriteString("[-] joined the server.")
b.WriteByte('\n')
case astatine.MessageTypeCall:
b.WriteString("[#5865F2]")
b.WriteString(m.Author.Username)
b.WriteString("[-] started a call.")
b.WriteByte('\n')
case astatine.MessageTypeChannelPinnedMessage:
b.WriteString("[#5865F2]")
b.WriteString(m.Author.Username)
b.WriteString("[-] pinned a message.")
b.WriteByte('\n')
}
if str := b.String(); str != "" {
b := make([]byte, len(str)+1)
copy(b, str)
return b
}
return nil
}
func buildReferencedMessage(b *strings.Builder, rm *astatine.Message, clientID string, app *App) {
if rm != nil {
b.WriteString(" ╭ ")
b.WriteString("[::d]")
if rm.Member == nil {
if len(rm.GuildID) > 0 {
member := discord.GetMember(app.Session, rm.GuildID, rm.Author.ID)
buildAuthor(b, rm.Author, clientID, member, app)
} else {
c := app.SelectedChannel
if c != nil && len(c.GuildID) > 0 {
member := discord.GetMember(app.Session, c.GuildID, rm.Author.ID)
buildAuthor(b, rm.Author, clientID, member, app)
} else {
buildAuthor(b, rm.Author, clientID, nil, app) // dm channel, probably.
}
}
} else {
buildAuthor(b, rm.Author, clientID, rm.Member, app)
}
if rm.Content != "" {
rm.Content = buildMentions(rm.Content, rm.Mentions, clientID)
var content string
if len(rm.Content) > 100 {
content = rm.Content[:100] + "..."
} else {
content = rm.Content
}
content = strings.ReplaceAll(content, "\n", " ")
b.WriteString(discord.ParseMarkdown(content))
}
b.WriteString("[::-]")
b.WriteByte('\n')
} else {
b.WriteString(" ╭ [::d]Original message deleted[::-]")
b.WriteByte('\n')
}
}
func buildContent(b *strings.Builder, m *astatine.Message, clientID string) {
if m.Content != "" {
content := buildMentions(m.Content, m.Mentions, clientID)
re := regexp.MustCompile(`\n{3,}`)
content = re.ReplaceAllString(content, "\n\n")
b.WriteString(discord.ParseMarkdown(content))
}
}
func buildEmbeds(b *strings.Builder, es []*astatine.MessageEmbed) {
for _, e := range es {
if e.Type != astatine.EmbedTypeRich {
continue
}
var (
embedBuilder strings.Builder
hasHeading bool
)
prefix := fmt.Sprintf("[#%06X]▐[-] ", e.Color)
b.WriteByte('\n')
embedBuilder.WriteString(prefix)
if e.Author != nil {
hasHeading = true
embedBuilder.WriteString("[::u]")
embedBuilder.WriteString(e.Author.Name)
embedBuilder.WriteString("[::-]")
}
if e.Title != "" {
if hasHeading {
embedBuilder.WriteByte('\n')
embedBuilder.WriteByte('\n')
}
embedBuilder.WriteString("[::b]")
embedBuilder.WriteString(e.Title)
embedBuilder.WriteString("[::-]")
}
if e.Description != "" {
if hasHeading {
embedBuilder.WriteByte('\n')
embedBuilder.WriteByte('\n')
}
embedBuilder.WriteString(discord.ParseMarkdown(e.Description))
}
if len(e.Fields) != 0 {
if hasHeading || e.Description != "" {
embedBuilder.WriteByte('\n')
embedBuilder.WriteByte('\n')
}
for i, ef := range e.Fields {
embedBuilder.WriteString("[::b]")
embedBuilder.WriteString(ef.Name)
embedBuilder.WriteString("[::-]")
embedBuilder.WriteByte('\n')
embedBuilder.WriteString(discord.ParseMarkdown(ef.Value))
if i != len(e.Fields)-1 {
embedBuilder.WriteString("\n\n")
}
}
}
if e.Footer != nil {
if hasHeading {
embedBuilder.WriteString("\n\n")
}
embedBuilder.WriteString(e.Footer.Text)
}
b.WriteString(strings.ReplaceAll(embedBuilder.String(), "\n", "\n"+prefix))
}
}
func buildAttachments(b *strings.Builder, as []*astatine.MessageAttachment) {
for _, a := range as {
b.WriteByte('\n')
b.WriteString("FILE: ")
b.WriteString(a.URL)
}
}
func buildMentions(content string, mentions []*astatine.User, clientID string) string {
for _, mUser := range mentions {
var color string
if mUser.ID == clientID {
color = "[:#FFA500]"
} else {
color = "[#EB459E]"
}
content = strings.NewReplacer(
// <@USER_ID>
"<@"+mUser.ID+">",
color+"@"+mUser.Username+"[-:-]",
// <@!USER_ID>
"<@!"+mUser.ID+">",
color+"@"+mUser.Username+"[-:-]",
).Replace(content)
}
return content
}
func buildAuthor(b *strings.Builder, u *astatine.User, clientID string, m *astatine.Member, app *App) {
if m != nil && len(m.Nick) > 0 {
b.WriteString("! ")
}
var gotRoleColor bool = false
/*if app != nil && m != nil && len(m.Roles) >= 1 {
//r, err := app.Session.State.Role(m.GuildID)
//if r != nil {
//TODO
//}
}*/
if !gotRoleColor {
if u.ID == clientID {
b.WriteString("[#57F287]")
} else {
b.WriteString("[#ED4245]")
}
}
if m != nil && len(m.Nick) > 0 {
b.WriteString(m.Nick)
} else {
b.WriteString(u.Username)
}
b.WriteString("[-] ")
if m != nil && m.CommunicationDisabledUntil != nil && m.CommunicationDisabledUntil.After(time.Now()) {
b.WriteString("[#FFFF00]MUTED[-] ")
}
// If the message author is a bot account, render the message with bot label
// for distinction.
if u.Bot {
b.WriteString("[#EB459E]BOT[-] ")
}
}