Advertisement
Guest User

CommandAPI

a guest
Dec 16th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. local CommandApi = {}
  2. local Command = {}
  3.  
  4. local Commands = {}
  5. local Prefix = "]"
  6.  
  7. Command.__index = Command
  8.  
  9. Command.Create = function(_, name, callback)
  10. local cmd = {}
  11. setmetatable(cmd, Command)
  12. cmd.name = name
  13. cmd.callback = callback
  14. return cmd
  15. end
  16.  
  17. Command.Run = function(_, args)
  18. _.callback(unpack(args))
  19. end
  20.  
  21. Command.GetName = function(_)
  22. return _.name
  23. end
  24.  
  25. CommandApi.GetCommands = function(_)
  26. return Commands
  27. end
  28.  
  29. CommandApi.FindCommandFromName = function(_, name)
  30. for i,v in pairs(Commands) do
  31. if v:GetName() == name then
  32. return v
  33. end
  34. end
  35. end
  36.  
  37. CommandApi.SetPrefix = function(_, prefix)
  38. Prefix = prefix
  39. end
  40.  
  41. CommandApi.GetPrefix = function(_)
  42. return Prefix
  43. end
  44.  
  45. CommandApi.AddCommand = function(_, name, callback)
  46. table.insert(Commands, Command:Create(name, callback))
  47. end
  48.  
  49. CommandApi.DelCommand = function(_, name)
  50. for i,v in pairs(Commands) do
  51. if v:GetName() == name then
  52. table.remove(Commands, i)
  53. end
  54. end
  55. end
  56.  
  57. CommandApi.RunCommand = function(_, name, args)
  58. local cmd = CommandApi:FindCommandFromName(name)
  59. if not cmd then return false end
  60. cmd:Run(args)
  61. return true
  62. end
  63.  
  64. CommandApi.OnChatted = function(msg)
  65. local function strStarts(String,Start)
  66. return string.sub(String,1,string.len(Start))==Start
  67. end
  68.  
  69. if strStarts(msg, CommandApi:GetPrefix()) then
  70. local args = {}
  71. local name = ""
  72. for i in string.gmatch(msg, "%S+") do
  73. table.insert(args, i)
  74. end
  75. name = string.gsub(args[1], CommandApi:GetPrefix(), "")
  76. table.remove(args, 1)
  77. CommandApi:RunCommand(name, args)
  78. end
  79. end
  80.  
  81. return CommandApi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement