Files
qb-core/server/commands.lua
T
2025-03-11 23:13:00 -05:00

86 lines
2.7 KiB
Lua

-- Handle Chat Input
AddEventHandler('chatMessage', function(_, _, message)
if string.sub(message, 1, 1) == '/' then
CancelEvent()
return
end
end)
-- Add Aces
CreateThread(function()
local permissions = QBCore.Config.Server.Permissions
for i = 1, #permissions do
local permission = permissions[i]
ExecuteCommand(('add_ace qbcore.%s %s allow'):format(permission, permission))
end
end)
-- Register & Refresh Commands
function QBCore.Commands.Add(name, help, arguments, argsrequired, callback, permission, ...)
local restricted = true
if not permission then permission = 'user' end
if permission == 'user' then restricted = false end
RegisterCommand(name, function(source, args, rawCommand)
if argsrequired and #args < #arguments then
return TriggerClientEvent('chat:addMessage', source, {
color = { 255, 0, 0 },
multiline = true,
args = { 'System', Lang:t('error.missing_args2') }
})
end
callback(source, args, rawCommand)
end, restricted)
local extraPerms = ... and table.pack(...) or nil
if extraPerms then
extraPerms[extraPerms.n + 1] = permission
extraPerms.n += 1
permission = extraPerms
for i = 1, permission.n do
if not QBCore.Commands.IgnoreList[permission[i]] then
ExecuteCommand(('add_ace qbcore.%s command.%s allow'):format(permission[i], name))
end
end
permission.n = nil
else
permission = tostring(permission:lower())
if not QBCore.Commands.IgnoreList[permission] then
ExecuteCommand(('add_ace qbcore.%s command.%s allow'):format(permission, name))
end
end
QBCore.Commands.List[name:lower()] = {
name = name:lower(),
permission = permission,
help = help,
arguments = arguments,
argsrequired = argsrequired,
callback = callback
}
end
function QBCore.Commands.Refresh(source)
local src = source
local Player = QBCore.Functions.GetPlayer(src)
local suggestions = {}
if Player then
for command, info in pairs(QBCore.Commands.List) do
local hasPerm = IsPlayerAceAllowed(tostring(src), 'command.' .. command)
if hasPerm then
suggestions[#suggestions + 1] = {
name = '/' .. command,
help = info.help,
params = info.arguments
}
else
TriggerClientEvent('chat:removeSuggestion', src, '/' .. command)
end
end
TriggerClientEvent('chat:addSuggestions', src, suggestions)
end
end