-
Notifications
You must be signed in to change notification settings - Fork 1
/
router.go
152 lines (123 loc) · 3.44 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package bot
import (
"fmt"
"log"
"github.com/bwmarrin/discordgo"
discord "github.com/bwmarrin/discordgo"
)
type Router struct {
commands map[string]*Command
registeredCommands []*discord.ApplicationCommand
}
func NewRouter(initial []*Command) (r *Router) {
r = &Router{commands: make(map[string]*Command, len(initial))}
for _, cmd := range initial {
r.Register(cmd)
}
return
}
func (r *Router) Register(cmd *Command) {
if _, ok := r.commands[cmd.Name]; !ok {
r.commands[cmd.Name] = cmd
}
}
func (r *Router) Get(name string) *Command {
if r == nil {
return nil
}
return r.commands[name]
}
func (r *Router) List() (list []*Command) {
if r == nil {
return nil
}
for _, c := range r.commands {
list = append(list, c)
}
return
}
func (r *Router) Count() (c int) {
if r == nil {
return 0
}
return len(r.commands)
}
func (r *Router) getSubcommand(cmd *Command, opt *discord.ApplicationCommandInteractionDataOption, parent []Handler) (*Command, *discord.ApplicationCommandInteractionDataOption, []Handler) {
if cmd == nil {
return nil, nil, nil
}
subcommand := cmd.SubCommands.Get(opt.Name)
switch opt.Type {
case discordgo.ApplicationCommandOptionSubCommand:
return subcommand, opt, append(parent, append(subcommand.Middlewares, subcommand.Handler)...)
case discordgo.ApplicationCommandOptionSubCommandGroup:
return r.getSubcommand(subcommand, opt.Options[0], append(parent, subcommand.Middlewares...))
}
return cmd, nil, append(parent, cmd.Handler)
}
func (r *Router) getMessageHandlers(cmd *Command) []MessageHandler {
var handlers []MessageHandler
if cmd.MessageHandler != nil {
handlers = append(handlers, cmd.MessageHandler)
}
if cmd.SubCommands != nil {
for _, cmd := range cmd.SubCommands.List() {
handlers = append(handlers, r.getMessageHandlers(cmd)...)
}
}
return handlers
}
func (r *Router) HandleInteraction(s *discord.Session, i *discord.InteractionCreate) {
if i.Type != discord.InteractionApplicationCommand {
return
}
data := i.ApplicationCommandData()
cmd := r.Get(data.Name)
if cmd == nil {
return
}
var parent *discord.ApplicationCommandInteractionDataOption
handlers := append(cmd.Middlewares, cmd.Handler)
if len(data.Options) != 0 {
cmd, parent, handlers = r.getSubcommand(cmd, data.Options[0], cmd.Middlewares)
}
if cmd != nil {
ctx := NewContext(s, cmd, i.Interaction, parent, handlers)
ctx.Next()
}
}
func (r *Router) HandleMessage(s *discord.Session, m *discord.MessageCreate) {
for _, cmd := range r.commands {
handlers := r.getMessageHandlers(cmd)
if len(handlers) > 0 {
ctx := NewMessageContext(s, cmd, m.Message, handlers)
ctx.Next()
}
}
}
func (r *Router) Sync(s *discord.Session, guild string) (err error) {
if s.State.User == nil {
return fmt.Errorf("cannot determine application id")
}
var commands []*discord.ApplicationCommand
for _, c := range r.commands {
commands = append(commands, c.ApplicationCommand())
}
r.registeredCommands, err = s.ApplicationCommandBulkOverwrite(s.State.User.ID, guild, commands)
return
}
func (r *Router) ClearCommands(s *discord.Session, guild string) (errors []error) {
if s.State.User == nil {
return []error{fmt.Errorf("cannot determine application id")}
}
for _, v := range r.registeredCommands {
err := s.ApplicationCommandDelete(s.State.User.ID, guild, v.ID)
if err != nil {
log.Panicf("Cannot delete '%v' command: %v", v.Name, err)
}
}
if len(errors) == 0 {
return nil
}
return errors
}