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
/
Copy pathrouter.go
137 lines (115 loc) · 3.78 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
package dgc
import (
"regexp"
"strings"
"github.com/bwmarrin/discordgo"
)
// regexSplitting represents the regex to split the arguments at
var regexSplitting = regexp.MustCompile("\\s+")
// Router represents a DiscordGo command router
type Router struct {
Prefixes []string
IgnorePrefixCase bool
BotsAllowed bool
Commands []*Command
Middlewares []Middleware
PingHandler ExecutionHandler
Storage map[string]*ObjectsMap
}
// Create makes sure all maps get initialized
func Create(router *Router) *Router {
router.Storage = make(map[string]*ObjectsMap)
return router
}
// RegisterCmd registers a new command
func (router *Router) RegisterCmd(command *Command) {
router.Commands = append(router.Commands, command)
}
// GetCmd returns the command with the given name if it exists
func (router *Router) GetCmd(name string) *Command {
// Loop through all commands to find the correct one
for _, command := range router.Commands {
// Define the slice to check
toCheck := make([]string, len(command.Aliases)+1)
toCheck = append(toCheck, command.Name)
toCheck = append(toCheck, command.Aliases...)
// Check the prefix of the string
if stringArrayContains(toCheck, name, command.IgnoreCase) {
return command
}
}
return nil
}
// RegisterMiddleware registers a new middleware
func (router *Router) RegisterMiddleware(middleware Middleware) {
router.Middlewares = append(router.Middlewares, middleware)
}
// InitializeStorage initializes a storage map
func (router *Router) InitializeStorage(name string) {
router.Storage[name] = newObjectsMap()
}
// Initialize initializes the message event listener
func (router *Router) Initialize(session *discordgo.Session) {
session.AddHandler(router.Handler())
}
// Handler provides the discordgo handler for the given router
func (router *Router) Handler() func(*discordgo.Session, *discordgo.MessageCreate) {
return func(session *discordgo.Session, event *discordgo.MessageCreate) {
// Define useful variables
message := event.Message
content := message.Content
// Check if the message was sent by a bot
if message.Author.Bot && !router.BotsAllowed {
return
}
// Execute the ping handler if the message equals the current bot's mention
if (content == "<@!"+session.State.User.ID+">" || content == "<@"+session.State.User.ID+">") && router.PingHandler != nil {
router.PingHandler(&Ctx{
Session: session,
Event: event,
Arguments: ParseArguments(""),
Router: router,
})
return
}
// Check if the message starts with one of the defined prefixes
hasPrefix, content := stringHasPrefix(content, router.Prefixes, router.IgnorePrefixCase)
if !hasPrefix {
return
}
// Get rid of additional spaces
content = strings.Trim(content, " ")
// Check if the message is empty after the prefix processing
if content == "" {
return
}
// Split the messages at any whitespace
parts := regexSplitting.Split(content, -1)
// Check if the message starts with a command name
for _, command := range router.Commands {
// Check if the first part is the current command
if !stringArrayContains(getIdentifiers(command), parts[0], command.IgnoreCase) {
continue
}
content = strings.Join(parts[1:], " ")
// Define the command context
ctx := &Ctx{
Session: session,
Event: event,
Arguments: ParseArguments(content),
CustomObjects: newObjectsMap(),
Router: router,
Command: command,
}
// Trigger the command
command.trigger(ctx)
}
}
}
func getIdentifiers(command *Command) []string {
// Define an array containing the commands name and the aliases
toCheck := make([]string, len(command.Aliases)+1)
toCheck = append(toCheck, command.Name)
toCheck = append(toCheck, command.Aliases...)
return toCheck
}