-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscord.go
55 lines (44 loc) · 1.39 KB
/
discord.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
package seabird_discord
import (
"strings"
"github.com/bwmarrin/discordgo"
"github.com/rs/zerolog"
)
// ComesFromDM returns true if a message comes from a DM channel
func ComesFromDM(s *discordgo.Session, m *discordgo.MessageCreate) (bool, error) {
channel, err := s.State.Channel(m.ChannelID)
if err != nil {
if channel, err = s.Channel(m.ChannelID); err != nil {
return false, err
}
}
return channel.Type == discordgo.ChannelTypeDM, nil
}
// ActionText tries to guess if a message was an action. It returns the action
// text without formatting and if it thinks the text was an action.
func ActionText(rawText string) (string, bool) {
text := strings.TrimPrefix(strings.TrimSuffix(rawText, "_"), "_")
if len(text) != len(rawText)-2 {
return text, false
}
return text, !strings.Contains(text, "_")
}
func ReplaceMentions(l zerolog.Logger, s *discordgo.Session, m *discordgo.Message) string {
rawText, err := m.ContentWithMoreMentionsReplaced(s)
if err != nil {
l.Warn().Err(err).Msg("failed to replace mentions, falling back to less agressive mentions")
return rawText
}
guild, err := s.State.Guild(m.GuildID)
if err != nil {
l.Warn().Err(err).Msg("failed to look up guild, skipping custom emoji")
return rawText
}
for _, emoji := range guild.Emojis {
rawText = strings.NewReplacer(
emoji.MessageFormat(),
":"+emoji.Name+":",
).Replace(rawText)
}
return rawText
}