Ok. So basically, there will be a commands module with a command being a table.
local system = {}
system.commands = require(script.Commands)
system.ranks = {}
system.settings = {} -- we will fix this later
We need to have a function to find a command from it’s call, which is below. It will scan through the command to look for a match.
function system.findCommand(call)
local function validate(commandCall)
return string.lower(call) == string.lower(commandCall)
end
for _, command in ipairs(system.commands) do
if validate(command.name) then
return command
else
for _, aliase in ipairs(command.aliases or {}) do
if validate(aliase) then
return command
end
end
end
end
end
We’ll also need functions to manage the ranks. These will simply read and write to the ranks
dictionary.
function system.setRank(speaker, rank)
system.ranks[tostring(speaker.UserId)] = rank
end
funciton system.getRank(speaker)
return system.ranks[tostring(speaker.UserId)] or 0
end
Once we have the command, we’ll need to have an permissions checker function, and the actual executor function
function system.checkExecutionPermissions(player, command)
return system.getRank(speaker) >= command.rank
end
function system.executeCommand(player, command, arguments)
if system.checkExecutionPermissions(player, command) then
local success, result = pcall(command.run, command, player, arguments)
if not success then
warn("Unable to run "..command.name.." command - "..result)
end
end
end
We have setup the ranks, and execution. Now all that’s left is the parser and setup.
You could have a complex parser that handles batches and conditions and formats it into a parse tree, but we’re keeping things simple here.
function system.parseMessage(message)
if not string.sub(message, 1, #system.settings.prefix) == system.settings.prefix then
return
end
local arguments = string.split(message, " ")
local command = commandData[1]
arguments = select(2, unpack(arguments))
return command, arguments
end
function system.onMessage(speaker, message)
local commandName, arguments = system.parseMessage(message)
if commandName and arguments then
local command = system.findCommand(command)
if command then
system.executeCommand(command, speaker, arguments)
end
end
end
Now we have our setup functions, these will mostly handle signals such as PlayerAdded
, and Chatted
.
local players = game:GetService("Players")
function system.setupPlayer(player)
player.Chatted:Connect(function(message)
system.onMessage(player, message)
end)
end
function system.setup(settings)
system.settings = settings
player.PlayerAdded:Connect(system.setupPlayer)
end