This repository has been archived by the owner on Sep 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
parsementions.go
102 lines (82 loc) · 2.21 KB
/
parsementions.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
package main
import (
"fmt"
"regexp"
"strconv"
"strings"
"github.com/diamondburned/discordgo"
"github.com/diamondburned/tview/v2"
"gitlab.com/diamondburned/6cord/md"
)
var (
patternChannels = regexp.MustCompile("<#[^>]*>")
)
// ParseMentionsFallback parses mentions into strings without failing
func ParseMentionsFallback(m *discordgo.Message) (content string) {
content = md.Parse(m.Content)
for _, user := range m.Mentions {
var username = tview.Escape(user.Username)
content = strings.NewReplacer(
// <@ID>
fmt.Sprintf("<@%d>", user.ID),
"[:blue:]@"+username+"[:-:]",
// <@!ID>
fmt.Sprintf("<@!%d>", user.ID),
"[:blue:]@"+username+"[:-:]",
).Replace(content)
}
return
}
func parseMessageContent(m *discordgo.Message) (content string, emojiMap map[string][]string) {
channel, err := d.State.Channel(m.ChannelID)
if err != nil {
content = ParseMentionsFallback(m)
return
}
_c, emojiMap := parseEmojis(m.Content)
content = md.Parse(_c)
for _, user := range m.Mentions {
var username = tview.Escape(user.Username)
member, err := d.State.Member(channel.GuildID, user.ID)
if err == nil && member.Nick != "" {
username = tview.Escape(member.Nick)
}
var color = "[-:" + cfg.Prop.MentionColor + ":-]"
if user.ID == d.State.User.ID {
color = "[-:" + cfg.Prop.MentionSelfColor + ":-]"
}
content = strings.NewReplacer(
// <@ID>
fmt.Sprintf("<@%d>", user.ID),
color+"@"+username+"[:-:]",
// <@!ID>
fmt.Sprintf("<@!%d>", user.ID),
color+"@"+username+"[:-:]",
).Replace(content)
}
var color = "[-:" + cfg.Prop.MentionColor + ":-]"
for _, roleID := range m.MentionRoles {
role, err := d.State.Role(channel.GuildID, roleID)
if err != nil {
continue
}
content = strings.Replace(
content,
fmt.Sprintf("<@&%d>", role.ID),
color+"@"+role.Name+"[:-:]",
1,
)
}
content = patternChannels.ReplaceAllStringFunc(content, func(mention string) string {
id, err := strconv.ParseInt(mention[2:len(mention)-1], 10, 64)
if err != nil {
return mention
}
channel, err := d.State.Channel(id)
if err != nil || channel.Type == discordgo.ChannelTypeGuildVoice {
return mention
}
return color + "#" + channel.Name + "[:-:]"
})
return
}