-
Notifications
You must be signed in to change notification settings - Fork 10
/
bot_commands.lua
246 lines (207 loc) · 6.22 KB
/
bot_commands.lua
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
-- Copyright (C) 2018 Jérôme Leclercq
-- This file is part of the "Not a Bot" application
-- For conditions of distribution and use, see copyright notice in LICENSE
local client = Bot.Client
local config = Config
local enums = discordia.enums
function Bot:BuildUsage(commandTable)
local usage = {}
for k,v in ipairs(commandTable.Args) do
if (v.Optional) then
table.insert(usage, string.format("[%s]", v.Name))
else
table.insert(usage, string.format("%s", v.Name))
end
end
return table.concat(usage, " ")
end
function Bot:ParseCommandArgs(member, expectedArgs, args)
local parsers = self.ConfigTypeParameter
local values = {}
local argumentIndex = 1
for argIndex, argData in ipairs(expectedArgs) do
if (not args[argumentIndex]) then
if (not argData.Optional) then
return false, string.format("Missing argument #%d (%s)", argIndex, argData.Name)
end
break
end
local argValue
if (argIndex == #expectedArgs and argumentIndex < #args) then
argValue = table.concat(args, " ", argumentIndex)
else
argValue = args[argumentIndex]
end
local value, err = parsers[argData.Type](argValue, member.guild, argData.Options)
if (value ~= nil) then
values[argIndex] = value
argumentIndex = argumentIndex + 1
elseif (argData.Optional) then
values[argIndex] = nil
-- Do not increment argumentIndex, try to parse it again as the next parameter
else
return false, string.format("Invalid value for argument %d (%s)%s", argIndex, argData.Name, err and ": " .. err or "")
end
end
return values
end
function Bot:RegisterCommand(values)
local name = string.lower(values.Name)
if (self.Commands[name]) then
error("Command \"" .. name .. " already exists")
end
local command = {
Args = values.Args,
Function = values.Func,
Help = values.Help,
Name = name,
PrivilegeCheck = values.PrivilegeCheck,
Silent = values.Silent ~= nil and values.Silent,
BotAware = values.BotAware ~= nil and values.BotAware
}
self.Commands[name] = command
end
function Bot:UnregisterCommand(commandName)
self.Commands[commandName:lower()] = nil
end
local prefixes = {
function (content, guild)
local prefix = Config.Prefix
if guild then
local serverconfig = Bot:GetModuleForGuild(guild, "serverconfig")
if serverconfig then
local config = serverconfig:GetConfig(guild)
if config then
prefix = config.Prefix
end
end
end
return content:startswith(prefix, true) and content:sub(#prefix + 1) or nil
end,
function (content)
local userPing, rest = content:match("<@!?(%d+)>%s*(.+)")
return userPing and userPing == client.user.id and rest or nil
end
}
client:on('messageCreate', function(message)
if (not Bot:IsPublicChannel(message.channel)) then
return
end
local content
for _, func in pairs(prefixes) do
content = func(message.content, message.guild)
if (content) then
break
end
end
if (not content) then
return
end
local commandName, args = content:match("^(%w+)%s*(.*)")
if (not commandName) then
return
end
commandName = commandName:lower()
local commandTable = Bot.Commands[commandName]
if (not commandTable) then
return
end
if (not commandTable.BotAware and message.author.bot) then
return
end
if (commandTable.PrivilegeCheck) then
local success, ret = Bot:ProtectedCall("Command " .. commandName .. " privilege check", commandTable.PrivilegeCheck, message.member)
if (not success) then
message:reply("An error occurred")
return
end
if (not ret) then
print(string.format("%s tried to use command %s on guild %s", message.author.tag, commandName, message.guild.name))
return
end
end
local args, err = Bot:ParseCommandArgs(message.member, commandTable.Args, string.GetArguments(args, #commandTable.Args))
if (not args) then
message:reply(err)
return
end
Bot:ProtectedCall("Command " .. commandName, commandTable.Function, message, table.unpack(args, 1, #commandTable.Args))
if (commandTable.Silent) then
message:delete()
end
end)
Bot:RegisterCommand({
Name = "help",
Args = {
{Name = "command", Type = Bot.ConfigType.String, Optional = true}
},
Silent = false,
Help = "Print commands list",
Func = function (message, commandName)
local member = message.member
local guild = message.guild
if (commandName) then
commandName = commandName:lower()
local commandTable = Bot.Commands[commandName]
if (not commandTable) then
return
end
if (commandTable.PrivilegeCheck) then
local success, ret = Bot:ProtectedCall("Command " .. commandName .. " privilege check", commandTable.PrivilegeCheck, member)
if (not success) then
message:reply("An error occurred")
return
end
if (not ret) then
print(string.format("%s tried to access command %s via help on guild %s", message.author.tag, commandName, guild.name))
return
end
end
local helpStr = commandTable.Help or "<none>"
if(type(helpStr) == "function") then -- localization
helpStr = commandTable.Help(guild)
end
message:reply({
embed = {
fields = {
{
name = string.format("**Command: %s**", commandName),
value = string.format("**Description:** %s\n**Usage:** %s %s", helpStr, commandName, Bot:BuildUsage(commandTable))
}
}
}
})
else
local commands = {}
for commandName, commandTable in pairs(Bot.Commands) do
local visible = true
if (commandTable.PrivilegeCheck) then
local success, ret = Bot:ProtectedCall("Command " .. commandName .. " privilege check", commandTable.PrivilegeCheck, member)
if (not success or not ret) then
visible = false
end
end
if (visible) then
table.insert(commands, commandTable)
end
end
table.sort(commands, function (a, b) return a.Name < b.Name end)
local fields = {}
for _, commandTable in pairs(commands) do
local helpStr = commandTable.Help or "<none>"
if(type(helpStr) == "function") then -- localization
helpStr = commandTable.Help(guild)
end
table.insert(fields, {
name = string.format("**Command: %s**", commandTable.Name),
value = string.format("**Description:** %s\n**Usage:** %s %s", helpStr, commandTable.Name, Bot:BuildUsage(commandTable))
})
end
message:reply({
embed = {
fields = fields
}
})
end
end
})