Hi, I had to do this code for someone for whom the new Essentialmode ban system was not working so I decide to share it.
It is a basic system that checks if a player who is banned is connected to the server.
If this is the case, a check is carried out every 15 seconds to exclude them.
Apparently, the connection time is enough to make the check and the player is instantly expelled.
The script require Essentialmode and the bans table in the DB.
To use it, you just need to add this code to an existing resource or create one. Do not forget to add server_script '@mysql-async/lib/MySQL.lua'
in the file _resource.lua if it is not already there.
local bannedUser = {}
TriggerEvent('es:addGroupCommand', 'banplayer', "admin", function(source, args, user)
if args[2] then
local src = args[2]
local identifier = GetPlayerIdentifiers(src)[1]
MySQL.Sync.execute('INSERT INTO bans (identifier, time, raison, duree) VALUES (@identifier, "1", "raison", "1")', {['@identifier'] = identifier})
table.insert(bannedUser, identifier)
end
end, function(source, args, user)
TriggerClientEvent('chatMessage', source, "SYSTEM", {255, 0, 0}, "Insufficienct permissions!")
end, {help = "Ban a player"})
AddEventHandler('onMySQLReady', function()
local result = MySQL.Sync.fetchAll('SELECT * FROM bans')
for i=1, #result do
table.insert(bannedUser, result[i].identifier)
end
end)
local function checkBannedUser()
SetTimeout(15000, function()
TriggerEvent('es:getPlayers', function(Users)
for k, v in pairs(Users) do
if Users[k] ~= nil then
local src = v.get('source')
local identifier = v.get('identifier')
for _, v in ipairs(bannedUser) do
if v == identifier then
DropPlayer(src, "You are banned from this server !")
end
end
end
end
end)
checkBannedUser()
end)
end
checkBannedUser()