Module
@ember/commands
Chat commands for the players in this instance; players elsewhere never see them.
local commands = require("@ember/commands")Functions
commands.register(name: string, options: commands.CommandOptions)Registers /name for players in this instance. A name the server already uses, or one registered twice, is an error.
local commands = require("@ember/commands")
local types = require("@ember/types")
-- `/pay <target> <amount>` moves coins from whoever typed it to another player. `run` gets the
-- player who ran the command first, then the arguments keyed by the names given in `args`.
-- Declare the arguments as a typed local: inline, strict mode reads each `kind` as a plain string.
local payArgs: { commands.CommandArg } = {
-- A "player" argument arrives as the player's handle; a name nobody here has is refused before
-- `run` is called.
{ name = "target", kind = "player" },
-- An "integer" arrives as a number, already checked against `min` and `max`.
{ name = "amount", kind = "integer", min = 1, max = 1000 },
}
commands.register("pay", {
description = "Give some of your coins to another player",
args = payArgs,
run = function(sender, args)
local target: types.Player = args.target
local amount: number = args.amount
if target.id == sender.id then
sender:send("<red>You can't pay yourself.")
return
end
if sender.inventory:count("coin") < amount then
sender:send(`<red>You only have {sender.inventory:count("coin")} coins.`)
return
end
sender.inventory:remove("coin", amount)
target.inventory:add("coin", amount)
sender:send(`<green>Sent {amount} coins to {target.name}.`)
target:send(`<green>{sender.name} sent you {amount} coins.`)
end,
})