ngd-jobduty/server/server.lua

147 lines
6.3 KiB
Lua
Raw Normal View History

2024-01-15 22:22:31 -06:00
local QBCore = exports['qb-core']:GetCoreObject()
local onDutyTimes = {}
2023-10-29 13:12:53 -05:00
-- ██ ██ ███████ ██████ ██ ██ ██████ ██████ ██ ██
-- ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
-- ██ █ ██ █████ ██████ ███████ ██ ██ ██ ██ █████
-- ██ ███ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
-- ███ ███ ███████ ██████ ██ ██ ██████ ██████ ██ ██
2024-01-15 22:22:31 -06:00
local JobWebhooks = {
police = 'CHANGEME',
ambulance = 'CHANGEME',
tobacco = 'CHANGEME',
--Add more to match config.
}
2023-10-29 13:12:53 -05:00
2024-01-15 22:22:31 -06:00
function sendDutyTimeWebhook(src, playerName, jobName, jobLabel, playerDropped)
2023-10-28 21:27:53 -05:00
local endTime = os.time()
local timeOnDuty = os.difftime(endTime, onDutyTimes[src])
2024-01-15 22:22:31 -06:00
local xPlayer = QBCore.Functions.GetPlayer(src)
local metadata = xPlayer.PlayerData.metadata
local callsign = ""
if Config.Jobs[jobName] and Config.Jobs[jobName].SendCallsign and metadata and metadata.callsign then
callsign = metadata.callsign
end
local dutyStatus = "Off Duty"
if playerDropped then
dutyStatus = "Off Duty - Player Dropped"
end
sendToDiscord(jobName, playerName, callsign, timeOnDuty, nil, dutyStatus, 16711680, jobLabel)
2023-10-28 21:27:53 -05:00
end
2023-10-28 22:19:53 -05:00
RegisterNetEvent('QBCore:ToggleDuty')
AddEventHandler('QBCore:ToggleDuty', function()
2024-01-15 22:22:31 -06:00
local src = source
local xPlayer = QBCore.Functions.GetPlayer(src)
local jobName = xPlayer.PlayerData.job.name
local jobLabel = QBCore.Shared.Jobs[jobName] and QBCore.Shared.Jobs[jobName].label or jobName
local jobConfig = Config.Jobs[jobName]
if jobConfig and JobWebhooks[jobName] then
local playerName = xPlayer.PlayerData.charinfo.firstname .. " " .. xPlayer.PlayerData.charinfo.lastname
local metadata = xPlayer.PlayerData.metadata
local callsign = ""
if jobConfig.SendCallsign and metadata and metadata.callsign then
callsign = metadata.callsign
end
if onDutyTimes[src] then --Off duty
sendDutyTimeWebhook(src, playerName, jobName, jobLabel)
onDutyTimes[src] = nil
else
onDutyTimes[src] = os.time() -- On duty
sendToDiscord(jobName, playerName, callsign, nil, nil, "On Duty", 65280, jobLabel)
end
end
end)
RegisterNetEvent('SendOnDutyWebhook')
AddEventHandler('SendOnDutyWebhook', function(jobName)
2023-10-28 22:19:53 -05:00
local src = source
local xPlayer = QBCore.Functions.GetPlayer(src)
local playerName = xPlayer.PlayerData.charinfo.firstname .. " " .. xPlayer.PlayerData.charinfo.lastname
2024-01-15 22:22:31 -06:00
local jobLabel = QBCore.Shared.Jobs[jobName] and QBCore.Shared.Jobs[jobName].label or jobName
2023-10-28 22:19:53 -05:00
local metadata = xPlayer.PlayerData.metadata
2024-01-15 22:22:31 -06:00
local callsign = ""
if Config.Jobs[jobName].SendCallsign and metadata and metadata.callsign then
callsign = metadata.callsign
2023-10-28 22:19:53 -05:00
end
2024-01-15 22:22:31 -06:00
if JobWebhooks[jobName] then
local customMessage = "**Player Name:**\n`" .. playerName .. "`\n"
customMessage = customMessage .. "**Job:**\n`" .. jobLabel .. "`\n"
if callsign and callsign ~= "" then
customMessage = customMessage .. "**Callsign:**\n`" .. callsign .. "`\n"
2023-10-28 22:19:53 -05:00
end
2024-01-15 22:22:31 -06:00
customMessage = customMessage .. "\n" .. playerName .. " has loaded into the server and was put on duty."
sendToDiscord(jobName, playerName, callsign, nil, customMessage, "Player Logged In", 65280)
2023-10-28 22:19:53 -05:00
end
end)
2024-01-15 22:22:31 -06:00
RegisterNetEvent('SetInitialDutyStatus')
AddEventHandler('SetInitialDutyStatus', function(isOnDuty)
local src = source
if isOnDuty then
onDutyTimes[src] = os.time()
else
onDutyTimes[src] = nil
end
end)
2023-10-29 13:12:53 -05:00
2024-01-15 22:22:31 -06:00
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
local jobName = xPlayer.PlayerData.job.name
local jobLabel = QBCore.Shared.Jobs[jobName] and QBCore.Shared.Jobs[jobName].label or jobName
sendDutyTimeWebhook(src, playerName, jobName, jobLabel, true)
onDutyTimes[src] = nil
end
end)
function sendToDiscord(jobName, playerName, callsign, timeOnDuty, customMessage, dutyStatus, color, jobLabel)
local webhook = JobWebhooks[jobName]
if not webhook or webhook == 'CHANGEME' then
print('Webhook URL is not set for job: ' .. jobName)
2023-10-28 21:27:53 -05:00
return
end
2024-01-15 22:22:31 -06:00
local title = "Duty Status Update"
if dutyStatus then
title = title .. " (" .. dutyStatus .. ")"
end
local message
if customMessage then
message = customMessage
else
message = "**Player Name:**\n`" .. playerName .. "`\n"
message = message .. "**Job:**\n`" .. (jobLabel or jobName) .. "`\n"
if callsign and callsign ~= "" then
message = message .. "**Callsign:**\n`" .. callsign .. "`\n"
end
if timeOnDuty then
local hours = math.floor(timeOnDuty / 3600)
local minutes = math.floor((timeOnDuty % 3600) / 60)
local seconds = timeOnDuty % 60
message = message .. "**Time on Duty:**\n`" .. hours .. "H " .. minutes .. "M " .. seconds .. "S`"
end
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 = {
{
2024-01-15 22:23:27 -06:00
["color"] = color or 255,
2024-01-15 22:22:31 -06:00
["title"] = title,
2023-10-28 21:27:53 -05:00
["description"] = message,
["footer"] = {
2024-01-15 22:22:31 -06:00
["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({
2024-01-15 22:22:31 -06:00
username = 'Nemesis Gaming Development | Duty Status',
2023-10-28 21:27:53 -05:00
embeds = connect,
avatar_url = 'https://media.discordapp.net/attachments/1077462714902917171/1077462755625418862/96Logo.png'
}),
{ ['Content-Type'] = 'application/json' })
end