mirror of
https://github.com/CommunityOx/ox_lib.git
synced 2026-08-17 06:56:03 +01:00
feat(commands): chat suggestions
This commit is contained in:
@@ -1,48 +1,107 @@
|
|||||||
|
local commands = {}
|
||||||
|
|
||||||
|
SetTimeout(1000, function()
|
||||||
|
TriggerClientEvent('chat:addSuggestions', -1, commands)
|
||||||
|
end)
|
||||||
|
|
||||||
|
AddEventHandler('playerJoining', function(source)
|
||||||
|
TriggerClientEvent('chat:addSuggestions', source, commands)
|
||||||
|
end)
|
||||||
|
|
||||||
|
local function chatSuggestion(name, parameters, help)
|
||||||
|
local params = {}
|
||||||
|
|
||||||
|
if parameters then
|
||||||
|
for i = 1, #parameters do
|
||||||
|
local arg, argType = string.strsplit(':', parameters[i])
|
||||||
|
|
||||||
|
if argType and argType:sub(0, 1) == '?' then
|
||||||
|
argType = argType:sub(2, #argType)
|
||||||
|
end
|
||||||
|
|
||||||
|
params[i] = {
|
||||||
|
name = arg,
|
||||||
|
help = argType
|
||||||
|
}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
commands[#commands + 1] = {
|
||||||
|
name = '/'..name,
|
||||||
|
help = help,
|
||||||
|
params = params
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
---@param group string
|
---@param group string
|
||||||
---@param name string
|
---@param name string
|
||||||
---@param callback function
|
---@param callback function
|
||||||
---@param arguments string
|
---@param parameters table
|
||||||
local function AddCommand(group, name, callback, arguments)
|
local function addCommand(group, name, callback, parameters, help)
|
||||||
|
if not group then group = 'builtin.everyone' end
|
||||||
|
|
||||||
if type(name) == 'table' then
|
if type(name) == 'table' then
|
||||||
for i=1, #name do
|
for i = 1, #name do
|
||||||
AddCommand(group, name[i], callback, arguments)
|
addCommand(group, name[i], callback, parameters, help)
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
if not group then group = 'builtin.everyone' end
|
chatSuggestion(name, parameters, help)
|
||||||
|
|
||||||
RegisterCommand(name, function(source, args)
|
RegisterCommand(name, function(source, args)
|
||||||
if arguments then
|
source = tonumber(source)
|
||||||
for i=1, #arguments do
|
|
||||||
local arg, argType = string.strsplit(':', arguments[i])
|
if parameters then
|
||||||
|
for i = 1, #parameters do
|
||||||
|
local arg, argType = string.strsplit(':', parameters[i])
|
||||||
local value = args[i]
|
local value = args[i]
|
||||||
|
|
||||||
if arg == 'target' and value == 'me' then value = source end
|
if arg == 'target' and value == 'me' then value = source end
|
||||||
|
|
||||||
if argType then
|
if argType then
|
||||||
if argType == 'number' then value = tonumber(value) end
|
local optional
|
||||||
assert(type(value) == argType or argType:find('?'), ('%s expected <%s> for argument %s (%s), received %s'):format(name, argType, i, arg, type(value)))
|
|
||||||
|
if argType:sub(0, 1) == '?' then
|
||||||
|
argType = argType:sub(2, #argType)
|
||||||
|
optional = true
|
||||||
|
end
|
||||||
|
|
||||||
|
if argType == 'number' then
|
||||||
|
value = tonumber(value) or value
|
||||||
|
end
|
||||||
|
|
||||||
|
local type = type(value)
|
||||||
|
|
||||||
|
if type ~= argType and (not optional or type ~= 'nil') then
|
||||||
|
local invalid = ('^1%s expected <%s> for argument %s (%s), received %s^0'):format(name, argType, i, arg, type)
|
||||||
|
if source < 1 then
|
||||||
|
return print(invalid)
|
||||||
|
else
|
||||||
|
return TriggerClientEvent('chat:addMessage', source, invalid)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
args[arg] = value
|
args[arg] = value
|
||||||
args[i] = nil
|
args[i] = nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
callback(tonumber(source), args)
|
|
||||||
|
callback(source, args)
|
||||||
end, group and true)
|
end, group and true)
|
||||||
|
|
||||||
name = ('command.%s'):format(name)
|
name = ('command.%s'):format(name)
|
||||||
if not IsPrincipalAceAllowed(group, name) then ExecuteCommand(('add_ace %s %s allow'):format(group, name)) end
|
if not IsPrincipalAceAllowed(group, name) then lib.addAce(group, name) end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return AddCommand
|
return addCommand
|
||||||
|
|
||||||
--[[ Example
|
--[[ Example
|
||||||
AddCommand('admin', {'additem', 'giveitem'}, function(source, args)
|
AddCommand('group.admin', {'additem', 'giveitem'}, function(source, args)
|
||||||
args.item = Items(args.item)
|
args.item = Items(args.item)
|
||||||
if args.item and args.count > 0 then
|
if args.item and args.count > 0 then
|
||||||
Inventory.AddItem(args.target, args.item.name, args.count, args.metatype)
|
Inventory.AddItem(args.target, args.item.name, args.count, args.metatype)
|
||||||
end
|
end
|
||||||
end, {'target:number', 'item:string', 'count:number', 'metatype:?string'})
|
end, {'target:number', 'item:string', 'count:number', 'metatype:?string'})
|
||||||
-- /additem 1 burger 1
|
-- /additem 1 burger 1
|
||||||
]]
|
]]
|
||||||
|
|||||||
Reference in New Issue
Block a user