-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathserver.lua
588 lines (459 loc) · 14.8 KB
/
server.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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
local DAY_SECONDS = 60 * 60 * 24
local function createSQLColumn(name)
local p = promise.new()
local exists = MySQL.scalar.await("SHOW COLUMNS FROM `users` LIKE '" .. name .. "'")
if exists then
return p:resolve(false)
end
MySQL.query(([[
ALTER TABLE `users`
ADD COLUMN %s TEXT NULL DEFAULT "";
]]):format(name), function()
Citizen.Trace(("[INFO]: New SQL column created. Name: %s\n"):format(name))
p:resolve(true)
end)
return p
end
local function loadPlayerPunishment(player, xPlayer)
local result = MySQL.single.await("SELECT comserv, jail FROM users WHERE identifier = ?", { xPlayer.identifier })
for key, row in pairs(result) do
if type(row) == "string" and (row or ""):len() > 0 then
row = json.decode(row)
TriggerClientEvent("updatePlayerPunishment", player, key, row)
return
end
end
TriggerClientEvent("updatePlayerPunishment", player, "clear")
end
AddEventHandler("esx:playerLoaded", loadPlayerPunishment)
local function sendToDiscord(title, message, color)
if not WEBHOOK or WEBHOOK:len() <= 0 then
return
end
local embeds = {
{
["color"] = color,
["title"] = "**".. title .."**\n",
["description"] = message,
["footer"] = {
["text"] = "fl_punishment by FiveM Land",
},
}
}
PerformHttpRequest(WEBHOOK, function() end, "POST", json.encode({ embeds = embeds }), { ["Content-Type"] = "application/json" })
end
CreateThread(function()
Citizen.Await(createSQLColumn("comserv"))
Citizen.Await(createSQLColumn("jail"))
Citizen.Await(createSQLColumn("ban"))
for _, player in pairs(GetPlayers()) do
local xPlayer = ESX.GetPlayerFromId(player)
if xPlayer then
loadPlayerPunishment(player, xPlayer)
end
end
end)
function getPlayerPunishment(xPlayer, name)
if not (name == "comserv" or name == "jail") then
return false
end
if type(xPlayer) ~= "table" then
xPlayer = ESX.GetPlayerFromId(xPlayer)
end
local result = MySQL.scalar.await("SELECT ?? FROM users WHERE identifier = ?", { name, xPlayer.identifier })
if type(result) ~= "string" or (result or ""):len() <= 0 then
return false
end
return json.decode(result)
end
exports("getPlayerPunishment", getPlayerPunishment)
function getPunishmentUsers(selectedTab)
local result = MySQL.query.await(
"SELECT identifier, firstname, lastname, ?? FROM users WHERE NOT (?? = '' OR ?? = 'null')",
{ selectedTab, selectedTab, selectedTab }
)
local newResult = {}
for _, row in pairs(result) do
row[selectedTab] = json.decode(row[selectedTab])
row.name = row.firstname .. " " .. row.lastname
table.insert(newResult, row)
end
return newResult
end
ESX.RegisterServerCallback("requestPlayerPunishment", function(player, cb, name)
cb(getPlayerPunishment(player, name))
end)
ESX.RegisterServerCallback("decreaseComservCount", function(player, cb)
local xPlayer = ESX.GetPlayerFromId(player)
if not xPlayer then
return cb(false)
end
local comserv = getPlayerPunishment(xPlayer, "comserv")
if not comserv then
return cb(false)
end
comserv.count = (comserv.count or comserv.all or 0) - 1
if comserv.count <= 0 then
comserv = nil
if COMSERV.outCoords.Enable then
xPlayer.setCoords(COMSERV.outCoords.Coords)
end
end
MySQL.query("UPDATE users SET comserv = ? WHERE identifier = ?", {
json.encode(comserv),
xPlayer.identifier,
})
cb(comserv)
end)
ESX.RegisterServerCallback("increaseAdminJailTime", function(player, cb)
local xPlayer = ESX.GetPlayerFromId(player)
if not xPlayer then
return cb(false)
end
local jail = getPlayerPunishment(xPlayer, "jail")
if not jail then
return cb(false)
end
jail.count = (jail.count or 0) + 1
if jail.count >= (jail.all or 0) then
jail = nil
output(Translate("adminjail_over"), player)
end
MySQL.query("UPDATE users SET jail = ? WHERE identifier = ?", {
json.encode(jail),
xPlayer.identifier,
})
cb(jail)
end)
ESX.RegisterServerCallback("requestPunishmentUsers", function(player, cb, selectedTab)
local xPlayer = ESX.GetPlayerFromId(player)
if not xPlayer or not ADMIN_RANKS[xPlayer.getGroup()] then
return cb(false)
end
cb(getPunishmentUsers(selectedTab))
end)
ESX.RegisterServerCallback("removeUserFromPunishment", function(player, cb, selectedTab, identifier)
local xPlayer = ESX.GetPlayerFromId(player)
if not xPlayer or not ADMIN_RANKS[xPlayer.getGroup()] then
return cb(false)
end
MySQL.query.await("UPDATE users SET ?? = '' WHERE identifier = ?", { selectedTab, identifier })
local xTarget = ESX.GetPlayerFromIdentifier(identifier)
if xTarget then
if selectedTab == "comserv" then
TriggerClientEvent("updatePlayerPunishment", xTarget.source, "comserv", false)
elseif selectedTab == "jail" then
TriggerClientEvent("updatePlayerPunishment", xTarget.source, "jail", false)
end
end
output(Translate("removed_punishment"), player)
cb(getPunishmentUsers(selectedTab))
end)
ESX.RegisterServerCallback("requestPunishmentUserData", function(player, cb, selectedTab, identifier)
local xPlayer = ESX.GetPlayerFromId(player)
if not xPlayer or not ADMIN_RANKS[xPlayer.getGroup()] then
return cb(Translate("not_admin"))
end
local result = MySQL.query.await(
"SELECT identifier, firstname, lastname, accounts, job, ?? FROM users WHERE identifier = ?",
{ selectedTab, identifier }
)
if not result or #result <= 0 then
return cb(Translate("user_not_found"))
end
cb(false, result[1])
end)
RegisterCommand("punishments", function(player)
if not isAdmin(player) then
return
end
TriggerClientEvent("togglePunishmentsAdmin", player)
end, false)
RegisterCommand("comserv", function(player, args)
local xPlayer = ESX.GetPlayerFromId(player)
if not isAdmin(xPlayer) then
return
end
if #args < 3 then
return output(Translate('invalid_syntax'), player)
end
local xTarget = ESX.GetPlayerFromId(args[1])
if not xTarget then
return output(Translate("user_not_found"), player)
end
local count = tonumber(args[2])
if not count or count <= 0 then
return output(Translate("count_invalid"), player)
end
count = math.abs(math.floor(count))
table.remove(args, 1)
table.remove(args, 1)
local reason = table.concat(args, " ")
if getPlayerPunishment(xTarget, "jail") then
return output(Translate("player_in_jail"), player)
end
if getPlayerPunishment(xTarget, "comserv") then
return output(Translate("player_in_comserv"), player)
end
local adminName = GetPlayerName(player)
local comserv = {
count = count,
all = count,
reason = reason,
start = os.time(os.date("!*t")),
admin = {
name = adminName,
identifier = xPlayer.identifier,
},
}
MySQL.insert("UPDATE users SET comserv = ? WHERE identifier = ?", { json.encode(comserv), xTarget.identifier })
TriggerClientEvent("updatePlayerPunishment", xTarget.source, "comserv", comserv)
output(Translate("work_allocated", reason), player)
output(Translate("assigned_you", adminName, count), xTarget.source)
output(Translate("reason", reason), xTarget.source)
sendToDiscord(
"comserv",
Translate("comserv_log", adminName, GetPlayerName(xTarget.source), xTarget.getName(), count, reason)
, 15105570
)
end, false)
RegisterCommand("removecomserv", function(player, args)
if not isAdmin(player) then
return
end
if #args < 1 then
return output(Translate("invalid_syntax"), player)
end
local xTarget = ESX.GetPlayerFromId(args[1])
if not xTarget then
return output(Translate("user_not_found"), player)
end
local comserv = getPlayerPunishment(xTarget, "comserv")
if not comserv then
return output(Translate("player_not_in_comserv"), player)
end
exports.oxmysql:update("UPDATE users SET comserv = '' WHERE identifier = ?", { xTarget.identifier })
TriggerClientEvent("updatePlayerPunishment", xTarget.source, "comserv", false)
local adminName = GetPlayerName(player)
output(Translate("removed_from_comserv"), player)
output(Translate("removed_you_comserv", adminName), xTarget.source)
sendToDiscord(
"removecomserv",
Translate("removecomserv_log", adminName, GetPlayerName(xTarget.source), xTarget.getName())
, 15105570
)
end, false)
function banPlayer(admin, target, days, reason)
admin = (type(admin) == "number" or type(admin) == "string") and ESX.GetPlayerFromId(admin) or admin
target = (type(target) == "number" or type(target) == "string") and ESX.GetPlayerFromId(target) or target
if not isAdmin(admin) then
Citizen.Trace(("%s (%s) try ban player %s\n"):format(admin.getName(), admin.identifier, target.getName()))
return false
end
local currentTimestamp = os.time(os.date("!*t"))
local adminName = GetPlayerName(admin.source)
local ban = {
count = days,
start = currentTimestamp,
endDate = currentTimestamp + ((days == 0 and 3650 or days) * DAY_SECONDS),
reason = reason,
admin = {
name = adminName,
identifier = admin.identifier,
},
}
sendToDiscord(
"ban",
Translate("ban_log", adminName, GetPlayerName(target.source), target.getName(), target.identifier, days == 0 and "Infinity" or days, reason)
, 15105570
)
MySQL.query("UPDATE users SET ban = ? WHERE identifier = ?", { json.encode(ban), target.identifier })
Wait(1000)
DropPlayer(
target.source,
Translate("ban_message", adminName, days == 0 and Translate("infinity") or days, reason)
)
return true
end
exports("banPlayer", banPlayer)
RegisterCommand("ban", function(player, args)
local xPlayer = ESX.GetPlayerFromId(player)
if not xPlayer or not isAdmin(xPlayer) then
return
end
if #args < 2 then
return output(Translate("invalid_syntax"), player)
end
local xTarget = ESX.GetPlayerFromId(args[1])
if not xTarget then
return output(Translate("user_not_found"), player)
end
local days = tonumber(args[2])
if not days or days < 0 then
return output(Translate("invalid_days"), player)
end
days = math.floor(days)
table.remove(args, 1)
table.remove(args, 1)
local reason = table.concat(args, " ")
if reason:len() <= 0 then
reason = Translate("no_reason")
end
local targetCharName = xTarget.getName()
banPlayer(xPlayer, xTarget, days, reason)
output(Translate("you_banned", targetCharName), player)
output(Translate("days", days == 0 and Translate("infinity") or days), player)
output(Translate("reason", reason), player)
end)
AddEventHandler("playerConnecting", function(name, setKickReason, deferrals)
local player = source
local identifiers = GetPlayerIdentifiers(player)
local selectedId = false
deferrals.defer()
Wait(0)
for _, id in pairs(identifiers) do
if id:find("license:") then
selectedId = id:gsub("license:", "")
break
end
end
deferrals.update(Translate("checking_ban"))
local banQuery = MySQL.query.await("SELECT ban FROM users WHERE SUBSTRING_INDEX(identifier, ':', -1) = ?", { selectedId })
if not banQuery or #banQuery <= 0 then
return deferrals.done()
end
-- Fix multicharacter ban check
local result = nil
for _, row in pairs(banQuery) do
if row.ban and row.ban ~= "" then
result = row.ban
break
end
end
if not result then
return deferrals.done()
end
result = json.decode(result)
local currentTimestamp = os.time(os.date("!*t"))
if result.endDate > currentTimestamp then
return deferrals.done(
Translate("you_banned_from_server",
result.admin.name,
(result.count == 0 and Translate("infinity") or result.count),
os.date("%Y-%b-%d", result.endDate),
result.reason,
selectedId
)
)
else
Wait(1000)
deferrals.update(Translate("ban_clear"))
exports.oxmysql:update("UPDATE users SET ban = '' WHERE identifier = ?", { selectedId })
end
Wait(1000)
deferrals.done()
end)
RegisterCommand("unban", function(player, args)
local xPlayer = ESX.GetPlayerFromId(player)
if not xPlayer or not isAdmin(xPlayer) then
return
end
if #args < 1 then
return output(Translate("invalid_syntax"), player)
end
local input = table.concat(args, " ")
local result = MySQL.query.await(
"SELECT identifier, firstname, lastname, ban FROM users WHERE identifier = ? OR LOWER(CONCAT(firstname, ' ', lastname)) = ?",
{ input, input }
)
if not result or #result < 1 then
return output(Translate("user_not_found"), player)
end
result = result[1]
if result.ban:len() <= 0 then
return output(Translate("player_not_banned"), player)
end
exports.oxmysql:update("UPDATE users SET ban = '' WHERE identifier = ?", { result.identifier })
local charName = result.firstname .. " " .. result.lastname
output(Translate("player_unbanned", charName), player)
sendToDiscord(
"unban",
Translate("unban_log", GetPlayerName(player), charName, result.identifier)
, 15105570
)
end, false)
RegisterCommand("adminjail", function(player, args)
local xPlayer = ESX.GetPlayerFromId(player)
if not xPlayer or not isAdmin(xPlayer) then
return
end
if #args < 2 then
return output(Translate("invalid_syntax"), player)
end
local xTarget = ESX.GetPlayerFromId(args[1])
if not xTarget then
return output(Translate("user_not_found"), player)
end
if getPlayerPunishment(xTarget, "comserv") then
return output(Translate("player_in_comserv"), player)
end
if getPlayerPunishment(xTarget, "jail") then
return output(Translate("player_in_jail"), player)
end
local time = tonumber(args[2])
if not time then
return output(Translate("time_not_number"), player)
end
time = math.abs(math.floor(time))
table.remove(args, 1)
table.remove(args, 1)
local reason = table.concat(args, " ")
local adminName = GetPlayerName(player)
local currentTimestamp = os.time(os.date("!*t"))
local jail = {
count = 0,
start = currentTimestamp,
all = time,
reason = reason,
admin = {
name = adminName,
identifier = xPlayer.identifier,
},
}
exports.oxmysql:update("UPDATE users SET jail = ? WHERE identifier = ?", { json.encode(jail), xTarget.identifier })
TriggerClientEvent("updatePlayerPunishment", xTarget.source, "jail", jail)
output(Translate("jail_allocated", reason), player)
output(Translate("jail_assigned", adminName, time), xTarget.source)
output(Translate("reason", reason), xTarget.source)
sendToDiscord(
"adminjail",
Translate("adminjail_log", adminName, GetPlayerName(xTarget.source), xTarget.getName(), time, reason)
, 15105570
)
end, false)
RegisterCommand("unjail", function(player, args)
local xPlayer = ESX.GetPlayerFromId(player)
if not xPlayer or not isAdmin(xPlayer) then
return
end
if #args < 1 then
return output(Translate("invalid_syntax"), player)
end
local xTarget = ESX.GetPlayerFromId(args[1])
if not xTarget then
return output(Translate("user_not_found"))
end
if not getPlayerPunishment(xTarget, "jail") then
return output(Translate("player_not_in_jail"), player)
end
local adminName = GetPlayerName(player)
exports.oxmysql:update("UPDATE users SET jail = '' WHERE identifier = ?", { xTarget.identifier })
TriggerClientEvent("updatePlayerPunishment", xTarget.source, "jail", false)
output(Translate("you_remove_jail"), player)
output(Translate("removed_from_jail", adminName), xTarget.source)
sendToDiscord(
"unjail",
Translate("unjail_log", adminName, GetPlayerName(xTarget.source), xTarget.getName())
, 15105570
)
end)