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: sendswizzin.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")What it does
Section titled “What it does”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.
Expected result
Section titled “Expected result”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.
Common mistakes
Section titled “Common mistakes”- Registering a name that is already a built-in (
theme,channels,scriptsandscriptare also reserved), or one an enabled alias already uses: the alias wins. - Using
ctx.targetwithout checking it fornil. - Forgetting
permissions: send.
Troubleshooting
Section titled “Troubleshooting”/hellosays unknown command: the script is Off or Needs approval.- See Registering commands for precedence and the toolbar action
script:hello.