-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.lua
316 lines (251 loc) · 6.2 KB
/
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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
local ffi = require("ffi")
local dis = import("discordutil")
local bot = import("botutil")
local CMD_HIDEFROMHELP, CMD_FILTERISBLACKLIST, CMD_RAWCONTENT = 1, 2, 4
local function PermAll()
return true
end
local PermAdmins
do
local disIsAdmin = dis.IsAdmin
function PermAdmins(m)
return disIsAdmin(m)
end
end
local Commands = {}
local CM = {}
CM.__index = CM
function CM:Alias(alias)
Commands[alias] = self
local a = self.aliases
a[#a + 1] = alias
end
function CM:Desc(desc)
self.description = desc
end
function CM:Permission(perm)
local p = self.permissions
p[#p + 1] = perm
end
function CM:Filter(f)
self.filters[f] = true
self.filtersused = true
end
function CM:Callback(c)
self.callback = c
end
do
local bitband, disIsAdmin, stringlower = bit.band, dis.IsAdmin, string.lower
function CM:CheckPerms(channel, cat, member)
if self.filtersused and not disIsAdmin(member) then
cat = stringlower(cat)
if bitband(self.flags, CMD_FILTERISBLACKLIST) ~= 0 then
local f = self.filters
if f[channel] or f[cat] then return false end
else
local f = self.filters
if not f[channel] and not f[cat] then return false end
end
end
local p = self.permissions
for i = 1, #p do
if p[i](member) then return true end
end
return false
end
end
do
local bitbor = bit.bor
function CM:AddFlag(flag)
self.flags = bitbor(self.flags, flag)
end
end
local function New(name)
local c = setmetatable({
name = name,
aliases = {},
description = "",
permissions = {},
filters = {},
filtersused = false,
flags = 0,
}, CM)
Commands[name] = c
Commands[#Commands + 1] = c
return c
end
local Parse
do
local buffer = ffi.typeof("char [?]")
local fficopy, ffistring = ffi.copy, ffi.string
function Parse(s, len)
local t, tlen, lastspace, openquote, closequote, quotefixup = {}, 0, 0, 0, false, false
do
local caret = 0
while caret < len do
local char = s[caret]
if not closequote and char == 32 then
do
local sublen = caret - lastspace
if sublen ~= 0 then
tlen = tlen + 1
t[tlen] = ffistring(s + lastspace, sublen)
end
end
lastspace = caret + 1
elseif char == 34 then
if caret ~= 0 and s[caret - 1] == 92 then
quotefixup = true
goto skip
end
if closequote then
do
local sublen = caret - openquote
tlen = tlen + 1
if quotefixup then
local buf = buffer(sublen)
local p = buf
local qcar, lastq = openquote, openquote
while qcar < caret do
local qchar = s[qcar]
if qchar == 34 and qcar ~= openquote and s[qcar - 1] == 92 then
local qlen = qcar - lastq - 1
fficopy(p, s + lastq, qlen)
p = p + qlen
p[0], p = 34, p + 1
lastq = qcar + 1
end
qcar = qcar + 1
end
if caret ~= lastq then
local qlen = qcar - lastq
fficopy(p, s + lastq, qlen)
p = p + qlen
end
t[tlen] = ffistring(buf, p - buf)
else
do
local sublen = openquote - 1 - lastspace
if sublen ~= 0 then
t[tlen] = ffistring(s + lastspace, sublen)
tlen = tlen + 1
end
end
t[tlen] = ffistring(s + openquote, sublen)
end
end
lastspace = caret + 1
closequote = false
else
closequote = true
openquote = caret + 1
end
end
::skip::
caret = caret + 1
end
end
if len ~= lastspace then
t[tlen + 1] = ffistring(s + lastspace, len - lastspace)
end
return t
end
end
do
local cmd = New("help")
cmd:Desc("Shows all available commands. `!help`")
cmd:Permission(PermAll)
do
local ipairs, tableconcat = ipairs, table.concat
local bitband = bit.band
cmd:Callback(function(msg)
local e = bot.CreateEmbed()
e:Title("Toolgun Help")
local t, len = {}, 0
local member = msg.member
for k, v in ipairs(Commands) do
if bitband(v.flags, CMD_HIDEFROMHELP) == 0 then
local p = v.permissions
for i = 1, #p do
if p[i](member) then
len = len + 1
local aliases = ""
if #v.aliases ~= 0 then
local a = v.aliases
for ai = 1, #a do
t[len + ai - 1] = "`!" .. a[ai] .. "`"
end
aliases = " (" .. tableconcat(t, ", ", len, len + #a - 1) .. ")"
end
t[len] = "`!" .. v.name .. "`" .. aliases .. "\n> " .. v.description .. "\n"
break
end
end
end
end
local desc = tableconcat(t, "\n", 1, len)
if #desc < 2000 then
e:Description(desc)
else
e:Description(tableconcat(t, "\n", 1, math.floor(len / 2)))
end
if dis.IsAdmin(msg.member) then
if #desc < 2000 then
bot.Reply(msg, {
content = dis.Mention(msg.member),
embed = e:Export()
})
else
local e2 = bot.CreateEmbed()
e2:Description(tableconcat(t, "\n", math.floor(len / 2), len))
bot.Reply(msg, {
content = dis.Mention(msg.member),
embed = e:Export()
})
bot.Reply(msg, {
embed = e2:Export()
})
end
else
dis.GetChannel("general"):send({
content = dis.Mention(msg.member),
embed = e:Export()
})
end
end)
end
end
local Run
do
local tableremove = table.remove
local stringsub, stringlower = string.sub, string.lower
local bitband = bit.band
local tochar = ffi.typeof("const char *")
function Run(msg)
if stringsub(msg.content, 1, 1) ~= "!" then return false end
local cc = msg.content
local args = Parse(tochar(cc), #cc)
local command = stringlower(stringsub(tableremove(args, 1), 2))
local n = string.find(command, "\n", 1, true)
if n then
table.insert(args, 1, command:sub(n + 1))
command = command:sub(1, n-1)
end
local cmd = Commands[command]
if not cmd then return false end
if cmd:CheckPerms(msg.channel.name, msg.channel.category.name, msg.member) then
cmd.callback(msg, args, stringsub(bitband(cmd.flags, CMD_RAWCONTENT) ~= 0 and msg.content or cc, #command + 3))
return true
end
return false
end
end
return {
New = New,
Run = Run,
CMD_HIDEFROMHELP = CMD_HIDEFROMHELP,
CMD_FILTERISBLACKLIST = CMD_FILTERISBLACKLIST,
CMD_RAWCONTENT = CMD_RAWCONTENT,
PermAll = PermAll,
PermAdmins = PermAdmins,
}