Skip to content

Commit b2c81e7

Browse files
committed
Updated AFK
1 parent 826082f commit b2c81e7

File tree

2 files changed

+25
-40
lines changed

2 files changed

+25
-40
lines changed

client/afk.lua

Lines changed: 17 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,6 @@
11
local QBCore = exports['qb-core']:GetCoreObject()
22
local isLoggedIn = LocalPlayer.state.isLoggedIn
33
local checkUser = true
4-
local prevPos, time = nil, nil
5-
local timeMinutes = {
6-
['900'] = 'minutes',
7-
['600'] = 'minutes',
8-
['300'] = 'minutes',
9-
['150'] = 'minutes',
10-
['60'] = 'minutes',
11-
['30'] = 'seconds',
12-
['20'] = 'seconds',
13-
['10'] = 'seconds',
14-
}
154

165
local function updatePermissionLevel()
176
QBCore.Functions.TriggerCallback('qb-afkkick:server:GetPermissions', function(userGroups)
@@ -34,40 +23,30 @@ RegisterNetEvent('QBCore:Client:OnPlayerUnload', function()
3423
isLoggedIn = false
3524
end)
3625

37-
RegisterNetEvent('QBCore:Client:OnPermissionUpdate', function()
38-
updatePermissionLevel()
39-
end)
40-
4126
CreateThread(function()
27+
local sleepTimer = 10000
4228
while true do
43-
Wait(10000)
44-
local ped = PlayerPedId()
45-
if isLoggedIn == true or Config.AFK.kickInCharMenu == true then
46-
if checkUser then
47-
local currPos = GetEntityCoords(ped, true)
48-
if prevPos then
49-
if currPos == prevPos then
50-
if time then
51-
if time > 0 then
52-
local _type = timeMinutes[tostring(time)]
53-
if _type == 'minutes' then
54-
QBCore.Functions.Notify(Lang:t('afk.will_kick') .. math.ceil(time / 60) .. Lang:t('afk.time_minutes'), 'error', 10000)
55-
elseif _type == 'seconds' then
56-
QBCore.Functions.Notify(Lang:t('afk.will_kick') .. time .. Lang:t('afk.time_seconds'), 'error', 10000)
57-
end
58-
time -= 10
59-
else
60-
TriggerServerEvent('KickForAFK')
61-
end
29+
if (isLoggedIn or Config.AFK.kickInCharMenu) and checkUser then
30+
while GetTimeSinceLastInput(0) > sleepTimer do
31+
local kickTimer = Config.AFK.minutesUntilKick * 60 * 1000
32+
local timeLeftSec = (kickTimer - GetTimeSinceLastInput(0)) / 1000
33+
for _, warning in pairs(Config.AFK.warnings) do
34+
if not warning.sent and timeLeftSec < warning.timeSec then
35+
if warning.timeSec >= 60 then
36+
QBCore.Functions.Notify(Lang:t('afk.will_kick') .. math.ceil(warning.timeSec / 60) .. Lang:t('afk.time_minutes'), 'error', 15000)
6237
else
63-
time = Config.AFK.secondsUntilKick
38+
QBCore.Functions.Notify(Lang:t('afk.will_kick') .. warning.timeSec .. Lang:t('afk.time_seconds'), 'error', 15000)
6439
end
65-
else
66-
time = Config.AFK.secondsUntilKick
40+
warning.sent = true
41+
end
42+
if GetTimeSinceLastInput(0) > kickTimer then
43+
TriggerServerEvent('KickForAFK')
6744
end
6845
end
69-
prevPos = currPos
46+
Wait(1000)
7047
end
48+
for _, warning in pairs(Config.AFK.warnings) do warning.sent = false end
7149
end
50+
Wait(sleepTimer)
7251
end
7352
end)

config.lua

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,14 @@ Config.AFK = {
1111
['admin'] = true,
1212
['god'] = true
1313
},
14-
secondsUntilKick = 1000000, -- AFK Kick Time Limit (in seconds)
15-
kickInCharMenu = false -- Set to true if you want to kick players for being AFK even when they are in the character menu.
14+
warnings = {
15+
{timeSec = 300, sent = false}, -- Will send a warning at 300 seconds (5 minutes) remaining on afk timer
16+
{timeSec = 120, sent = false},
17+
{timeSec = 60, sent = false},
18+
{timeSec = 30, sent = false},
19+
},
20+
minutesUntilKick = 120, -- AFK Kick Time Limit (in minutes)
21+
kickInCharMenu = false -- Set to true if you want to kick players for being AFK even when they are in the character menu.
1622
}
1723

1824
Config.HandsUp = {

0 commit comments

Comments
 (0)