forked from BryanSLam/discord-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommander.go
50 lines (41 loc) · 1.27 KB
/
commander.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
package discordbot
import (
"fmt"
"regexp"
"github.com/BryanSLam/discord-bot/util"
dg "github.com/bwmarrin/discordgo"
)
// Commander return pattern matching handler
func (b *Bot) Commander(s *dg.Session, m *dg.MessageCreate) {
commandRegex := regexp.MustCompile(`(?i)^![\w]+[\w ".]*[ 0-9/]*$`)
if commandRegex.MatchString(m.Content) {
// Ignore all messages created by the bot itself
// This isn't required in this specific example but it's a good practice.
if m.Author.ID == s.State.User.ID {
return
}
dr := NewDiscordReader(s, m, "")
dw := NewDiscordWriter(s, m, "")
drw := NewDiscordReadWriter(dr, dw)
logWriter := NewDiscordWriter(s, nil, b.botLogChannelID)
logger := util.NewLogger(logWriter)
for _, c := range b.cmds {
if c.Match(m.Content) {
go func() {
defer func() {
if err := recover(); err != nil {
logger.Write([]byte(util.MentionMaintainers(b.maintainers) + " an error has occurred"))
logger.Warn(fmt.Sprintln("function", util.FuncName(c.Fn), "failed:", err))
}
}()
// TODO: use context and change command to an interface instead of struct
mm := map[string]interface{}{}
mm["messageCreate"] = m
c.Fn(drw, logger, mm)
}()
return
}
}
drw.Write([]byte("Invalid Command"))
}
}