Skip to content

Lesson 3: Create a slash command

Goal: add /hello [name].

--! name: Hello command
--! description: /hello [name] says hello in the current channel
--! api: 1
--! permissions: send
swizzin.command("hello", function(ctx, args)
if ctx.target == nil then
swizzin.log("/hello needs a channel or conversation window")
return
end
local who = args[1] or "there"
swizzin.send(ctx.target, `Hello, {who}!`)
end, "Say hello in the current channel")

swizzin.command(name, fn, description?) adds /hello. ctx says where the command was typed (ctx.target, ctx.network, ctx.me, ctx.text). args is a read-only list of the words you typed. swizzin.send needs the send permission, which the header requests and you approve.

In a channel, typing /hello Sam sends Hello, Sam! to the channel. /hello alone sends Hello, there!. In a server window (no target) it only writes a note to the Console.

  • Registering a name that is already a built-in (theme, channels, scripts and script are also reserved), or one an enabled alias already uses: the alias wins.
  • Using ctx.target without checking it for nil.
  • Forgetting permissions: send.
  • /hello says unknown command: the script is Off or Needs approval.
  • See Registering commands for precedence and the toolbar action script:hello.