From be15684c23cf27636d92d7a561d6779e2616a6d5 Mon Sep 17 00:00:00 2001 From: Musiker15 Date: Sat, 31 Aug 2024 21:39:24 +0200 Subject: [PATCH] Update v2.4.1 * Fixed the Bansystem --- client/main.lua | 18 ++- config.lua | 2 +- fxmanifest.lua | 2 +- server/functions/bansystem.lua | 224 ++++++++++++++++----------------- server/versionchecker.lua | 2 +- 5 files changed, 129 insertions(+), 119 deletions(-) diff --git a/client/main.lua b/client/main.lua index e3be408..bb8286f 100644 --- a/client/main.lua +++ b/client/main.lua @@ -42,7 +42,7 @@ if MSK.Bridge.Framework.Type == 'ESX' then TriggerServerEvent(MSK.Bridge.Framework.Events.setPlayerData) end) - RegisterNetEvent('esx:playerLoaded', function(xPlayer) + RegisterNetEvent('esx:playerLoaded', function(xPlayer, isNew, skin) MSK.Bridge.isPlayerLoaded = true TriggerEvent(MSK.Bridge.Framework.Events.playerLoaded, MSK.Bridge.Player) end) @@ -93,4 +93,18 @@ GetLib = function() return MSK end exports('GetLib', GetLib) -exports('getCoreObject', GetLib) -- Support for old Versions \ No newline at end of file +exports('getCoreObject', GetLib) -- Support for old Versions + +if Config.BanSystem.enable and Config.BanSystem.commands.enable then + CreateThread(function() + TriggerEvent('chat:addSuggestion', '/' .. Config.BanSystem.commands.ban, 'Ban a Player', { + {name = "targetId", help = "ServerId"}, + {name = "time", help = "1M = 1 Minute / 1H = 1 Hour / 1D = 1 Day / 1W = 1 Week / P = Permanent"}, + {name = "reason", help = "Reason"}, + }) + + TriggerEvent('chat:addSuggestion', '/' .. Config.BanSystem.commands.unban, 'Unban a Player', { + {name = "banId", help = "BanId"} + }) + end) +end \ No newline at end of file diff --git a/config.lua b/config.lua index e2714a0..1156696 100644 --- a/config.lua +++ b/config.lua @@ -84,6 +84,6 @@ Config.BanSystem = { enable = false, groups = {'superadmin', 'admin', 'god'}, ban = 'banPlayer', - unbank = 'unbanPlayer' + unban = 'unbanPlayer' } } \ No newline at end of file diff --git a/fxmanifest.lua b/fxmanifest.lua index 32eecb9..dafd346 100644 --- a/fxmanifest.lua +++ b/fxmanifest.lua @@ -4,7 +4,7 @@ games { 'gta5' } author 'Musiker15 - MSK Scripts' name 'msk_core' description 'Functions for MSK Scripts' -version '2.4.0' +version '2.4.1' lua54 'yes' diff --git a/server/functions/bansystem.lua b/server/functions/bansystem.lua index 712587c..a9a1aeb 100644 --- a/server/functions/bansystem.lua +++ b/server/functions/bansystem.lua @@ -1,14 +1,14 @@ -- Insert you Discord Webhook here local webhookLink = "https://discord.com/api/webhooks/" -banLog = function(source, bannedby, targetId, time, reason, playerIds, banId) +local banLog = function(playerId, bannedby, targetId, targetName, banTime, reason, playerIds, banId) if not Config.BanSystem.discordLog then return end local botColor = Config.BanSystem.botColor local botName = Config.BanSystem.botName local botAvatar = Config.BanSystem.botAvatar local title = "MSK Bansystem" - local description = ('Player %s (ID: %s) banned the Player %s (ID: %s) for %s until %s'):format(bannedby, source or 0, GetPlayerName(targetId), targetId, reason, time) + local description = ('Player %s (ID: %s) was banned by %s (ID: %s) for %s until %s. BanID: %s'):format(targetName, targetId, bannedby, playerId or 0, reason, banTime, banId) local fields = { {name = "Some IDs", value = playerIds}, } @@ -21,14 +21,14 @@ banLog = function(source, bannedby, targetId, time, reason, playerIds, banId) MSK.AddWebhook(webhookLink, botColor, botName, botAvatar, title, description, fields, footer, time) end -unbanLog = function(source, unbannedby, banId) +local unbanLog = function(playerId, unbannedby, banId) if not Config.BanSystem.discordLog then return end local botColor = Config.BanSystem.botColor local botName = Config.BanSystem.botName local botAvatar = Config.BanSystem.botAvatar local title = "MSK Bansystem" - local description = ('Player %s (ID: %s) unbanned. BanID %s'):format(unbannedby, source or 0, banId) + local description = ('BanID %s was unbanned by Player %s (ID: %s)'):format(banId, unbannedby, playerId or 0) local fields = false local footer = { text = "© MSK Scripts", @@ -43,100 +43,99 @@ local bannedPlayers = {} AddEventHandler('onResourceStart', function(resourceName) if GetCurrentResourceName() ~= resourceName then return end - if not Config.BanSystem.enable then return end - - local createTable = MySQL.query.await("CREATE TABLE IF NOT EXISTS msk_bansystem (`id` int(10) NOT NULL AUTO_INCREMENT, `ids` longtext DEFAULT NULL, `time` text NULL, `reason` text NOT NULL, `bannedby` varchar(80) NOT NULL, PRIMARY KEY (`id`));") - if createTable.warningStatus == 0 then - logging('debug', '^2 Successfully ^3 created ^2 table ^3 msk_bansystem ^0') - end + if not Config.BanSystem.enable then return end + MySQL.query.await("CREATE TABLE IF NOT EXISTS msk_bansystem (`id` int(10) NOT NULL AUTO_INCREMENT, `ids` longtext DEFAULT NULL, `time` text NULL, `reason` text NOT NULL, `bannedby` varchar(80) NOT NULL, PRIMARY KEY (`id`));") local data = MySQL.query.await("SELECT * FROM msk_bansystem") if not data then return end for k, v in pairs(data) do - table.insert(bannedPlayers, {id = v.id, ids = json.decode(v.ids), reason = v.reason, time = v.time, from = v.bannedby}) + bannedPlayers[#bannedPlayers + 1] = {id = v.id, ids = json.decode(v.ids), reason = v.reason, time = v.time, from = v.bannedby} + end +end) + +AddEventHandler('playerConnecting', function(playerName, setKickReason, deferrals) + local playerId = source + if not Config.BanSystem.enable then return end + local isBanned, expired = MSK.IsPlayerBanned(playerId) + + if isBanned and not expired then + CancelEvent() -- FiveM Native Function for cancelling the currently executing event + setKickReason(('Banned by %s until %s for %s. BanID: %s'):format(isBanned.from, isBanned.time, isBanned.reason, isBanned.id)) + elseif isBanned and expired then + MSK.UnbanPlayer(nil, isBanned.id) end end) -local split = function(s, delimiter) - local result = {} - - for match in (s..delimiter):gmatch("(.-)"..delimiter) do - table.insert(result, match) - end +local formatTime = function(time) + local banTime = 0 - return result + if time:find('P') then + banTime = os.time() + (60 * 60 * 24 * 7 * 52 * 100) + elseif time:find('M') then + banTime = os.time() + (60 * MSK.Split(time, 'M')[1]) + elseif time:find('H') then + banTime = os.time() + (60 * 60 * MSK.Split(time, 'H')[1]) + elseif time:find('D') then + banTime = os.time() + (60 * 60 * 24 * MSK.Split(time, 'D')[1]) + elseif time:find('W') then + banTime = os.time() + (60 * 60 * 24 * 7 * MSK.Split(time, 'W')[1]) + end + + return banTime, os.date('%d-%m-%Y %H:%M', banTime) end -local isPlayerBanned = function(source, playerName) - local identifiers = GetPlayerIdentifiers(source) +MSK.IsPlayerBanned = function(playerId) + local identifiers = GetPlayerIdentifiers(playerId) local player = {} - player['name'] = playerName or GetPlayerName(source) + player.name = GetPlayerName(playerId) for _, v in pairs(identifiers) do - player[split(v, ':')[1]] = v + player[MSK.Split(v, ':')[1]] = v end - for k, v in pairs(bannedPlayers) do - for name, id in pairs(v.ids) do - if player[name] and id == player[name] then - local pattern = "(%d+)-(%d+)-(%d+) (%d+):(%d+)" - local timeToConvert = v.time - local day, month, year, hour, minute = timeToConvert:match(pattern) + for i = 1, #bannedPlayers do + local playerIds = bannedPlayers[i].ids + local timeUntil = bannedPlayers[i].time + + for name, id in pairs(playerIds) do + if player[name] and player[name] == id then + local day, month, year, hour, minute = timeUntil:match("(%d+)-(%d+)-(%d+) (%d+):(%d+)") local time = os.time({day = day, month = month, year = year, hour = hour, min = minute}) logging('debug', os.date('%d-%m-%Y %H:%M', os.time()), os.date('%d-%m-%Y %H:%M', time)) - if os.time() < time then - return v - else - return v, true + if os.time() > time then + return bannedPlayers[i], true end - break + return bannedPlayers[i], false end end end return false end -exports('isPlayerBanned', isPlayerBanned) - -local formatTime = function(time) - local banTime = 0 - - if time:find('P') then - banTime = os.time() + (60 * 60 * 24 * 7 * 52 * 100) - elseif time:find('M') then - banTime = os.time() + (60 * split(time, 'M')[1]) - elseif time:find('H') then - banTime = os.time() + (60 * 60 * split(time, 'H')[1]) - elseif time:find('D') then - banTime = os.time() + (60 * 60 * 24 * split(time, 'D')[1]) - elseif time:find('W') then - banTime = os.time() + (60 * 60 * 24 * 7 * split(time, 'W')[1]) - end - - return banTime, os.date('%d-%m-%Y %H:%M', banTime) -end +exports('IsPlayerBanned', MSK.IsPlayerBanned) +exports('isPlayerBanned', MSK.IsPlayerBanned) -- Support for old Version -MSK.BanPlayer = function(source, playerId, time, reason) - local playerName = GetPlayerName(playerId) +MSK.BanPlayer = function(playerId, targetId, time, reason) + local targetName = GetPlayerName(targetId) - if not playerName then - if source then Config.Notification(source, ('Player with ID %s not found!'):format(playerId)) end - return logging('debug', ('Player with ^2ID %s^0 not found!'):format(playerId)) + if not targetName then + if playerId then MSK.Notification(playerId, 'MSK Bansystem', ('Player with ID ~y~%s~s~ not found!'):format(targetId)) end + return logging('debug', ('Player with ^2ID %s^0 not found!'):format(targetId)) end - local identifiers = GetPlayerIdentifiers(playerId) + local identifiers = GetPlayerIdentifiers(targetId) local timestamp, banTime = formatTime(time) local player = {} - player['name'] = playerName - for _, v in pairs(identifiers) do - player[split(v, ':')[1]] = v + player.name = targetName + for k, v in pairs(identifiers) do + player[MSK.Split(v, ':')[1]] = v end local bannedby = 'System' - if source then bannedby = GetPlayerName(source) end + if playerId then bannedby = GetPlayerName(playerId) end MySQL.query('INSERT INTO msk_bansystem (ids, time, reason, bannedby) VALUES (@ids, @time, @reason, @bannedby)', { ['@ids'] = json.encode(player), @@ -145,99 +144,96 @@ MSK.BanPlayer = function(source, playerId, time, reason) ['@bannedby'] = bannedby }, function(response) if response then - logging('debug', 'Player with ID ^2' .. playerId .. '^0 was banned until ^2' .. banTime .. '^0 for Reason: ^2' .. reason .. '^0. BanID: ^2' .. response.insertId .. '^0') - if source then Config.Notification(source, 'Player with ID ' .. playerId .. ' was banned until ' .. banTime .. ' for Reason: ' .. reason .. '. BanID: ' .. response.insertId) end - table.insert(bannedPlayers, {id = response.insertId, ids = player, reason = reason, time = banTime, from = bannedby}) - banLog(source, bannedby, playerId, banTime, reason, json.encode(player), response.insertId) - DropPlayer(playerId, ('Banned by %s for %s until %s. BanID: %s'):format(bannedby, reason, banTime, response.insertId)) + local banId = tonumber(response.insertId) + + logging('debug', ('Player with ID ^3%s^0 was banned until ^3%s^0 for Reason ^3%s^0. BanID: ^3%s^0'):format(targetId, banTime, reason, banId)) + if playerId then + MSK.Notification(playerId, 'MSK Bansystem', ('Player with ID ~y~%s~s~ was banned until ~y~%s~s~ for Reason ~y~%s~s~. BanID: ~y~%s~s~'):format(targetId, banTime, reason, banId)) + end + + bannedPlayers[#bannedPlayers + 1] = {id = banId, ids = player, reason = reason, time = banTime, from = bannedby} + banLog(playerId, bannedby, targetId, targetName, banTime, reason, json.encode(player), banId) + -- DropPlayer(targetId, ('Banned by %s for %s until %s. BanID: %s'):format(bannedby, reason, banTime, banId)) end end) end exports('BanPlayer', MSK.BanPlayer) exports('banPlayer', MSK.BanPlayer) -- Support for old Scripts -MSK.UnbanPlayer = function(source, banId) +MSK.UnbanPlayer = function(playerId, banId) + banId = tonumber(banId) + MySQL.query('DELETE FROM msk_bansystem WHERE id = @id', { ['@id'] = banId }, function(response) if response.affectedRows > 0 then - logging('debug', 'Player with BanID ^2' .. banId .. '^0 was unbanned.') - if source then Config.Notification(source, 'Player with BanID ' .. banId .. ' was unbanned.') end + logging('debug', ('Player with BanID ^3%s^0 was unbanned.'):format(banId)) + if playerId then MSK.Notification(playerId, 'MSK Bansystem', ('Player with BanID ~y~%s~s~ was unbanned.'):format(banId)) end - local index - for k, v in pairs(bannedPlayers) do - if v.id == banID then - index = k + for i = 1, #bannedPlayers do + if bannedPlayers[i].id == banId then + bannedPlayers[i] = nil + break end end - table.remove(bannedPlayers, index) local unbannedby = 'System' - if source then unbannedby = GetPlayerName(source) end - unbanLog(source, unbannedby, banId) + if playerId then unbannedby = GetPlayerName(playerId) end + unbanLog(playerId, unbannedby, banId) else - logging('debug', 'BanID ^2' .. banId .. '^0 was not found.') - if source then Config.Notification(source, 'BanID ' .. banId .. ' was not found.') end + logging('debug', ('BanId ^3%s^0 not found'):format(banId)) + if playerId then MSK.Notification(playerId, 'MSK Bansystem', ('BanId ~y~%s~s~ not found'):format(banId)) end end end) end exports('UnbanPlayer', MSK.UnbanPlayer) exports('unbanPlayer', MSK.UnbanPlayer) -- Support for old Scripts -AddEventHandler('playerConnecting', function(playerName, setKickReason, deferrals) - local src = source - if not Config.BanSystem.enable then return end - local isBanned, expired = isPlayerBanned(src, playerName) - - if isBanned and not expired then - CancelEvent() -- FiveM Native Function for cancelling the currently executing event - setKickReason(('Banned by %s until %s for %s. BanID: %s'):format(isBanned.from, isBanned.time, isBanned.reason, isBanned.id)) - elseif isBanned and expired then - MSK.UnbanPlayer(nil, isBanned.id) - end -end) - if Config.BanSystem.enable and Config.BanSystem.commands.enable then - for k, v in pairs(Config.BanSystem.commands.groups) do - ExecuteCommand(('add_ace group.%s command.%s allow'):format(v, Config.BanSystem.commands.ban)) - ExecuteCommand(('add_ace group.%s command.%s allow'):format(v, Config.BanSystem.commands.unban)) + for i = 1, #Config.BanSystem.commands.groups do + local group = Config.BanSystem.commands.groups[i] + ExecuteCommand(('add_ace group.%s command.%s allow'):format(group, Config.BanSystem.commands.ban)) + ExecuteCommand(('add_ace group.%s command.%s allow'):format(group, Config.BanSystem.commands.unban)) end - local isAllowed = function(source) - for k, group in pairs(Config.BanSystem.commands.groups) do - if IsPlayerAceAllowed(source, group) then - return true - end - end - return false + local IsPlayerAllowed = function(playerId, command) + return IsPlayerAceAllowed(playerId, ('command.%s'):format(command)) end RegisterCommand(Config.BanSystem.commands.ban, function(source, args, raw) - local src = source - local playerId, time, reason = args[1], args[2], args[3] + local playerId = source + local targetId, time, reason = args[1], args[2], args[3] - if not playerId or not time then return end + if not targetId or not time then return end if not reason then reason = 'Unknown' end - if src == 0 then return MSK.BanPlayer(nil, playerId, time, reason) end - if isAllowed(src) then - MSK.BanPlayer(src, playerId, time, reason) + if playerId == 0 then + MSK.BanPlayer(nil, targetId, time, reason) + return + end + + if IsPlayerAllowed(playerId, Config.BanSystem.commands.ban) then + MSK.BanPlayer(playerId, targetId, time, reason) else - Config.Notification(src, 'You don\'t have permission to do that!') + MSK.Notification(playerId, 'MSK Bansystem', 'You don\'t have permission to do that!') end end) RegisterCommand(Config.BanSystem.commands.unban, function(source, args, raw) - local src = source + local playerId = source local banId = args[1] if not banId then return end - if src == 0 then return MSK.UnbanPlayer(nil, banId) end - if isAllowed(src) then - MSK.UnbanPlayer(src, banId) + if playerId == 0 then + MSK.UnbanPlayer(nil, banId) + return + end + + if IsPlayerAllowed(playerId, Config.BanSystem.commands.unban) then + MSK.UnbanPlayer(playerId, banId) else - Config.Notification(src, 'You don\'t have permission to do that!') + MSK.Notification(playerId, 'MSK Bansystem', 'You don\'t have permission to do that!') end end) end \ No newline at end of file diff --git a/server/versionchecker.lua b/server/versionchecker.lua index 2179e5b..9b74fb2 100644 --- a/server/versionchecker.lua +++ b/server/versionchecker.lua @@ -8,7 +8,7 @@ local GITHUB_URL = "https://raw.githubusercontent.com/%s/%s/main/%s" local DOWNLOAD_URL = "https://github.com/MSK-Scripts/msk_core/releases/tag/v%s" local RENAME_WARNING = NAME_COLORED .. "^3 [WARNING] This resource should not be renamed! This can lead to errors. Please rename it to^0 %s" -local CHECK_FAILED = NAME_COLORED .. "^2 [ERROR] Version Check failed! Http Error: %s^0\n^3Please update to the latest version.^0" +local CHECK_FAILED = NAME_COLORED .. "^1 [ERROR] Version Check failed! Http Error: %s^0\n^3Please update to the latest version.^0" local BETA_VERSION = NAME_COLORED .. "^3 [WARNING] Beta version detected^0 - ^5Current Version:^0 %s - ^5Latest Version:^0 %s" local UP_TO_DATE = NAME_COLORED .. "^2 ✓ Resource is Up to Date^0 - ^5Current Version:^2 %s ^0" local NEW_VERSION = NAME_COLORED .. "^3 [Update Available] ^5Current Version:^0 %s - ^5Latest Version:^0 %s\n^5Download:^4 %s ^0"