Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function expect( value, name, expectedType )
- local actualType = type( value )
- if actualType != expectedType then
- error( string.format( 'TypeError: parameter %s has wrong type (%s expected, got %s)', name, expectedType, actualType ) )
- end
- end
- CommandManager = {
- prefixKeyword = 'aoki',
- prefixes = {
- '/',
- '!',
- '.',
- },
- commands = {},
- addCommand = function( params )
- expect( params.name, 'name', 'string' )
- expect( params.args, 'args', 'string' )
- expect( params.description, 'description', 'string' )
- expect( params.localPlayerOnly or false, 'localPlayerOnly', 'boolean' )
- expect( params.friendsOnly or false, 'friendsOnly', 'boolean' )
- expect( params.callback, 'callback', 'function' )
- CommandManager.commands[params.name:lower()] = {
- name = params.name,
- args = params.args,
- description = params.description,
- localPlayerOnly = params.localPlayerOnly,
- friendsOnly = params.friendsOnly,
- callback = params.callback,
- }
- end,
- subPrefix = function( text )
- local usedPrefix = nil
- for _, prefix in ipairs( CommandManager.prefixes ) do
- if string.StartsWith( text, prefix ) then
- usedPrefix = prefix
- break
- end
- end
- if usedPrefix then
- return string.TrimLeft( string.sub( text, #usedPrefix + 1 ) )
- end
- end,
- parseArgs = function( stringArgs )
- local args = string.Explode( '%s+', stringArgs, true )
- local cmd = table.remove( args, 1 ):lower()
- if cmd != CommandManager.prefixKeyword then
- return
- end
- cmd = table.remove( args, 1 ):lower()
- local i = 1
- while i <= #args do
- local quote = nil
- if string.StartsWith( args[i], [["]] ) then
- quote = [["]]
- elseif string.StartsWith( args[i], [[']] ) then
- quote = [[']]
- end
- if quote then
- local arg = {}
- local i2 = i
- while i2 <= #args do
- table.insert( arg, args[i2] )
- if string.EndsWith( args[i2], quote ) then
- for j = i, i2 do
- table.remove( args, i )
- end
- arg = table.concat( arg, ' ' )
- table.insert( args, i, string.sub( arg, 2, #arg - 1 ) )
- i = i2
- break
- end
- i2 = i2 + 1
- end
- end
- i = i + 1
- end
- return cmd, args
- end,
- onMessage = function( ply, message, _, _, localChat )
- message = CommandManager.subPrefix( message )
- if !message then return end
- local cmd, args = CommandManager.parseArgs( message )
- local command = cmd and CommandManager.commands[cmd]
- if command then
- if command.localPlayerOnly and ply != LocalPlayer() then return end
- if command.friendsOnly and ply != LocalPlayer() and !ply:IsFriend( LocalPlayer() ) then return end
- local success, response = xpcall( command.callback, CommandManager.onError, unpack( args ) )
- if !success then
- ErrorNoHalt( response )
- response = 'Error executing command'
- end
- if response then
- if command.localPlayerOnly then
- chat.AddText( string.format( "You executed '%s': %s", cmd, response ) )
- else
- local SayFunction = localChat and SayLocal or Say
- SayFunction( string.format( [[;"%s" executed '%s': %s]], ply:Name(), cmd, response ) )
- end
- else
- chat.AddText( string.format( "%s executed '%s'", ply:Name(), cmd ) )
- end
- end
- end,
- onError = function( error )
- return error
- end,
- initHooks = function()
- hook.Add( 'OnPlayerChat', 'AokiChatCommands', CommandManager.onMessage )
- end,
- }
- -- CommandManager.addCommand({
- -- name = 'test',
- -- args = '...any args',
- -- description = 'arg tester',
- -- callback = function( ... )
- -- PrintTable({ ... })
- -- end,
- -- })
- local function makeFunctionName( command )
- local permittedFor
- if command.localPlayerOnly then
- permittedFor = 'owner'
- elseif command.friendsOnly then
- permittedFor = 'friends'
- else
- permittedFor = 'all'
- end
- return "%s (%s)" % { command.name, permittedFor }
- end
- CommandManager.addCommand({
- name = 'help',
- args = '[commandName]',
- description = 'displays available commands or information about a command',
- callback = function( commandName )
- if !commandName then
- local commands = {}
- for _, command in pairs( CommandManager.commands ) do
- table.insert( commands, #commands, makeFunctionName( command ) )
- end
- return table.concat( commands, ', ' )
- end
- local command = CommandManager.commands[commandName:lower()]
- if command then
- local name = makeFunctionName( command )
- return string.format( '%s %s - %s', name, command.args, command.description )
- end
- return string.format( '"%s" command does not exist', commandName )
- end,
- })
- include( './color-utils.lua' )( CommandManager )
- include( './text-utils.lua' )( CommandManager )
- include( './find.lua' )( CommandManager )
- include( './dnd.lua' )( CommandManager )
- include( './include.lua' )( CommandManager )
- include( './goto-and-siton.lua' )( CommandManager )
- include( './apartment.lua' )( CommandManager )
- include( './sit.lua' )( CommandManager )
- include( './rewear.lua' )( CommandManager )
- CommandManager.initHooks()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement