-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
332 lines (301 loc) · 7.98 KB
/
Copy pathmain.go
File metadata and controls
332 lines (301 loc) · 7.98 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
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
package main
import (
"context"
"flag"
"fmt"
"os"
"os/signal"
"strings"
"syscall"
"time"
"M45HelpBot/cwlog"
"M45HelpBot/sclean"
"github.com/bwmarrin/discordgo"
)
var (
skipThrottle bool
helpsFile string
)
func main() {
token := flag.String("token", "", "discord token")
role := flag.String("staffid", "", "discord role ID for moderator/staff")
staffChan := flag.String("staffChannel", "", "specify a staff-only channel")
guildid := flag.String("guildid", "", "discord guild id")
testMode := flag.Bool("testmode", false, "skip throttle check")
helpPath := flag.String("helpFilePath", "helps.json", "Specify path to helps file.")
flag.Parse()
discToken = *token
staffRole = *role
guildID = *guildid
skipThrottle = *testMode
helpsFile = *helpPath
staffChannel = *staffChan
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()
ctx, cancel := context.WithTimeout(ctx, rebootTime)
defer cancel()
if err := run(ctx); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func run(ctx context.Context) error {
cwlog.StartCWLog()
defer cwlog.CloseCWLog()
cwlog.DoLog("Starting goDiscInfoBot.")
if strings.TrimSpace(guildID) == "" {
return fmt.Errorf("Discord guild ID not set")
}
if !readHelps() {
return fmt.Errorf("could not load help file %q", helpsFile)
}
if ctx.Err() != nil {
return nil
}
type connectionResult struct {
bot *discordgo.Session
err error
}
connected := make(chan connectionResult)
go func(token string) {
bot, err := startbot(ctx, token)
select {
case connected <- connectionResult{bot, err}:
case <-ctx.Done():
if bot != nil {
bot.Close()
}
}
}(discToken)
// Open can block in the gateway handshake. Signals must still stop the bot.
select {
case result := <-connected:
if result.err != nil {
if ctx.Err() != nil {
return nil
}
return result.err
}
defer result.bot.Close()
case <-ctx.Done():
return nil
}
<-ctx.Done()
return nil
}
func startbot(ctx context.Context, token string) (*discordgo.Session, error) {
if strings.TrimSpace(token) == "" {
return nil, fmt.Errorf("Discord token not set")
}
var lastErr error
for attempt := 0; attempt < maxAttempts; attempt++ {
if attempt > 0 {
timer := time.NewTimer(time.Duration(attempt*5) * time.Second)
select {
case <-ctx.Done():
timer.Stop()
return nil, ctx.Err()
case <-timer.C:
}
}
if err := ctx.Err(); err != nil {
return nil, err
}
cwlog.DoLog("Starting Discord bot...")
bot, err := newBotSession(token)
if err == nil {
err = bot.Open()
if err == nil {
return bot, nil
}
bot.Close()
}
lastErr = err
cwlog.DoLog(fmt.Sprintf("Discord connection attempt %d failed: %v", attempt+1, err))
}
return nil, fmt.Errorf("Discord connection failed after %d attempts: %w", maxAttempts, lastErr)
}
func newBotSession(token string) (*discordgo.Session, error) {
bot, err := discordgo.New("Bot " + token)
if err != nil {
return nil, err
}
bot.Identify.Intents = discordgo.IntentsAllWithoutPrivileged | discordgo.IntentMessageContent
bot.LogLevel = discordgo.LogWarning
// Register once per session: READY can be delivered again after reconnecting.
bot.AddHandler(BotReady)
bot.AddHandler(MessageCreate)
return bot, nil
}
func BotReady(s *discordgo.Session, r *discordgo.Ready) {
/* Set the bot's Discord status message */
botstatus := "m45sci.xyz"
errc := s.UpdateGameStatus(0, botstatus)
if errc != nil {
cwlog.DoLog(errc.Error())
}
cwlog.DoLog("Discord bot ready.")
}
func MessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
if s == nil || m == nil || m.Message == nil || m.Author == nil ||
m.Author.Bot || m.WebhookID != "" || m.GuildID == "" || m.GuildID != guildID {
return
}
if s.State != nil {
s.State.RLock()
isSelf := s.State.User != nil && m.Author.ID == s.State.User.ID
s.State.RUnlock()
if isSelf {
return
}
}
filterMessages(s, m)
}
func filterMessages(s *discordgo.Session, m *discordgo.MessageCreate) {
staffMode := false
//Switch lists if user is staff
searchList := HelpsListData{}
if staffRole != "" && m.Member != nil {
for _, role := range m.Member.Roles {
if role == staffRole {
staffMode = true
if staffChannel != "" &&
m.ChannelID != staffChannel {
return
}
break
}
}
}
for _, help := range helpsList {
if !staffMode && strings.EqualFold(help.Name, "main") {
searchList = help
break
} else if staffMode && strings.EqualFold(help.Name, "staff") {
searchList = help
break
}
}
if len(searchList.Data) == 0 {
cwlog.DoLog("No helps data found.")
return
}
outLines := helpReplyLines(m.Content, searchList.Data)
if len(outLines) > 0 {
sendHelpReply(s, m, strings.Join(outLines, "\n"))
}
}
func helpReplyLines(content string, helps []helpData) []string {
msgLower := strings.ToLower(sclean.RemoveDiscordMarkdown(content))
msgLower = sclean.StripControlAndSubSpecial(msgLower)
msgLower = strings.Join(strings.Fields(msgLower), " ")
msgWild := sclean.AlphaNumOnly(strings.ReplaceAll(msgLower, " the ", " "))
msgWords := strings.Fields(msgLower)
for i, word := range msgWords {
msgWords[i] = sclean.AlphaNumOnly(word)
}
var outLines []string
responseCount := 0
for _, help := range helps {
if responseCount >= maxCombinedResponses {
break
}
keyword := help.match(msgLower, msgWild, msgWords)
if keyword == "" {
continue
}
if len(outLines) != 0 {
outLines = append(outLines, "")
}
outLines = append(outLines, help.title(keyword)+":")
outLines = append(outLines, help.ReplyLines...)
responseCount++
}
return outLines
}
func (help helpData) match(msgLower, msgWild string, msgWords []string) string {
for _, exclude := range help.Exclude {
exclude = strings.ToLower(exclude)
// URLs need their punctuation; existing compact exclusions still work.
if exclude != "" && (strings.Contains(msgLower, exclude) || strings.Contains(msgWild, exclude)) {
return ""
}
}
for _, wildcard := range help.Wildcards {
if wildcard != "" && strings.Contains(msgWild, strings.ToLower(wildcard)) {
return wildcard
}
}
for _, word := range msgWords {
if word == "" {
continue
}
for _, keyword := range help.Words {
if strings.EqualFold(word, keyword) {
return keyword
}
}
}
return ""
}
func sendHelpReply(s *discordgo.Session, m *discordgo.MessageCreate, content string) {
// Keep the check, send, and accounting together so concurrent handlers cannot
// bypass the limits. A failed send must not consume a user's daily reply.
replyMu.Lock()
defer replyMu.Unlock()
if !checkThrottle(m) {
return
}
reply := &discordgo.MessageSend{
Content: content,
Reference: &discordgo.MessageReference{
MessageID: m.ID,
ChannelID: m.ChannelID,
GuildID: m.GuildID,
},
}
if _, err := s.ChannelMessageSendComplex(m.ChannelID, reply); err != nil {
cwlog.DoLog(fmt.Sprintf("Could not send help reply: %v", err))
return
}
if !skipThrottle {
now := time.Now()
if users[m.Author.ID] == nil {
users[m.Author.ID] = &userData{id: m.Author.ID}
}
users[m.Author.ID].lastSaw = now
users[m.Author.ID].total++
lastReply = now
totalMsgCount++
}
cwlog.DoLog(fmt.Sprintf("TRIGGERED:\n%v: %v: %v\nReply: %v", m.ChannelID, m.Author.Username, m.Content, content))
}
func (help helpData) title(fallback string) string {
if help.Title != "" {
return help.Title
}
return fallback
}
// checkThrottle requires replyMu to be held. It does not consume an allowance.
func checkThrottle(m *discordgo.MessageCreate) bool {
if skipThrottle {
return true
}
if totalMsgCount >= maxGlobal {
return false
}
if time.Since(lastReply) < throttleGlobal {
cwlog.DoLog(fmt.Sprintf("global throttled: User: %v, Message: %v", m.Author.ID, m.Content))
return false
}
if user := users[m.Author.ID]; user != nil {
if user.total >= maxPerUser {
return false
}
if time.Since(user.lastSaw) < throttlePerUser {
cwlog.DoLog(fmt.Sprintf("user throttled: User: %v, Message: %v", m.Author.ID, m.Content))
return false
}
}
return true
}