From ae872278a3362f25ae26808bb82ac235e8eaec34 Mon Sep 17 00:00:00 2001 From: Kakarot <57848836+GhzGarage@users.noreply.github.com> Date: Tue, 11 Mar 2025 23:13:00 -0500 Subject: [PATCH] Basic commands support --- server/commands.lua | 85 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 server/commands.lua diff --git a/server/commands.lua b/server/commands.lua new file mode 100644 index 0000000..01d862f --- /dev/null +++ b/server/commands.lua @@ -0,0 +1,85 @@ +-- 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