-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.go
115 lines (98 loc) · 2.67 KB
/
router.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
package cmdlr3
import (
"log"
"github.com/andersfylling/disgord"
)
type Router struct {
Commands []*Command
CommandComponents []*CommandMessageComponent
CommandComponentsMap map[string]*Command
Client *disgord.Client
}
func Create(client *disgord.Client) *Router {
return &Router{
Commands: []*Command{},
Client: client,
CommandComponentsMap: make(map[string]*Command),
}
}
func (r *Router) GetCmd(name string) *Command {
for _, cmd := range r.Commands {
if cmd.Name == name {
return cmd
}
}
return nil
}
func (r *Router) InitializeCommands() disgord.HandlerReady {
return func(s disgord.Session, h *disgord.Ready) {
user, _ := r.Client.Cache().GetCurrentUser()
for i := range r.Commands {
if err := r.Client.ApplicationCommand(user.ID).Global().Create(&disgord.CreateApplicationCommand{
Type: disgord.ApplicationCommandType(r.Commands[i].Type),
Name: r.Commands[i].Name,
Description: r.Commands[i].Description,
Options: append(r.Commands[i].Options, r.Commands[i].ConvertSubcommandArray()...),
}); err != nil {
log.Fatal(err)
}
}
}
}
func (r *Router) RegisterCommand(command *Command) {
r.Commands = append(r.Commands, command)
for _, cmdComponent := range command.Components {
r.CommandComponents = append(r.CommandComponents, cmdComponent)
}
}
func (r *Router) RegisterCMDList(commands []*Command) {
r.Commands = append(r.Commands, commands...)
for _, c := range commands {
for _, cmdComponent := range c.Components {
r.CommandComponents = append(r.CommandComponents, cmdComponent)
}
}
}
func (r *Router) Init() {
r.Client.Gateway().Ready(r.InitializeCommands())
r.Client.Gateway().InteractionCreate(r.Handler())
}
func (r *Router) Handler() disgord.HandlerInteractionCreate {
return func(s disgord.Session, h *disgord.InteractionCreate) {
ctx := &Ctx{
Client: r.Client,
Session: &s,
Interaction: h,
Router: r,
}
for _, cmd := range r.Commands {
if h.Data.Name != "" && h.Data.Name == cmd.Name {
subCmd := checkForSubCommand(cmd, h.Data.Options)
if h.Data.Options != nil && subCmd != nil {
subCmd.Handler(&SubCommandCtx{
Client: r.Client,
Session: &s,
Interaction: h,
Command: subCmd,
Router: r,
})
return
}
ctx.Command = cmd
cmd.Handler(ctx)
return
}
}
for _, component := range r.CommandComponents {
if h.Data.CustomID != "" && h.Data.CustomID == component.CustomID {
component.Handler(&ComponentCtx{
Client: r.Client,
Session: &s,
Interaction: h,
Router: r,
})
return
}
}
}
}