Advertisement
DrawingJhon

CMDFunction

Sep 8th, 2020 (edited)
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.29 KB | None | 0 0
  1. local CMDS = {}
  2.  
  3. function splitSpaces(str)
  4.     local sp = str:split(' ')
  5.     return table.concat(sp, '')
  6. end
  7.  
  8.  
  9. function execCmd(speaker, cmd, args, cmdbar)
  10.     if typeof(cmd) ~= "string" then error("Cannot execute cmd of type "..tostring(typeof(cmd))) end
  11.     if typeof(args) ~= "table" then error("Cannot find arguments of type "..tostring(typeof(args))) end
  12.     for i = 1,#CMDS do
  13.         local info = CMDS[i]
  14.         if cmd == info.CMD or table.find(info.ALIAS, cmd) then
  15.             local func = info.FUNC
  16.             getfenv(func).CMD = cmd
  17.             if cmd:lower() == "wait" then
  18.                 func(speaker, args)
  19.             else
  20.                 coroutine.wrap(func)(speaker, args, cmdbar)
  21.             end
  22.         end
  23.     end
  24. end
  25.  
  26.  
  27. function cmdFunc(speaker, message, commandBar)
  28.     local function sepCmd(str)
  29.         local sp = str:split("|")
  30.         return function(func)
  31.             for i = 1,#sp do
  32.                 func(sp[i])
  33.             end
  34.         end
  35.     end
  36.     local sp = sepCmd(message:gsub("^/e ", ""))
  37.     sp(function(msg)
  38.         local executed = false
  39.         for i = 1,#CMDS do
  40.             if executed then break end
  41.             local info = CMDS[i]
  42.             local cmd = info.CMD
  43.             local numArgs = #info.ARGS
  44.             local sep = msg:split(' ')
  45.             local ArgCmd
  46.             local Args = {}
  47.             local finished = false
  48.             for i = 1, #sep do
  49.                 if finished then
  50.                     break
  51.                 elseif sep[i] == "" then
  52.                 elseif not ArgCmd then
  53.                     ArgCmd = sep[i]
  54.                 elseif Args[numArgs - 1] or numArgs == 1 then
  55.                     table.insert(Args, sep[i])
  56.                     local AAA = {}
  57.                     for z = i, #sep do
  58.                         table.insert(AAA, sep[z])
  59.                     end
  60.                     local rest = table.concat(AAA, ' ')
  61.                     Args["rest"] = rest
  62.                     Args["REST"] = rest
  63.                     finished = true
  64.                 elseif numArgs > 0 then
  65.                     table.insert(Args, sep[i])
  66.                 end
  67.             end
  68.             if ArgCmd:match("^"..prefix) then
  69.                 if ArgCmd:sub(string.len(prefix) + 1):lower() == cmd:lower() then
  70.                     executed = true
  71.                     execCmd(speaker, cmd, Args, commandBar)
  72.                 end
  73.             end
  74.         end
  75.     end)
  76. end
  77.  
  78. function addCmd(cmd, alias, args, func)
  79.     local info = {
  80.         CMD = (type(cmd) == "string") and cmd:lower() or error("The name of a command is needed to run");
  81.         ALIAS = alias or {};
  82.         ARGS = args or {};
  83.         FUNC = func or error("No function detected");
  84.     }
  85.     table.insert(CMDS, info)
  86.     return info
  87. end
  88.  
  89. --[[
  90. //Example:
  91. addCmd("print", {}, {"message"}, function(speaker, args, commandBar)
  92.     print(speaker.Name.." says: "..args.rest)
  93. end)
  94. ]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement