-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.lua
549 lines (518 loc) · 15.4 KB
/
api.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
local AdminAPI = {
["Debug"] = false,
["Commands"] = {},
["CommandArgs"] = {},
["Prefix"] = ";",
}
local Players = game:GetService("Players")
local wait, spawn = wait, spawn
local isNumber = function(str)
if tonumber(str) ~= nil or str == "inf" then
return true
end
end
local FindInTable = function(tbl, val)
if not tbl then return false end
for _, v in pairs(tbl) do
if v == val then return true end
end
return false
end
local getCharacter = function(player)
player = player or Players.LocalPlayer
return player.Character
end
local getHum = function(character)
character = character or getCharacter()
return character:FindFirstChildOfClass("Humanoid")
end
local getBp = function(player)
player = player or Players.LocalPlayer
return player:FindFirstChildOfClass("Backpack")
end
local getRoot = function(character)
character = character or getCharacter()
return character:FindFirstChild("HumanoidRootPart") or character:FindFirstChild("Torso") or character:FindFirstChild("UpperTorso")
end
local findCmd = function(cmd_name)
for i,v in pairs(AdminAPI.Commands) do
if string.lower(v.NAME) == string.lower(cmd_name) or FindInTable(v.ALIAS, string.lower(cmd_name)) then
return v
end
end
end
local splitString = function(str, delim)
local broken = {}
if not delim then delim = "," end
for w in string.gmatch(str, "[^" .. delim .. "]+") do
table.insert(broken, w)
end
return broken
end
local SpecialPlayerCases = {
["all"] = function(speaker) return Players:GetPlayers() end,
["others"] = function(speaker)
local plrs = {}
for i,v in pairs(Players:GetPlayers()) do
if v ~= speaker then
table.insert(plrs, v)
end
end
return plrs
end,
["me"] = function(speaker) return {speaker} end,
["#(%d+)"] = function(speaker, args, currentList)
local returns = {}
local randAmount = tonumber(args[1])
local players = {unpack(currentList)}
for i = 1,randAmount do
if #players == 0 then break end
local randIndex = math.random(1, #players)
table.insert(returns, players[randIndex])
table.remove(players, randIndex)
end
return returns
end,
["random"] = function(speaker, args, currentList)
local players = currentList
return {players[math.random(1, #players)]}
end,
["%%(.+)"] = function(speaker, args)
local returns = {}
local team = args[1]
for _,plr in pairs(Players:GetPlayers()) do
if plr.Team and string.sub(string.lower(plr.Team.Name), 1, #team) == string.lower(team) then
table.insert(returns, plr)
end
end
return returns
end,
["allies"] = function(speaker)
local returns = {}
local team = speaker.Team
for _,plr in pairs(Players:GetPlayers()) do
if plr.Team == team then
table.insert(returns, plr)
end
end
return returns
end,
["enemies"] = function(speaker)
local returns = {}
local team = speaker.Team
for _,plr in pairs(Players:GetPlayers()) do
if plr.Team ~= team then
table.insert(returns, plr)
end
end
return returns
end,
["team"] = function(speaker)
local returns = {}
local team = speaker.Team
for _,plr in pairs(Players:GetPlayers()) do
if plr.Team == team then
table.insert(returns, plr)
end
end
return returns
end,
["nonteam"] = function(speaker)
local returns = {}
local team = speaker.Team
for _,plr in pairs(Players:GetPlayers()) do
if plr.Team ~= team then
table.insert(returns, plr)
end
end
return returns
end,
["friends"] = function(speaker, args)
local returns = {}
for _,plr in pairs(Players:GetPlayers()) do
if plr:IsFriendsWith(speaker.UserId) and plr ~= speaker then
table.insert(returns, plr)
end
end
return returns
end,
["nonfriends"] = function(speaker, args)
local returns = {}
for _,plr in pairs(Players:GetPlayers()) do
if not plr:IsFriendsWith(speaker.UserId) and plr ~= speaker then
table.insert(returns, plr)
end
end
return returns
end,
["guests"] = function(speaker, args)
local returns = {}
for _,plr in pairs(Players:GetPlayers()) do
if plr.Guest then
table.insert(returns, plr)
end
end
return returns
end,
["bacons"] = function(speaker, args)
local returns = {}
for _,plr in pairs(Players:GetPlayers()) do
if plr.Character:FindFirstChild("Pal Hair") or plr.Character:FindFirstChild("Kate Hair") then
table.insert(returns, plr)
end
end
return returns
end,
["age(%d+)"] = function(speaker, args)
local returns = {}
local age = tonumber(args[1])
if not age then return end
for _,plr in pairs(Players:GetPlayers()) do
if plr.AccountAge <= age then
table.insert(returns, plr)
end
end
return returns
end,
["nearest"] = function(speaker, args, currentList)
local speakerChar = speaker.Character
if not speakerChar or not getRoot(speakerChar) then return end
local lowest = math.huge
local NearestPlayer = nil
for _,plr in pairs(currentList) do
if plr ~= speaker and plr.Character then
local distance = plr:DistanceFromCharacter(getRoot(speakerChar).Position)
if distance < lowest then
lowest = distance
NearestPlayer = {plr}
end
end
end
return NearestPlayer
end,
["farthest"] = function(speaker, args, currentList)
local speakerChar = speaker.Character
if not speakerChar or not getRoot(speakerChar) then return end
local highest = 0
local Farthest = nil
for _,plr in pairs(currentList) do
if plr ~= speaker and plr.Character then
local distance = plr:DistanceFromCharacter(getRoot(speakerChar).Position)
if distance > highest then
highest = distance
Farthest = {plr}
end
end
end
return Farthest
end,
["group(%d+)"] = function(speaker, args)
local returns = {}
local groupID = tonumber(args[1])
for _,plr in pairs(Players:GetPlayers()) do
if plr:IsInGroup(groupID) then
table.insert(returns, plr)
end
end
return returns
end,
["alive"] = function(speaker, args)
local returns = {}
for _,plr in pairs(Players:GetPlayers()) do
if plr.Character and plr.Character:FindFirstChildOfClass("Humanoid") and plr.Character:FindFirstChildOfClass("Humanoid").Health > 0 then
table.insert(returns, plr)
end
end
return returns
end,
["dead"] = function(speaker, args)
local returns = {}
for _,plr in pairs(Players:GetPlayers()) do
if (not plr.Character or not plr.Character:FindFirstChildOfClass("Humanoid")) or plr.Character:FindFirstChildOfClass("Humanoid").Health <= 0 then
table.insert(returns, plr)
end
end
return returns
end,
["rad(%d+)"] = function(speaker, args)
local returns = {}
local radius = tonumber(args[1])
local speakerChar = speaker.Character
if not speakerChar or not getRoot(speakerChar) then return end
for _,plr in pairs(Players:GetPlayers()) do
if plr.Character and getRoot(plr.Character) then
local magnitude = (getRoot(plr.Character).Position-getRoot(speakerChar).Position).magnitude
if magnitude <= radius then table.insert(returns, plr) end
end
end
return returns
end
}
local addspecialplayerCase = function(str, func)
SpecialPlayerCases[tostring(str)] = func
end
local removespecialplayerCase = function(str)
SpecialPlayerCases[tostring(str)] = nil
end
local toTokens = function(str)
local tokens = {}
for op,name in string.gmatch(str, "([+-])([^+-]+)") do table.insert(tokens, {["Operator"] = op, ["Name"] = name}) end
return tokens
end
local onlyIncludeInTable = function(tab, matches)
local matchTable = {}
local resultTable = {}
for i,v in pairs(matches) do matchTable[v.Name] = true end
for i,v in pairs(tab) do if matchTable[v.Name] then table.insert(resultTable, v) end end
return resultTable
end
local removeTableMatches = function(tab, matches)
local matchTable = {}
local resultTable = {}
for i,v in pairs(matches) do matchTable[v.Name] = true end
for i,v in pairs(tab) do if not matchTable[v.Name] then table.insert(resultTable, v) end end
return resultTable
end
local getPlayersByName = function(Name)
local Name, Len, Found = string.lower(Name), #Name, {}
for _,v in pairs(Players:GetPlayers()) do
if string.sub(Name, 0, 1) == "@" then
if string.sub(string.lower(v.Name), 1, Len - 1) == string.sub(Name, 2) then
table.insert(Found, v)
end
else
if string.sub(string.lower(v.Name), 1, Len) == Name or string.sub(string.lower(v.DisplayName), 1, Len) == Name then
table.insert(Found, v)
end
end
end
return Found
end
local getPlayer = function(list, speaker)
if not list then return {speaker.Name} end
local nameList = splitString(list, ",")
local foundList = {}
for _,name in pairs(nameList) do
if string.sub(name, 1, 1) ~= "+" and string.sub(name, 1, 1) ~= "-" then name = "+" .. name end
local tokens = toTokens(name)
local initialPlayers = Players:GetPlayers()
for i,v in pairs(tokens) do
if v.Operator == "+" then
local tokenContent = v.Name
local foundCase = false
for regex,case in pairs(SpecialPlayerCases) do
local matches = {string.match(tokenContent, "^" .. regex .. "$")}
if #matches > 0 then
foundCase = true
initialPlayers = onlyIncludeInTable(initialPlayers, case(speaker, matches, initialPlayers))
end
end
if not foundCase then
initialPlayers = onlyIncludeInTable(initialPlayers, getPlayersByName(tokenContent))
end
else
local tokenContent = v.Name
local foundCase = false
for regex,case in pairs(SpecialPlayerCases) do
local matches = {string.match(tokenContent, "^" .. regex .. "$")}
if #matches > 0 then
foundCase = true
initialPlayers = removeTableMatches(initialPlayers, case(speaker, matches, initialPlayers))
end
end
if not foundCase then
initialPlayers = removeTableMatches(initialPlayers, getPlayersByName(tokenContent))
end
end
end
for i,v in pairs(initialPlayers) do table.insert(foundList, v) end
end
local foundNames = {}
for i,v in pairs(foundList) do table.insert(foundNames, v.Name) end
return foundNames
end
local cmdHistory = {}
local lastCmds = {}
local split = " "
local lastBreakTime = 0
local execCmd = function(cmdStr, speaker, store)
cmdStr = string.gsub(cmdStr, "%s+$", "")
spawn(function()
local rawCmdStr = cmdStr
cmdStr = string.gsub(cmdStr, "\\\\", "%%BackSlash%%")
local commandsToRun = splitString(cmdStr, "\\")
for i,v in pairs(commandsToRun) do
v = string.gsub(v, "%%BackSlash%%", "\\")
local x, y, num = string.find(v, "^(%d+)%^")
local cmdDelay = 0
local infTimes = false
if num then
v = string.sub(v, y + 1)
local x, y, del = string.find(v, "^([%d%.]+)%^")
if del then
v = string.sub(v, y + 1)
cmdDelay = tonumber(del) or 0
end
else
local x,y = string.find(v, "^inf%^")
if x then
infTimes = true
v = string.sub(v, y + 1)
local x, y, del = string.find(v, "^([%d%.]+)%^")
if del then
v = string.sub(v, y + 1)
del = tonumber(del) or 1
cmdDelay = (del > 0 and del or 1)
else
cmdDelay = 1
end
end
end
num = tonumber(num or 1)
if string.sub(v, 1, 1) == "!" then
local chunks = splitString(string.sub(v, 2), split)
if chunks[1] and lastCmds[chunks[1]] then v = lastCmds[chunks[1]] end
end
local args = splitString(v, split)
local cmdName = args[1]
local cmd = findCmd(cmdName)
if cmd then
table.remove(args, 1)
AdminAPI.CommandArgs = args
if not speaker then speaker = Players.LocalPlayer end
if store then
if speaker == Players.LocalPlayer then
if cmdHistory[1] ~= rawCmdStr and string.sub(rawCmdStr, 1, 11) ~= "lastcommand" and string.sub(rawCmdStr, 1, 7) ~= "lastcmd" then
table.insert(cmdHistory, 1, rawCmdStr)
end
end
if #cmdHistory > 30 then table.remove(cmdHistory) end
lastCmds[cmdName] = v
end
local cmdStartTime = tick()
if infTimes then
while lastBreakTime < cmdStartTime do
local success, err = pcall(cmd.FUNC, args, speaker)
if not success and AdminAPI.Debug then
warn("Command Error:", cmdName, err)
end
wait(cmdDelay)
end
else
for rep = 1,num do
if lastBreakTime > cmdStartTime then break end
local success, err = pcall(function()
cmd.FUNC(args, speaker)
end)
if not success and AdminAPI.Debug then
warn("Command Error:", cmdName, err)
end
if cmdDelay ~= 0 then wait(cmdDelay) end
end
end
end
end
end)
end
local getstring = function(begin)
local start = begin - 1
local AA = "" for i,v in pairs(AdminAPI.CommandArgs) do
if i > start then
if AA ~= "" then
AA = AA .. " " .. v
else
AA = AA .. v
end
end
end
return AA
end
local getRandomString = function()
return string.sub(string.gsub(game:GetService("HttpService"):GenerateGUID(false), "-", ""), 1, math.random(25, 30))
end
local createChatHook = function()
local getprfx = function(strn)
if string.sub(strn, 1, string.len(AdminAPI.Prefix)) == AdminAPI.Prefix then return {"cmd", string.len(AdminAPI.Prefix) + 1} end return
end
local do_exec = function(str, plr)
str = string.gsub(str, "/e ", "")
local t = getprfx(str)
if not t then return end
str = string.sub(str, t[2])
if t[1] == "cmd" then
execCmd(str, plr, true)
end
end
Players.LocalPlayer.Chatted:Connect(function(message)
spawn(function()
wait()
do_exec(string.lower(tostring(message)), Players.LocalPlayer)
end)
end)
end
local addcmdtotable = function(name, alias, func)
local Tag = #AdminAPI.Commands + 1
AdminAPI.Commands[Tag] = {
["NAME"] = name,
["ALIAS"] = alias or {},
["FUNC"] = func
}
local DestroyFunc = function()
AdminAPI.Commands[Tag] = nil
end
return {["Destroy"]=DestroyFunc,["Remove"]=DestroyFunc,["Delete"]=DestroyFunc}
end
local writeOverCommand = function(cmd, func)
cmd = tostring(cmd) or " "
if cmd ~= " " then
if type(func) == "function" then
cmd = string.lower(cmd)
for i = #AdminAPI.Commands, 1, -1 do
if AdminAPI.Commands[i].NAME == cmd or FindInTable(AdminAPI.Commands[i].ALIAS, cmd) then
AdminAPI.Commands[i].FUNC = func
end
end
end
end
end
local makeNotification = function(...)
local arguments = {...}
if arguments[1] and arguments[2] then
game:GetService("StarterGui"):SetCore("SendNotification", {
["Title"] = tostring(arguments[1]),
["Text"] = tostring(arguments[2])
})
end
if arguments[1] and not arguments[2] then
game:GetService("StarterGui"):SetCore("SendNotification", {
["Title"] = "admin",
["Text"] = tostring(arguments[1])
})
end
end
AdminAPI.GetPlayer = getPlayer
AdminAPI.ExecuteCommand = execCmd
AdminAPI.GetString = getstring
AdminAPI.FindCommand = findCmd
AdminAPI.SearchCommand = findCmd
AdminAPI.IsNumber = isNumber
AdminAPI.RandomString = getRandomString
AdminAPI.CreateChatHook = createChatHook
AdminAPI.CreateCommand = addcmdtotable
AdminAPI.NewCommand = addcmdtotable
AdminAPI.AddCommand = addcmdtotable
AdminAPI.GetCharacter = getCharacter
AdminAPI.GetHumanoid = getHum
AdminAPI.GetBackpack = getBp
AdminAPI.GetRoot = getRoot
AdminAPI.Notify = makeNotification
AdminAPI.CreateSpecialPlayerCase = addspecialplayerCase
AdminAPI.NewSpecialPlayerCase = addspecialplayerCase
AdminAPI.AddSpecialPlayerCase = addspecialplayerCase
AdminAPI.RemoveSpecialPlayerCase = removespecialplayerCase
AdminAPI.DeleteSpecialPlayerCase = removespecialplayerCase
AdminAPI.DestroySpecialPlayerCase = removespecialplayerCase
AdminAPI.ReplaceCommand = writeOverCommand
AdminAPI.RewriteCommand = writeOverCommand
AdminAPI.FindInTable = FindInTable
return AdminAPI