/
router.go
232 lines (198 loc) · 6.7 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package dgc
import (
"fmt"
"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
DM bool
}
// 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())
session.AddHandler(router.EditHandler())
}
// 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
}
//Avoid user DMs
if message.GuildID == "" && !router.DM {
session.ChannelMessageSend(event.ChannelID, "Use commands in a server, thanks")
if event.Author != nil {
session.ChannelMessageSend("869974921311289374", fmt.Sprintf("<@801243015016087562>, %s tried to DM the bot `%s`", event.Author.Mention(), content))
} else {
session.ChannelMessageSend("869974921311289374", fmt.Sprintf("<@801243015016087562>, someone tried to DM the bot `%s`", 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 (router *Router) EditHandler() func(*discordgo.Session, *discordgo.MessageUpdate) {
return func(session *discordgo.Session, event *discordgo.MessageUpdate) {
// Define useful variables
if event.BeforeUpdate == nil || event.Author == nil {
return
}
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: &discordgo.MessageCreate{
Message: event.Message,
},
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
}
// Avoid user DMs
if message.GuildID == "" && !router.DM {
session.ChannelMessageSend(event.ChannelID, "Use commands in a server, thanks")
if event.Author != nil {
session.ChannelMessageSend("869974921311289374", fmt.Sprintf("<@801243015016087562>, %s tried to DM the bot `%s`", event.Author.Mention(), content))
} else {
session.ChannelMessageSend("869974921311289374", fmt.Sprintf("<@801243015016087562>, someone tried to DM the bot `%s`", 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: &discordgo.MessageCreate{
Message: event.Message,
},
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
}