-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdiscordMsg.go
More file actions
172 lines (144 loc) · 4.85 KB
/
discordMsg.go
File metadata and controls
172 lines (144 loc) · 4.85 KB
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
package main
import (
"ChatWire/cfg"
"ChatWire/cwlog"
"ChatWire/disc"
"ChatWire/fact"
"ChatWire/glob"
"ChatWire/sclean"
"ChatWire/support"
"fmt"
"regexp"
"strings"
"github.com/bwmarrin/discordgo"
)
// Protect against spam
func handleDiscordMessages(s *discordgo.Session, m *discordgo.MessageCreate) {
message, _ := m.ContentWithMoreMentionsReplaced(s)
message = sclean.UnicodeCleanup(message)
messageHandlerLock.Lock()
defer messageHandlerLock.Unlock()
/* Protect players from dumb mistakes with registration codes, even on other maps */
/* Do this before we reject bot messages, to catch factorio chat on different maps/channels */
if support.ProtectIdiots(message) {
replyChan := m.ChannelID
/* If they manage to post it into chat in Factorio on a different server,
the message will be seen in discord but not factorio... eh whatever it still gets invalidated */
buf := "You are supposed to type that into Factorio, not Discord... Invalidating code. Please read the directions more carefully..."
embed := &discordgo.MessageEmbed{Title: "WARNING!!!", Description: m.Author.Username + ": " + buf, Color: glob.COLOR_RED}
disc.SmartWriteDiscordEmbed(replyChan, embed)
/* Delete message if possible */
err := s.ChannelMessageDelete(replyChan, m.ID)
if err != nil {
cwlog.DoLogCW(err.Error())
}
}
/* Ignore messages from self */
if m.Author.ID == s.State.User.ID {
return
}
/* Throw away messages from bots */
if m.Author.Bot {
return
}
//Show attachments
if len(m.Attachments) > 0 {
message = message + "Attachments: "
}
urls := ""
for _, item := range m.Attachments {
if item.URL != "" {
if urls != "" {
urls = urls + ", "
}
urls = urls + item.ContentType + ": " + item.URL
}
}
message = message + urls
//Show stickers
stickers := ""
if len(m.StickerItems) > 0 {
message = message + "Stickers: "
}
for _, item := range m.StickerItems {
if item.Name != "" {
if stickers != "" {
stickers = stickers + ", "
}
stickers = stickers + item.Name
}
}
message = message + stickers
if message == "" {
return
}
//Limit size
if len(message) > 500 {
message = fmt.Sprintf("%s(cut, too long!)", sclean.TruncateStringEllipsis(message, 500))
}
//Kill continuity.
glob.SetBootMessage(nil)
glob.ResetUpdateMessage()
/* Factorio channel ONLY */
if strings.EqualFold(cfg.Local.Channel.ChatChannel, m.ChannelID) && cfg.Local.Channel.ChatChannel != "" {
/* Used for name matching */
alphafilter, _ := regexp.Compile("[^a-zA-Z]+")
/* Remove control characters and discord markdown */
cleanedMessage := sclean.RemoveDiscordMarkdown(message)
/* Try to find factorio name, for registered players */
factName := disc.GetFactorioNameFromDiscordID(m.Author.ID)
/* Name to lowercase */
factNameLower := strings.ToLower(factName)
discNameLower := strings.ToLower(m.Author.GlobalName)
/* Reduce names to letters only */
factNameReduced := alphafilter.ReplaceAllString(factNameLower, "")
discNameReduced := alphafilter.ReplaceAllString(discNameLower, "")
/* Mark as seen, async */
go func(factname string) {
fact.UpdateSeen(factname)
}(factName)
/* Filter names... */
discordName := sclean.UnicodeCleanup(m.Author.GlobalName)
factorioName := sclean.UnicodeCleanup(factName)
/* Just in case of weird nickname */
discordNameLen := len(discordName)
if discordNameLen < 2 {
discordName = m.Author.Username
}
/* Just in case of weird username */
discordNameLen = len(discordName)
if discordNameLen < 2 {
discordName = fmt.Sprintf("ID#%v", m.Member.User.ID)
}
/* Cap name length for safety/annoyance */
discordName = sclean.TruncateString(discordName, 64)
factorioName = sclean.TruncateString(factorioName, 64)
namePrefix := ""
interaction := &discordgo.InteractionCreate{Interaction: &discordgo.Interaction{Member: m.Member}}
if disc.CheckAdmin(interaction) {
namePrefix = "(Admin) "
} else if disc.CheckModerator(interaction) {
namePrefix = "(Moderator) "
} else if disc.IsPatreon(m.Author.ID) || disc.CheckSupporter(interaction) {
namePrefix = "(Supporter) "
} else if disc.CheckVeteran(interaction) {
namePrefix = "(Veteran) "
} else if disc.CheckRegular(interaction) {
namePrefix = "(Regular) "
} else if disc.CheckMember(interaction) {
namePrefix = "(Member) "
}
/* Check if Discord name contains Factorio name, if not lets show both their names */
output := ""
if factName != "" && !strings.Contains(factNameReduced, discNameReduced) && !strings.Contains(discNameReduced, factNameReduced) {
output = fmt.Sprintf("[Discord] %v%v(%v): %v", namePrefix, discordName, factorioName, cleanedMessage)
} else {
output = fmt.Sprintf("[Discord] %v%v: %v", namePrefix, discordName, cleanedMessage)
}
/* Send the final text to factorio */
if fact.FactorioBooted && fact.FactIsRunning {
fact.FactChat(output)
}
cwlog.DoLogGame(output)
}
}