Skip to content

Commit

Permalink
Fivemanage logging support
Browse files Browse the repository at this point in the history
  • Loading branch information
GhzGarage committed Apr 19, 2024
1 parent aec0f59 commit e97c159
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 28 deletions.
1 change: 1 addition & 0 deletions config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Config.UseTarget = GetConvar('UseTarget', 'false') == 'true' -- Use qb-target in
Config.PauseMapText = '' -- Text shown above the map when ESC is pressed. If left empty 'FiveM' will appear
Config.HarnessUses = 20
Config.DamageNeeded = 100.0 -- amount of damage till you can push your vehicle. 0-1000
Config.Logging = 'discord' -- fivemanage

Config.AFK = {
ignoredGroups = {
Expand Down
87 changes: 59 additions & 28 deletions server/logs.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local QBCore = exports['qb-core']:GetCoreObject()

local Webhooks = {
['default'] = '',
['testwebhook'] = '',
Expand Down Expand Up @@ -58,35 +59,65 @@ local logQueue = {}
RegisterNetEvent('qb-log:server:CreateLog', function(name, title, color, message, tagEveryone)
local postData = {}
local tag = tagEveryone or false
if not Webhooks[name] then print('Tried to call a log that isn\'t configured with the name of ' ..name) return end
local webHook = Webhooks[name] ~= '' and Webhooks[name] or Webhooks['default']
local embedData = {
{
['title'] = title,
['color'] = colors[color] or colors['default'],
['footer'] = {
['text'] = os.date('%c'),
},
['description'] = message,
['author'] = {
['name'] = 'QBCore Logs',
['icon_url'] = 'https://raw.githubusercontent.com/GhzGarage/qb-media-kit/main/Display%20Pictures/Logo%20-%20Display%20Picture%20-%20Stylized%20-%20Red.png',
},

if Config.Logging == 'discord' then
if not Webhooks[name] then
print('Tried to call a log that isn\'t configured with the name of ' .. name)
return
end
local webHook = Webhooks[name] ~= '' and Webhooks[name] or Webhooks['default']
local embedData = {
{
['title'] = title,
['color'] = colors[color] or colors['default'],
['footer'] = {
['text'] = os.date('%c'),
},
['description'] = message,
['author'] = {
['name'] = 'QBCore Logs',
['icon_url'] = 'https://raw.githubusercontent.com/GhzGarage/qb-media-kit/main/Display%20Pictures/Logo%20-%20Display%20Picture%20-%20Stylized%20-%20Red.png',
},
}
}
}

if not logQueue[name] then logQueue[name] = {} end
logQueue[name][#logQueue[name] + 1] = {webhook = webHook, data = embedData}
if not logQueue[name] then logQueue[name] = {} end
logQueue[name][#logQueue[name] + 1] = { webhook = webHook, data = embedData }

if #logQueue[name] >= 10 then
if tag then
postData = {username = 'QB Logs', content = '@everyone', embeds = {}}
else
postData = {username = 'QB Logs', embeds = {}}
if #logQueue[name] >= 10 then
if tag then
postData = { username = 'QB Logs', content = '@everyone', embeds = {} }
else
postData = { username = 'QB Logs', embeds = {} }
end
for i = 1, #logQueue[name] do postData.embeds[#postData.embeds + 1] = logQueue[name][i].data[1] end
PerformHttpRequest(logQueue[name][1].webhook, function() end, 'POST', json.encode(postData), { ['Content-Type'] = 'application/json' })
logQueue[name] = {}
end
elseif Config.Logging == 'fivemanage' then
local FiveManageAPIKey = GetConvar('FIVEMANAGE_LOGS_API_KEY', 'false')
if FiveManageAPIKey == 'false' then
print('You need to set the FiveManage API key in your server.cfg')
return
end
for i = 1, #logQueue[name] do postData.embeds[#postData.embeds + 1] = logQueue[name][i].data[1] end
PerformHttpRequest(logQueue[name][1].webhook, function() end, 'POST', json.encode(postData), { ['Content-Type'] = 'application/json' })
logQueue[name] = {}
local extraData = {
level = tagEveryone and 'warn' or 'info', -- info, warn, error or debug
message = title, -- any string
metadata = { -- a table or object with any properties you want
description = message,
playerId = source,
playerLicense = GetPlayerIdentifierByType(source, 'license'),
playerDiscord = GetPlayerIdentifierByType(source, 'discord')
},
resource = GetInvokingResource(),
}
PerformHttpRequest('https://api.fivemanage.com/api/logs', function(statusCode, response, headers)
-- Uncomment the following line to enable debugging
-- print(statusCode, response, json.encode(headers))
end, 'POST', json.encode(extraData), {
['Authorization'] = FiveManageAPIKey,
['Content-Type'] = 'application/json',
})
end
end)

Expand All @@ -99,11 +130,11 @@ Citizen.CreateThread(function()
timer = 0
for name, queue in pairs(logQueue) do
if #queue > 0 then
local postData = {username = 'QB Logs', embeds = {}}
local postData = { username = 'QB Logs', embeds = {} }
for i = 1, #queue do
postData.embeds[#postData.embeds + 1] = queue[i].data[1]
end
PerformHttpRequest(queue[1].webhook, function() end, 'POST', json.encode(postData), {['Content-Type'] = 'application/json'})
PerformHttpRequest(queue[1].webhook, function() end, 'POST', json.encode(postData), { ['Content-Type'] = 'application/json' })
logQueue[name] = {}
end
end
Expand All @@ -113,4 +144,4 @@ end)

QBCore.Commands.Add('testwebhook', 'Test Your Discord Webhook For Logs (God Only)', {}, false, function()
TriggerEvent('qb-log:server:CreateLog', 'testwebhook', 'Test Webhook', 'default', 'Webhook setup successfully')
end, 'god')
end, 'god')

0 comments on commit e97c159

Please sign in to comment.