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

Refactor AFK function + Support for Logged out + Fix Locale message #333

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
84 changes: 45 additions & 39 deletions client/afk.lua
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
local QBCore = exports['qb-core']:GetCoreObject()
local isLoggedIn = LocalPlayer.state.isLoggedIn
local checkUser = true
local prevPos, time = nil, nil
local timeMinutes = {
['900'] = 'minutes',
['600'] = 'minutes',
['300'] = 'minutes',
['150'] = 'minutes',
['60'] = 'minutes',
['30'] = 'seconds',
['20'] = 'seconds',
['10'] = 'seconds',
}
local checkInterval = 10
local notificationDuration = (checkInterval - 2) * 1000

local function updatePermissionLevel()
QBCore.Functions.TriggerCallback('qb-afkkick:server:GetPermissions', function(userGroups)
Expand All @@ -38,36 +29,51 @@ RegisterNetEvent('QBCore:Client:OnPermissionUpdate', function()
updatePermissionLevel()
end)

CreateThread(function()
while true do
Wait(10000)
local playerPed = PlayerPedId()
if isLoggedIn then
if checkUser then
function getTimeUnit(time)
return time >= 60 and 'minutes' or 'seconds'
end
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Needs a linebreak here below end


if Config.AFK.enabled then
CreateThread(function()
local time = nil
local prevPos = nil

while true do
Wait(checkInterval * 1000)
local playerPed = PlayerPedId()
if not time then
time = Config.AFK.secondsUntilKick
end

if not checkUser then
goto skip
end

if isLoggedIn then
local currentPos = GetEntityCoords(playerPed, true)
if prevPos then
if currentPos == prevPos then
if time then
if time > 0 then
local _type = timeMinutes[tostring(time)]
if _type == 'minutes' then
QBCore.Functions.Notify(Lang:t('afk.will_kick') .. math.ceil(time / 60) .. Lang:t('afk.time_minutes'), 'error', 10000)
elseif _type == 'seconds' then
QBCore.Functions.Notify(Lang:t('afk.will_kick') .. time .. Lang:t('afk.time_seconds'), 'error', 10000)
end
time -= 10
else
TriggerServerEvent('KickForAFK')
end
else
time = Config.AFK.secondsUntilKick
end
else
time = Config.AFK.secondsUntilKick
end
if currentPos ~= prevPos then
time = Config.AFK.secondsUntilKick
end
prevPos = currentPos
end

if time > 0 then
if time < Config.AFK.secondsUntilKick / 2 then
local timeString = math.ceil(time / 60) .. Lang:t('afk.time_minutes')

if getTimeUnit(time) == 'seconds' then
timeString = time .. Lang:t('afk.time_seconds')
end

QBCore.Functions.Notify(Lang:t('afk.will_kick') .. timeString, 'error', notificationDuration)
end

time -= checkInterval
else
TriggerServerEvent('KickForAFK')
end

::skip::
end
end
end)
end)
end
3 changes: 2 additions & 1 deletion config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ Config.UseTarget = GetConvar('UseTarget', 'false') == 'true' -- Use qb-target in
-- To make this simple. Everything you need is in the config, except for discord, weapon drops, vehicle classes for cruise, hands up disabled keys, and recoil

Config.AFK = {
enabled = true, -- Enable or disable AFK Kick
ignoredGroups = {
['mod'] = true,
['admin'] = true,
['god'] = true
},
secondsUntilKick = 1800 -- AFK Kick Time Limit (in seconds)
secondsUntilKick = 1800, -- AFK Kick Time Limit (in seconds)
}

Config.Binoculars = {
Expand Down
8 changes: 4 additions & 4 deletions locales/pt.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
local Translations = {
afk = {
will_kick = "Estás AFK e vais ser kickado em",
time_seconds = " segundos!",
time_minutes = " minutos!",
kick_message = "Foste Kickado Por Estares AFK",
["will_kick"] = "Estás AFK e vais ser kickado em ",
["time_minutes"] = " minutos",
["time_seconds"] = " segundos",
["kick_message"] = "Foste Kickado Por Estares AFK",
},
error = {
["car_wash_canceled"] = "Lavagem cancelada...",
Expand Down