Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Incident Webhook Functionality #378

Merged
merged 3 commits into from
Oct 12, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 79 additions & 11 deletions server/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ local MugShotWebhook = ''
-- Clock-in notifications for duty. Add a Discord webhook.
-- Command /mdtleaderboard, will display top players per clock-in hours.
local ClockinWebhook = ''

-- Incident and Incident editting. Add a Discord webhook.
-- Incident Author, Title, and Report will display in webhook post.
local IncidentWebhook = ''
--------------------------------

QBCore.Functions.CreateCallback('ps-mdt:server:MugShotWebhook', function(source, cb)
Expand Down Expand Up @@ -1306,6 +1310,27 @@ RegisterNetEvent('mdt:server:saveIncident', function(id, title, information, tag
jobtype = 'police',
}, function(infoResult)
if infoResult then
MySQL.Async.fetchAll('SELECT `author`, `title`, `details` FROM `mdt_incidents` WHERE `id` = @id', { ['@id'] = infoResult }, function(result)
-- Check if the query returned any result
if result and #result > 0 then
-- Fetch the author, title, and details from the result
local author = result[1].author
local title = result[1].title
local details = result[1].details

details = details:gsub("<[^>]+>", ""):gsub("&nbsp;", "")

-- Construct the webhook message
local message = "Author: " .. author .. "\n"
message = message .. "Title: " .. title .. "\n"
message = message .. "Details: " .. details

-- Send the webhook using the sendToDiscord function
sendIncidentToDiscord(3989503, "MDT Incident Report", message, "ps-mdt | Made by Project Sloth")
else
print('No incident found in the mdt_incidents table with id: ' .. infoResult)
end
end)
for i=1, #associated do
MySQL.insert('INSERT INTO `mdt_convictions` (`cid`, `linkedincident`, `warrant`, `guilty`, `processed`, `associated`, `charges`, `fine`, `sentence`, `recfine`, `recsentence`, `time`) VALUES (:cid, :linkedincident, :warrant, :guilty, :processed, :associated, :charges, :fine, :sentence, :recfine, :recsentence, :time)', {
cid = associated[i]['Cid'],
Expand All @@ -1328,17 +1353,41 @@ RegisterNetEvent('mdt:server:saveIncident', function(id, title, information, tag
end)
elseif id > 0 then
MySQL.update("UPDATE mdt_incidents SET title=:title, details=:details, civsinvolved=:civsinvolved, tags=:tags, officersinvolved=:officersinvolved, evidence=:evidence WHERE id=:id", {
title = title,
details = information,
tags = json.encode(tags),
officersinvolved = json.encode(officers),
civsinvolved = json.encode(civilians),
evidence = json.encode(evidence),
id = id
})
for i=1, #associated do
TriggerEvent('mdt:server:handleExistingConvictions', associated[i], id, time)
end
title = title,
details = information,
tags = json.encode(tags),
officersinvolved = json.encode(officers),
civsinvolved = json.encode(civilians),
evidence = json.encode(evidence),
id = id
}, function(rowsChanged)
if rowsChanged > 0 then
MySQL.Async.fetchAll('SELECT `author`, `title`, `details` FROM `mdt_incidents` WHERE `id` = @id', { ['@id'] = id }, function(result)
-- Check if the query returned any result
if result and #result > 0 then
-- Fetch the author, title, and details from the result
local author = result[1].author
local title = result[1].title
local details = result[1].details

details = details:gsub("<[^>]+>", ""):gsub("&nbsp;", "")

-- Construct the webhook message
local message = "Author: " .. author .. "\n"
message = message .. "Title: " .. title .. "\n"
message = message .. "Details: " .. details

-- Send the webhook using the sendToDiscord function
sendIncidentToDiscord(16711680, "MDT Incident Report has been Updated", message, "ps-mdt | Made by Project Sloth")
else
print('No incident found in the mdt_incidents table with id: ' .. id)
end
end)
for i=1, #associated do
TriggerEvent('mdt:server:handleExistingConvictions', associated[i], id, time)
end
end
end)
end
end
end
Expand Down Expand Up @@ -1888,6 +1937,25 @@ function sendToDiscord(color, name, message, footer)
end
end

function sendIncidentToDiscord(color, name, message, footer)
if ClockinWebhook == '' then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IncidentWebhook

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My mistake will fix this right now

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait - this is just the clock in webhook for the time clock - this should be correct.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait - this is just the clock in webhook for the time clock - this should be correct.

No, you're using the function sendIncidentToDiscord and sending it to ClockinWebhook, it needs to be sent to IncidentWebhook

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MonkeyWhisper - I have corrected this issue. My fault, for whatever reason I was having a brain fart.

print("\27[31mA webhook is missing in: IncidentWebhook (server > main.lua > line 24)\27[0m")
else
local embed = {
{
color = color,
title = "**".. name .."**",
description = message,
footer = {
text = footer,
},
}
}

PerformHttpRequest(IncidentWebhook, function(err, text, headers) end, 'POST', json.encode({username = name, embeds = embed}), { ['Content-Type'] = 'application/json' })
end
end

function format_time(time)
local days = math.floor(time / 86400)
time = time % 86400
Expand Down