-
Notifications
You must be signed in to change notification settings - Fork 15
/
pingpong.go
64 lines (50 loc) · 1.4 KB
/
pingpong.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
package main
import (
"flag"
"log"
"github.com/Necroforger/dgrouter"
"github.com/Necroforger/dgrouter/exrouter"
"github.com/bwmarrin/discordgo"
)
// Command line flags
var (
fToken = flag.String("t", "", "bot token")
fPrefix = flag.String("p", "!", "bot prefix")
)
func main() {
flag.Parse()
s, err := discordgo.New("Bot " + *fToken)
if err != nil {
log.Fatal(err)
}
router := exrouter.New()
// Add some commands
router.On("ping", func(ctx *exrouter.Context) {
ctx.Reply("pong")
}).Desc("responds with pong")
router.On("avatar", func(ctx *exrouter.Context) {
ctx.Reply(ctx.Msg.Author.AvatarURL("2048"))
}).Desc("returns the user's avatar")
// Match the regular expression user(name)?
router.OnMatch("username", dgrouter.NewRegexMatcher("user(name)?"), func(ctx *exrouter.Context) {
ctx.Reply("Your username is " + ctx.Msg.Author.Username)
})
router.Default = router.On("help", func(ctx *exrouter.Context) {
var text = ""
for _, v := range router.Routes {
text += v.Name + " : \t" + v.Description + "\n"
}
ctx.Reply("```" + text + "```")
}).Desc("prints this help menu")
// Add message handler
s.AddHandler(func(_ *discordgo.Session, m *discordgo.MessageCreate) {
router.FindAndExecute(s, *fPrefix, s.State.User.ID, m.Message)
})
err = s.Open()
if err != nil {
log.Fatal(err)
}
log.Println("bot is running...")
// Prevent the bot from exiting
<-make(chan struct{})
}