ngd-jobduty/server/server.lua

81 lines
3.5 KiB
Lua
Raw Normal View History

2023-10-29 13:12:53 -05:00
-- ██ ██ ███████ ██████ ██ ██ ██████ ██████ ██ ██
-- ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
-- ██ █ ██ █████ ██████ ███████ ██ ██ ██ ██ █████
-- ██ ███ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
-- ███ ███ ███████ ██████ ██ ██ ██████ ██████ ██ ██
2023-10-28 22:19:53 -05:00
Config.Webhook = 'CHANGEME'
2023-10-29 13:12:53 -05:00
---------------------------------------------------------------------------------------------------------------------------------------------
2023-10-28 21:27:53 -05:00
local QBCore = exports['qb-core']:GetCoreObject()
local onDutyTimes = {}
AddEventHandler('playerDropped', function(reason)
local src = source
if onDutyTimes[src] then
local xPlayer = QBCore.Functions.GetPlayer(src)
local playerName = xPlayer.PlayerData.charinfo.firstname .. " " .. xPlayer.PlayerData.charinfo.lastname
sendDutyTimeWebhook(src, playerName)
onDutyTimes[src] = nil
end
end)
function sendDutyTimeWebhook(src, playerName)
local endTime = os.time()
local timeOnDuty = os.difftime(endTime, onDutyTimes[src])
local hours = math.floor(timeOnDuty / 3600)
local minutes = math.floor((timeOnDuty % 3600) / 60)
local seconds = timeOnDuty % 60
sendToDiscord(playerName ..
2023-10-29 13:12:53 -05:00
" went off duty. Total time on duty: " .. hours .. "H " .. minutes .. "M " .. seconds .. "S")
2023-10-28 21:27:53 -05:00
end
2023-10-28 22:19:53 -05:00
RegisterNetEvent('QBCore:ToggleDuty')
AddEventHandler('QBCore:ToggleDuty', function()
local src = source
local xPlayer = QBCore.Functions.GetPlayer(src)
local playerName = xPlayer.PlayerData.charinfo.firstname .. " " .. xPlayer.PlayerData.charinfo.lastname
local metadata = xPlayer.PlayerData.metadata
if metadata and metadata.callsign then
playerName = playerName .. " (Callsign: " .. metadata.callsign .. ")"
end
2023-10-29 13:12:53 -05:00
if xPlayer.PlayerData.job and xPlayer.PlayerData.job.name == Config.PoliceJob or xPlayer.PlayerData.job and xPlayer.PlayerData.job.type == Config.PoliceJobType then
2023-10-28 22:19:53 -05:00
if onDutyTimes[src] then
sendDutyTimeWebhook(src, playerName)
onDutyTimes[src] = nil
else
onDutyTimes[src] = os.time()
sendToDiscord(playerName .. " went on duty.")
end
end
end)
2023-10-29 13:12:53 -05:00
2023-10-28 21:27:53 -05:00
function sendToDiscord(message)
local webhook = Config.Webhook
if webhook == '' or webhook == 'CHANGEME' then
print('Please put webhook into editableserver.lua')
return
end
2023-10-29 13:12:53 -05:00
local currentDateTime = os.date("%m-%d-%Y %H:%M:%S")
2023-10-28 21:27:53 -05:00
local connect = {
{
["color"] = 255,
2023-10-28 22:19:53 -05:00
["title"] = "Police Duty Log",
2023-10-28 21:27:53 -05:00
["description"] = message,
["footer"] = {
["icon_url"] = "https://media.discordapp.net/attachments/1077462714902917171/1077462755625418862/96Logo.png",
2023-10-29 13:12:53 -05:00
["text"] = "www.nemesisGD.com | " .. currentDateTime,
2023-10-28 21:27:53 -05:00
},
}
}
PerformHttpRequest(webhook, function(err, text, headers) end, 'POST',
json.encode({
username = 'Nemesis Gaming Development | Police Duty',
embeds = connect,
avatar_url = 'https://media.discordapp.net/attachments/1077462714902917171/1077462755625418862/96Logo.png'
}),
{ ['Content-Type'] = 'application/json' })
end