-
Notifications
You must be signed in to change notification settings - Fork 1
/
command.go
67 lines (53 loc) · 1.85 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
package bot
import (
discord "github.com/bwmarrin/discordgo"
)
type Handler interface {
HandleCommand(ctx *Context)
}
type HandlerFunc func(ctx *Context)
func (f HandlerFunc) HandleCommand(ctx *Context) { f(ctx) }
type MessageHandler interface {
HandleMessageCommand(ctx *MessageContext)
}
type MessageHandlerFunc func(ctx *MessageContext)
func (f MessageHandlerFunc) HandleMessageCommand(ctx *MessageContext) { f(ctx) }
type Command struct {
Name string
Description string
DMPermission bool
DefaultMemberPermissions int64
Options []*discord.ApplicationCommandOption
Type discord.ApplicationCommandType
Handler Handler
Middlewares []Handler
MessageHandler MessageHandler
SubCommands *Router
}
func (cmd Command) ApplicationCommand() *discord.ApplicationCommand {
applicationCommand := &discord.ApplicationCommand{
Name: cmd.Name,
Description: cmd.Description,
DMPermission: &cmd.DMPermission,
DefaultMemberPermissions: &cmd.DefaultMemberPermissions,
Options: cmd.Options,
Type: cmd.Type,
}
for _, subcommand := range cmd.SubCommands.List() {
applicationCommand.Options = append(applicationCommand.Options, subcommand.ApplicationCommandOption())
}
return applicationCommand
}
func (cmd Command) ApplicationCommandOption() *discord.ApplicationCommandOption {
applicationCommand := cmd.ApplicationCommand()
typ := discord.ApplicationCommandOptionSubCommand
if cmd.SubCommands != nil && cmd.SubCommands.Count() != 0 {
typ = discord.ApplicationCommandOptionSubCommandGroup
}
return &discord.ApplicationCommandOption{
Name: applicationCommand.Name,
Description: applicationCommand.Description,
Options: applicationCommand.Options,
Type: typ,
}
}