Advertisement
Aoki1337

Untitled

Apr 13th, 2025 (edited)
210
0
28 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.05 KB | None | 0 0
  1. local function expect( value, name, expectedType )
  2.     local actualType = type( value )
  3.  
  4.     if actualType != expectedType then
  5.         error( string.format( 'TypeError: parameter %s has wrong type (%s expected, got %s)', name, expectedType, actualType ) )
  6.     end
  7. end
  8.  
  9. CommandManager = {
  10.     prefixKeyword = 'aoki',
  11.     prefixes = {
  12.         '/',
  13.         '!',
  14.         '.',
  15.     },
  16.     commands = {},
  17.     addCommand = function( params )
  18.         expect( params.name, 'name', 'string' )
  19.         expect( params.args, 'args', 'string' )
  20.         expect( params.description, 'description', 'string' )
  21.         expect( params.localPlayerOnly or false, 'localPlayerOnly', 'boolean' )
  22.         expect( params.friendsOnly or false, 'friendsOnly', 'boolean' )
  23.         expect( params.callback, 'callback', 'function' )
  24.  
  25.         CommandManager.commands[params.name:lower()] = {
  26.             name = params.name,
  27.             args = params.args,
  28.             description = params.description,
  29.             localPlayerOnly = params.localPlayerOnly,
  30.             friendsOnly = params.friendsOnly,
  31.             callback = params.callback,
  32.         }
  33.     end,
  34.     subPrefix = function( text )
  35.         local usedPrefix = nil
  36.  
  37.         for _, prefix in ipairs( CommandManager.prefixes ) do
  38.             if string.StartsWith( text, prefix ) then
  39.                 usedPrefix = prefix
  40.                 break
  41.             end
  42.         end
  43.  
  44.         if usedPrefix then
  45.             return string.TrimLeft( string.sub( text, #usedPrefix + 1 ) )
  46.         end
  47.     end,
  48.     parseArgs = function( stringArgs )
  49.         local args = string.Explode( '%s+', stringArgs, true )
  50.         local cmd = table.remove( args, 1 ):lower()
  51.  
  52.         if cmd != CommandManager.prefixKeyword then
  53.             return
  54.         end
  55.  
  56.         cmd = table.remove( args, 1 ):lower()
  57.  
  58.         local i = 1
  59.         while i <= #args do
  60.             local quote = nil
  61.  
  62.             if string.StartsWith( args[i], [["]] ) then
  63.                quote = [["]]
  64.             elseif string.StartsWith( args[i], [[']] ) then
  65.                quote = [[']]
  66.             end
  67.  
  68.             if quote then
  69.                 local arg = {}
  70.                 local i2 = i
  71.                 while i2 <= #args do
  72.                     table.insert( arg, args[i2] )
  73.  
  74.                     if string.EndsWith( args[i2], quote ) then
  75.                         for j = i, i2 do
  76.                             table.remove( args, i )
  77.                         end
  78.  
  79.                         arg = table.concat( arg, ' ' )
  80.                         table.insert( args, i, string.sub( arg, 2, #arg - 1 ) )
  81.  
  82.                         i = i2
  83.                         break
  84.                     end
  85.  
  86.                     i2 = i2 + 1
  87.                 end
  88.             end
  89.  
  90.             i = i + 1
  91.         end
  92.  
  93.         return cmd, args
  94.     end,
  95.     onMessage = function( ply, message, _, _, localChat )
  96.         message = CommandManager.subPrefix( message )
  97.         if !message then return end
  98.  
  99.         local cmd, args = CommandManager.parseArgs( message )
  100.         local command = cmd and CommandManager.commands[cmd]
  101.  
  102.         if command then
  103.             if command.localPlayerOnly and ply != LocalPlayer() then return end
  104.             if command.friendsOnly and ply != LocalPlayer() and !ply:IsFriend( LocalPlayer() ) then return end
  105.  
  106.             local success, response = xpcall( command.callback, CommandManager.onError, unpack( args ) )
  107.  
  108.             if !success then
  109.                 ErrorNoHalt( response )
  110.                 response = 'Error executing command'
  111.             end
  112.  
  113.             if response then
  114.                 if command.localPlayerOnly then
  115.                     chat.AddText( string.format( "You executed '%s': %s", cmd, response ) )
  116.                 else
  117.                     local SayFunction = localChat and SayLocal or Say
  118.                     SayFunction( string.format( [[;"%s" executed '%s': %s]], ply:Name(), cmd, response ) )
  119.                 end
  120.             else
  121.                 chat.AddText( string.format( "%s executed '%s'", ply:Name(), cmd ) )
  122.             end
  123.         end
  124.     end,
  125.     onError = function( error )
  126.         return error
  127.     end,
  128.     initHooks = function()
  129.         hook.Add( 'OnPlayerChat', 'AokiChatCommands', CommandManager.onMessage )
  130.     end,
  131. }
  132.  
  133. -- CommandManager.addCommand({
  134. --     name = 'test',
  135. --     args = '...any args',
  136. --     description = 'arg tester',
  137. --     callback = function( ... )
  138. --         PrintTable({ ... })
  139. --     end,
  140. -- })
  141.  
  142. local function makeFunctionName( command )
  143.     local permittedFor
  144.  
  145.     if command.localPlayerOnly then
  146.         permittedFor = 'owner'
  147.     elseif command.friendsOnly then
  148.         permittedFor = 'friends'
  149.     else
  150.         permittedFor = 'all'
  151.     end
  152.  
  153.     return "%s (%s)" % { command.name, permittedFor }
  154. end
  155.  
  156. CommandManager.addCommand({
  157.     name = 'help',
  158.     args = '[commandName]',
  159.     description = 'displays available commands or information about a command',
  160.     callback = function( commandName )
  161.         if !commandName then
  162.             local commands = {}
  163.  
  164.             for _, command in pairs( CommandManager.commands ) do
  165.                 table.insert( commands, #commands, makeFunctionName( command ) )
  166.             end
  167.  
  168.             return table.concat( commands, ', ' )
  169.         end
  170.  
  171.         local command = CommandManager.commands[commandName:lower()]
  172.  
  173.         if command then
  174.             local name = makeFunctionName( command )
  175.             return string.format( '%s %s - %s', name, command.args, command.description )
  176.         end
  177.  
  178.         return string.format( '"%s" command does not exist', commandName )
  179.     end,
  180. })
  181.  
  182. include( './color-utils.lua' )( CommandManager )
  183. include( './text-utils.lua' )( CommandManager )
  184. include( './find.lua' )( CommandManager )
  185. include( './dnd.lua' )( CommandManager )
  186. include( './include.lua' )( CommandManager )
  187. include( './goto-and-siton.lua' )( CommandManager )
  188. include( './apartment.lua' )( CommandManager )
  189. include( './sit.lua' )( CommandManager )
  190. include( './rewear.lua' )( CommandManager )
  191.  
  192. CommandManager.initHooks()
  193.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement