This repository has been archived by the owner on Oct 1, 2021. It is now read-only.
/
basic.go
119 lines (95 loc) · 3.06 KB
/
basic.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
package main
import (
"fmt"
"os"
"os/signal"
"strconv"
"syscall"
"time"
"github.com/bwmarrin/discordgo"
"github.com/lus/dgc"
)
func main() {
// Open a simple Discord session
token := os.Getenv("TOKEN")
session, err := discordgo.New("Bot " + token)
if err != nil {
panic(err)
}
err = session.Open()
if err != nil {
panic(err)
}
// Wait for the user to cancel the process
defer func() {
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc
}()
// Create a dgc router
// NOTE: The dgc.Create function makes sure all the maps get initialized
router := dgc.Create(&dgc.Router{
// We will allow '!' and 'example!' as the bot prefixes
Prefixes: []string{
"!",
"example!",
},
// We will ignore the prefix case, so 'eXaMpLe!' is also a valid prefix
IgnorePrefixCase: true,
// We don't want bots to be able to execute our commands
BotsAllowed: false,
// We may initialize our commands in here, but we will use the corresponding method later on
Commands: []*dgc.Command{},
// We may inject our middlewares in here, but we will also use the corresponding method later on
Middlewares: []dgc.Middleware{},
// This handler gets called if the bot just got pinged (no argument provided)
PingHandler: func(ctx *dgc.Ctx) {
ctx.RespondText("Pong!")
},
})
// Register the default help command
router.RegisterDefaultHelpCommand(session, nil)
// Register a simple middleware that injects a custom object
router.RegisterMiddleware(func(next dgc.ExecutionHandler) dgc.ExecutionHandler {
return func(ctx *dgc.Ctx) {
// Inject a custom object into the context
ctx.CustomObjects.Set("myObject", 69)
// You can retrieve the object like this
obj := ctx.CustomObjects.MustGet("myObject").(int)
fmt.Println(obj)
// Call the next execution handler
next(ctx)
}
})
// Register a simple command that responds with our custom object
router.RegisterCmd(&dgc.Command{
// We want to use 'obj' as the primary name of the command
Name: "obj",
// We also want the command to get triggered with the 'object' alias
Aliases: []string{
"object",
},
// These fields get displayed in the default help messages
Description: "Responds with the injected custom object",
Usage: "obj",
Example: "obj",
// You can assign custom flags to a command to use them in middlewares
Flags: []string{},
// We want to ignore the command case
IgnoreCase: true,
// You may define sub commands in here
SubCommands: []*dgc.Command{},
// We want the user to be able to execute this command once in five seconds and the cleanup interval shpuld be one second
RateLimiter: dgc.NewRateLimiter(5*time.Second, 1*time.Second, func(ctx *dgc.Ctx) {
ctx.RespondText("You are being rate limited!")
}),
// Now we want to define the command handler
Handler: objCommand,
})
// Initialize the router
router.Initialize(session)
}
func objCommand(ctx *dgc.Ctx) {
// Respond with the just set custom object
ctx.RespondText(strconv.Itoa(ctx.CustomObjects.MustGet("myObject").(int)))
}