This repository has been archived by the owner on Oct 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
command.go
80 lines (71 loc) · 2.09 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
76
77
78
79
80
package dgc
import (
"strings"
)
// Command represents a simple command
type Command struct {
Name string
Aliases []string
Description string
Usage string
Example string
Flags []string
IgnoreCase bool
SubCommands []*Command
RateLimiter RateLimiter
Handler ExecutionHandler
}
// GetSubCmd returns the sub command with the given name if it exists
func (command *Command) GetSubCmd(name string) *Command {
// Loop through all commands to find the correct one
for _, subCommand := range command.SubCommands {
// Define the slice to check
toCheck := make([]string, len(subCommand.Aliases)+1)
toCheck = append(toCheck, subCommand.Name)
toCheck = append(toCheck, subCommand.Aliases...)
// Check the prefix of the string
if stringArrayContains(toCheck, name, subCommand.IgnoreCase) {
return subCommand
}
}
return nil
}
// NotifyRateLimiter notifies the rate limiter about a new execution and returns false if the user is being rate limited
func (command *Command) NotifyRateLimiter(ctx *Ctx) bool {
if command.RateLimiter == nil {
return true
}
return command.RateLimiter.NotifyExecution(ctx)
}
// trigger triggers the given command
func (command *Command) trigger(ctx *Ctx) {
// Check if the first argument matches a sub command
if len(ctx.Arguments.arguments) > 0 {
argument := ctx.Arguments.Get(0).Raw()
subCommand := command.GetSubCmd(argument)
if subCommand != nil {
// Define the arguments for the sub command
arguments := ParseArguments("")
if ctx.Arguments.Amount() > 1 {
arguments = ParseArguments(strings.Join(strings.Split(ctx.Arguments.Raw(), " ")[1:], " "))
}
// Trigger the sub command
subCommand.trigger(&Ctx{
Session: ctx.Session,
Event: ctx.Event,
Arguments: arguments,
CustomObjects: ctx.CustomObjects,
Router: ctx.Router,
Command: subCommand,
})
return
}
}
// Prepare all middlewares
nextHandler := command.Handler
for _, middleware := range ctx.Router.Middlewares {
nextHandler = middleware(nextHandler)
}
// Run all middlewares
nextHandler(ctx)
}