RegisterServerEvent('rup-deathlog:OnPlayerKilled')
AddEventHandler('rup-deathlog:OnPlayerKilled', function(Message, Weapon, Street, Position, Killer)
print("Log received on server:", Message, Weapon, Street, Position, Killer)
local webhookUrl = Config.Discord.Settings.Webhook
local victimName = GetPlayerName(source) -- Nombre de la víctima
local killerName = GetPedKiller(PlayerPedId()) or "Unknown" -- Nombre del asesino
local victimIds = GetPlayerIdentifiers(source)
local killerIds = Killer and GetPlayerIdentifiers(Killer) or {}
print("Killer:", Killer)
print("Killer Name:", killerName)
print("Killer Identifiers:", killerIds)
local victimId = nil
local killerId = nil
-- Obtener el ID de Discord de la víctima
for _, id in ipairs(victimIds) do
if string.match(id, "^discord:") then
victimId = string.gsub(id, "discord:", "")
break
end
end
-- Obtener el ID de Discord del asesino (si existe)
if Killer then
for _, id in ipairs(killerIds) do
if string.match(id, "^discord:") then
killerId = string.gsub(id, "discord:", "")
break
end
end
end
if Config.Debug then
print("^5DEBUG^7: ^4Victim Name:^7", victimName)
print("^5DEBUG^7: ^4Victim Discord ID:^7", victimId)
print("^5DEBUG^7: ^4Killer Name:^7", killerName)
print("^5DEBUG^7: ^4Killer Discord ID:^7", killerId)
print("^5DEBUG^7: ^8Message:^7", Message)
print("^5DEBUG^7: ^2Weapon:^7", Weapon)
print("^5DEBUG^7: ^3Street:^7", Street)
print("^5DEBUG^7: ^3Position:^7", Position)
print("^5DEBUG^7: ^3Killer:^7", Killer)
end
-- Formatear la posición en string
local formattedPos = string.format("vector3(%.2f, %.2f, %.2f)", Position.x, Position.y, Position.z)
-- Crear el embed para Discord
local embed = {
{
["color"] = 16711680,
["title"] = "Player Death",
["fields"] = {
{["name"] = "Victim", ["value"] = victimName, ["inline"] = true},
{["name"] = "Killer", ["value"] = killerName, ["inline"] = true},
{["name"] = "Description", ["value"] = Message, ["inline"] = false},
{["name"] = "Weapon", ["value"] = (Weapon or "Unknown"), ["inline"] = true},
{["name"] = "Street", ["value"] = Street, ["inline"] = true},
{["name"] = "Death Coordinates", ["value"] = formattedPos, ["inline"] = true},
},
["timestamp"] = os.date('!%Y-%m-%dT%H:%M:%SZ'),
["footer"] = {
["text"] = "Player Death Log",
["icon_url"] = Config.Discord.Settings.Images,
},
}
}
-- Añadir los IDs de Discord si están disponibles
if victimId then
table.insert(embed[1]["fields"], {["name"] = "Victim's Discord", ["value"] = '<@' .. victimId .. '>', ["inline"] = true})
end
if killerId then
table.insert(embed[1]["fields"], {["name"] = "Killer's Discord", ["value"] = '<@' .. killerId .. '>', ["inline"] = true})
end
-- Convertir a JSON
local jsonData = json.encode({embeds = embed})
-- Realizar la solicitud HTTP
PerformHttpRequest(webhookUrl, function(err, text, headers)
if err == 0 then
print("^5DEBUG^7: ^7Webhook URL missing or incorrect in ^8Config.lua^7! Error Code:", err)
elseif err ~= 200 and err ~= 204 then
print("^5DEBUG^7: ^4Error sending death log to Discord:^7 Error Code:", err, "Response Text:", text)
else
print("^5DEBUG^7: ^2Success:^7 Death log sent to Discord.")
end
end, 'POST', jsonData, {['Content-Type'] = 'application/json'})
end)