-
Notifications
You must be signed in to change notification settings - Fork 5
/
command.go
75 lines (62 loc) · 2.38 KB
/
command.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
package disgolf
import (
"github.com/bwmarrin/discordgo"
)
// A Handler processes the command
type Handler interface {
HandleCommand(ctx *Ctx)
}
// HandlerFunc is a wrapper around Handler for functions
type HandlerFunc func(ctx *Ctx)
// HandleCommand implements Handler interface and calls the function with provided context
func (f HandlerFunc) HandleCommand(ctx *Ctx) { f(ctx) }
// A MessageHandler processes the message command
type MessageHandler interface {
HandleMessageCommand(ctx *MessageCtx)
}
// HandlerFunc is a wrapper around MessageHandler for functions
type MessageHandlerFunc func(ctx *MessageCtx)
// HandleCommand implements MessageHandler interface and calls the function with provided context
func (f MessageHandlerFunc) HandleMessageCommand(ctx *MessageCtx) { f(ctx) }
// Command represents a command.
type Command struct {
Name string
Description string
Options []*discordgo.ApplicationCommandOption
Type discordgo.ApplicationCommandType
Handler Handler
Middlewares []Handler
MessageHandler MessageHandler
MessageMiddlewares []MessageHandler
// NOTE: nesting of more than 3 level has no effect
SubCommands *Router
// Custom payload for the command. Useful for module names, and such stuff.
Custom interface{}
}
// ApplicationCommand converts Command to discordgo.ApplicationCommand.
func (cmd Command) ApplicationCommand() *discordgo.ApplicationCommand {
applicationCommand := &discordgo.ApplicationCommand{
Name: cmd.Name,
Description: cmd.Description,
Options: cmd.Options,
Type: cmd.Type,
}
for _, subcommand := range cmd.SubCommands.List() {
applicationCommand.Options = append(applicationCommand.Options, subcommand.ApplicationCommandOption())
}
return applicationCommand
}
// ApplicationCommandOption converts Command to discordgo.ApplicationCommandOption (subcommand).
func (cmd Command) ApplicationCommandOption() *discordgo.ApplicationCommandOption {
applicationCommand := cmd.ApplicationCommand()
typ := discordgo.ApplicationCommandOptionSubCommand
if cmd.SubCommands != nil && cmd.SubCommands.Count() != 0 {
typ = discordgo.ApplicationCommandOptionSubCommandGroup
}
return &discordgo.ApplicationCommandOption{
Name: applicationCommand.Name,
Description: applicationCommand.Description,
Options: applicationCommand.Options,
Type: typ,
}
}