-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
148 lines (127 loc) · 3.84 KB
/
main.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
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
package main
import (
"flag"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"github.com/Necroforger/dgrouter"
"github.com/Necroforger/dgrouter/exrouter"
"github.com/asenci/pickerbot/cmd"
"github.com/bwmarrin/discordgo"
)
var (
Token string
// ManageChannels bool
Prefix string
Verbose bool
PrintVersion bool
version = "dev"
)
func init() {
flag.StringVar(&Token, "token", "", "bot token")
// flag.BoolVar(&ManageChannels, "manage-channels", false, "manage voice channels")
flag.StringVar(&Prefix, "prefix", "", "bot prefix")
flag.BoolVar(&Verbose, "verbose", false, "increase verbosity")
flag.BoolVar(&PrintVersion, "version", false, "print version")
flag.Parse()
}
func main() {
if PrintVersion {
fmt.Printf("pickerbot %s\n", version)
return
}
if Verbose {
log.Printf("starting pickerbot %s\n", version)
}
s, err := discordgo.New("Bot " + Token)
if err != nil {
log.Fatal("error creating Discord session,", err)
}
router := exrouter.New()
router.On("draw", wrapCmd(cmd.RunDraw, Verbose)).Desc("pick teams for the current draw")
router.On("games", wrapCmd(cmd.Games, Verbose)).Desc("list known games")
router.On("maps", wrapCmd(cmd.Maps, Verbose)).Desc("list known game maps")
router.On("me", wrapCmd(cmd.JoinDraw, Verbose)).Desc("join the current draw")
router.On("ping", wrapCmd(cmd.Ping, Verbose)).Desc("responds with pong")
router.On("play", wrapCmd(cmd.NewDraw, Verbose)).Desc("start a new draw")
router.On("quickdraw", wrapCmd(cmd.RunQuickDraw, Verbose)).Desc("run a quick draw with all online players on the channel")
router.On("whereto", wrapCmd(cmd.WhereTo, Verbose)).Desc("pick a place to drop or spawn")
router.Default = router.On("help", wrapCmd(cmd.Help, Verbose)).Desc("prints this help menu")
s.AddHandler(func(s *discordgo.Session, m *discordgo.Ready) {
if Verbose {
log.Println("ready to process requests")
}
})
s.AddHandler(func(s *discordgo.Session, m *discordgo.MessageCreate) {
wrapRouter(router, s, m.Message, Prefix, Verbose)
})
s.AddHandler(func(s *discordgo.Session, m *discordgo.MessageUpdate) {
wrapRouter(router, s, m.Message, Prefix, Verbose)
})
if Verbose {
log.Println("connecting to Discord")
}
err = s.Open()
if err != nil {
log.Fatal("error connecting to Discord,", err)
}
defer func(s *discordgo.Session) {
if err := s.Close(); err != nil {
log.Fatal(err)
}
}(s)
if Verbose {
defer log.Println("disconnecting from Discord")
}
if Verbose {
log.Println(s.State.User.Username, "connected")
}
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
if Verbose {
log.Printf("received %s signal, exiting\n", <-sc)
}
}
func wrapCmd(f func(ctx *exrouter.Context) (string, error), verbose bool) exrouter.HandlerFunc {
return func(ctx *exrouter.Context) {
sendReply := func(s string) {
if verbose {
log.Printf("sending reply: %q\n", s)
}
_, err := ctx.Reply(s)
if err != nil {
log.Println("error sending reply,", err)
}
}
reply, err := f(ctx)
if err != nil {
log.Println("error processing request,", err)
if reply == "" {
reply = fmt.Sprintf("Sorry <@%s>, an error has occurred while processing your request. Please try again.", ctx.Msg.Author.ID)
}
}
if reply != "" {
sendReply(reply)
}
}
}
func wrapRouter(r *exrouter.Route, s *discordgo.Session, m *discordgo.Message, prefix string, verbose bool) {
// Ignore messages sent by the bot
if m.Author.ID == s.State.User.ID {
return
}
// TODO: ignore messages without prefix or mention
if verbose {
log.Printf("received request from %s: %q\n", m.Author.Username, m.Content)
}
err := r.FindAndExecute(s, prefix, s.State.User.ID, m)
if err == dgrouter.ErrCouldNotFindRoute {
log.Printf("invalid request from %s: %q\n", m.Author.Username, m.Content)
return
}
if err != nil {
log.Println("error processing request,", err)
}
}