/
registrar.go
277 lines (236 loc) · 7.62 KB
/
registrar.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package commands
import (
"avorioncontrol/ifaces"
"avorioncontrol/logger"
"errors"
"fmt"
"log"
"regexp"
"sort"
"strings"
"github.com/bwmarrin/discordgo"
)
var registrars map[string]*CommandRegistrar
func init() {
registrars = make(map[string]*CommandRegistrar)
}
// CommandRegistrar - Guild specific container for commands and authorization
// level settings
type CommandRegistrar struct {
GuildID string
commands map[string]*CommandRegistrant
commandnames []string
loglevel int
server ifaces.IGameServer
embeds []chan struct{}
}
// SetLoglevel - Set the current loglevel
func (reg *CommandRegistrar) SetLoglevel(l int) {
reg.loglevel = l
logger.LogInfo(reg, sprintf("Setting loglevel to %d", l))
}
// UUID - Return a commands name.
func (reg *CommandRegistrar) UUID() string {
return "CommandRegistrar:" + reg.GuildID
}
// NewRegistrar - Create and return a new instance of CommandRegistrar
// @gid string ID string of the guild the CommandRegistrar belongs to
func NewRegistrar(gid string, gs ifaces.IGameServer) *CommandRegistrar {
registrars[gid] = &CommandRegistrar{
GuildID: gid,
commands: make(map[string]*CommandRegistrant, 10),
server: gs,
loglevel: 1,
embeds: make([]chan struct{}, 0)}
return registrars[gid]
}
// Registrar - Return the Registrar that is associated with a specific guild
func Registrar(gid string) (r *CommandRegistrar, err error) {
if r = registrars[gid]; r == nil {
return nil, errors.New("no registrar exists for that guild")
}
return r, nil
}
// Loglevel - Return the current loglevel for the CommandRegistrant
func (reg *CommandRegistrar) Loglevel() int {
var l int
if reg.loglevel < 0 {
l = 0
} else {
l = reg.loglevel
}
return l
}
// Register - Register a command with the given options
// @n string Name
// @d string Description text
// @u string Usage text
// @a []CommandArgument Valid arguments
// @f BotCommand Function to execute
//
// @owner ...string Registered command to add this subcommand to
func (reg *CommandRegistrar) Register(n, d, u string, a []CommandArgument,
f BotCommand, owners ...string) error {
// Does it exist?
if reg.IsRegistered(n) && len(owners) < 1 {
log.Fatal(fmt.Errorf("command %s was already defined", n))
}
if len(a) == 0 {
a = nil
}
registrant := &CommandRegistrant{
name: n,
description: d,
exec: f,
args: a,
usage: u,
loglevel: reg.Loglevel(),
registrar: reg}
// Add cmdlets to owners if strings containing command names are both
// supplied and valid
if len(owners) > 0 {
for _, owner := range owners {
if !reg.IsRegistered(owner) {
log.Fatal("Invalid subcommand owner passed to commands.Register")
}
reg.commands[owner].cmdlets = append(reg.commands[owner].cmdlets,
registrant)
logger.LogDebug(reg, sprintf("Registered subcommand %s to %s ", n,
reg.commands[owner].UUID()))
}
return nil
}
// Otherwise, register the command to the registrar
reg.commands[n] = registrant
reg.commandnames = append(reg.commandnames, registrant.Name())
// Sort the slice
sort.Strings(reg.commandnames)
logger.LogDebug(reg, sprintf("Registered command [%s]", n))
return nil
}
// IsRegistered - Return true if the command is registered, false if its not
// @n string Name of a command
func (reg *CommandRegistrar) IsRegistered(n string) bool {
if reg.commands[n] == nil {
return false
}
return true
}
// Command - Return a pointer to a given registered CommandRegistrant
// @n string Name of a command
// TODO: Consider refactoring this to make use of BotConfig.GetAliasesCommand
func (reg *CommandRegistrar) Command(n string) (*CommandRegistrant, error) {
if !reg.IsRegistered(n) {
return nil, errors.New(sprintf("Command %s isn't registered!", n))
}
return reg.commands[n], nil
}
// AllCommands - Return an int and a string slice. The int is how many commands
// are currently registered, and the slice is list of their names
func (reg *CommandRegistrar) AllCommands() (int, []string) {
return len(reg.commandnames), reg.commandnames
}
// ProcessCommand - Processes a Discord message that has the configured prefix,
// and runs the correct command given its contents
// @s *discordgo.Session Discordgo Session
// @m *discordgo.MessageCreate Discordgo message event
// @c IConfigurator Bot configuration pointer
func (reg *CommandRegistrar) ProcessCommand(s *discordgo.Session,
m *discordgo.MessageCreate, c ifaces.IConfigurator,
exitch chan struct{}) (string, ICommandError) {
var (
err error
out *CommandOutput
cmd *CommandRegistrant
member *discordgo.Member
cmderr ICommandError
)
args := make(BotArgs, 0)
input := strings.TrimPrefix(m.Content, c.Prefix())
input = strings.TrimSpace(input)
// Split our arguments and add them to the args slice
for _, m := range regexp.MustCompile("[^\\s]+").
FindAllStringSubmatch(input, -1) {
args = append(args, m[0])
}
// If there was no command given, don't do anything else
if len(args) == 0 || len(args[0]) == 0 {
return "empty", nil
}
name := args[0]
// If the command doesn't exist, check if what was passed was an alias. If
// it was, then we just reference the command that was aliased
// TODO: Refactor this if CommandRegistrar.GetAliasedCommand and
// BotConfig.GetAliasedCommand are refactored. See the TODO
// on those methods for details
if cmd, err = reg.Command(name); err != nil {
if b, ac := c.GetAliasedCommand(name); b == true {
if cmd, err = reg.Command(ac); err != nil {
logger.LogError(reg, "Invalid alias: "+ac)
return ac, &ErrInvalidAlias{cmd: cmd, alias: ac}
}
} else {
return name, &ErrInvalidCommand{name: name, cmd: cmd}
}
}
if c.CommandDisabled(cmd.Name()) {
logger.LogInfo(cmd, sprintf(
"%s attempted to run the disabled command: %s", m.Author.String, cmd.Name()))
return cmd.Name(), &ErrInvalidCommand{name: cmd.Name(), cmd: cmd}
}
if member, err = s.GuildMember(reg.GuildID, m.Author.ID); err != nil {
return cmd.Name(), &ErrCommandError{
message: "You are not part of the guild, and cannot use this command",
cmd: cmd}
}
authreq := c.GetCmndAuth(cmd.Name())
authlvl := 0
if authreq > 0 {
for _, r := range member.Roles {
if l := c.GetRoleAuth(r); l > authlvl {
authlvl = l
}
}
if authlvl < authreq {
return cmd.Name(), &ErrUnauthorizedUsage{cmd: cmd}
}
}
if cmd.exec == nil {
logger.LogError(cmd, sprintf("Can't execute %s (missing exec field)",
cmd.Name()))
return cmd.Name(), &ErrInvalidCommand{name: cmd.Name()}
}
// Update our arguments with the full command name
args[0] = cmd.Name()
if len(args) > 1 && args[1] == "help" && cmd.Name() != "rcon" {
out = cmd.Help()
} else {
out, cmderr = cmd.exec(s, m, args, c, cmd)
}
if cmderr != nil {
s.MessageReactionAdd(m.ChannelID, m.ID, "🚫")
} else {
s.MessageReactionAdd(m.ChannelID, m.ID, "✅")
if out != nil {
// Get the number of pages and use that to determine if we need a pager
if _, max := out.Index(); max > 0 {
if len(reg.embeds) > 4 {
close(reg.embeds[0])
reg.embeds[0] = nil
reg.embeds = reg.embeds[1:]
}
expirech := make(chan struct{})
reg.embeds = append(reg.embeds, expirech)
logger.LogDebug(reg, "Starting a multipage embed goroutine")
go CreatePagedEmbed(out, s, m, expirech, exitch)
} else {
logger.LogDebug(reg, "Generating a single page embed")
embed, _, _ := GenerateOutputEmbed(out, out.ThisPage())
if _, err := s.ChannelMessageSendEmbed(m.ChannelID, embed); err != nil {
logger.LogError(cmd, "discordgo: "+err.Error())
}
}
}
}
return cmd.Name(), cmderr
}