/
handlers.go
77 lines (68 loc) · 1.67 KB
/
handlers.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
package pail
import (
"fmt"
"strings"
log "github.com/sirupsen/logrus"
"github.com/bwmarrin/discordgo"
)
func (p *Pail) messageHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author.ID == s.State.User.ID {
return
}
msg := strings.TrimSpace(m.Content)
parts := strings.Split(msg, " ")
mentioned := false
for _, mention := range m.Mentions {
log.Debug(fmt.Sprintf("Mention: %+v. Mention ID: %s. My ID: %s. Part 0: %s", mention, mention.ID, s.State.User.ID, parts[0]))
if mention.ID == s.State.User.ID && (fmt.Sprintf("<@%s>", s.State.User.ID) == parts[0] || fmt.Sprintf("<@!%s>", s.State.User.ID) == parts[0]) {
msg = strings.Join(parts[1:], " ")
mentioned = true
log.Debug("My mention: ", msg)
break
}
}
fact, err := findFact(p.db, msg, m.Author.Mention())
if err != nil {
return
}
if fact != nil {
p.lastFact = fact
reply, err := fact.handle()
if err != nil {
log.Error(err)
return
}
p.Say(m.ChannelID, reply)
return
}
if mentioned {
regex := getAllRegex(p.db, true)
for _, rxp := range regex {
if rxp.Compiled.MatchString(msg) {
log.Debug("Mention regex found: ", rxp.Expression)
reply, err := rxp.handle(p, msg, m.Author.Mention())
if err != nil {
log.Error(err)
return
}
p.Say(m.ChannelID, reply)
return
}
}
log.Debug("No mention regex found")
}
regex := getAllRegex(p.db, false)
for _, rxp := range regex {
if rxp.Compiled.MatchString(msg) {
log.Debug("Regex found: ", rxp.Expression)
reply, err := rxp.handle(p, msg, m.Author.Mention())
if err != nil {
log.Error(err)
return
}
p.Say(m.ChannelID, reply)
return
}
}
log.Debug("No regex found")
}