Docs
Dashboard
KyroScript

Functions and errors

Your own functions

A function is a set of lines you can use again. Make one with func, give it inputs, and send a value back with return.

Command file
command: progress
description: See your level progress
---
func bar(fraction, width=10):
    filled = round(fraction * width)
    return "▰" * filled + "▱" * (width - filled)

# XP needed to reach a level
func needed(lvl):
    return lvl * lvl * 100

level = levels.level(user)
xp = levels.xp(user)
start = needed(level)
finish = needed(level + 1)
fraction = (xp - start) / (finish - start)
reply("Level **{level}** (rank #{levels.rank(user)})\n{bar(fraction)} {xp - start}/{finish - start} XP")

Inputs can have a default (width=10), and you can pass them by name: bar(0.5, width=20). Functions go at the top level of your script, not inside an if or a loop, and they can be used before or after they’re written.

When something goes wrong

If a line fails (a role that doesn’t exist, dividing by zero, someone without enough coins), the script stops and the person sees a short error. You see the full message and line number in your logs.

  • fail("message") stops the script on purpose and shows your message. Use it for checks like “you need 100 coins”.
  • try: / except err: catches a problem so you can handle it yourself. err.message says what went wrong.
  • error("message") raises a problem that a try around it can catch.
  • print(…) writes a line to the test runner, not to Discord. Handy for checking values.
Command file
command: split
description: Split a bill
arg total: number, required, The bill
arg people: number, required, How many people
---
try:
    each = round(args.total / args.people, 2)
    reply("That’s **{each}** each.")
except err:
    reply("I couldn’t work that out: {err.message}", private=true)
⚠️ Heads up Hitting a limit (too many steps, too slow) and fail() can’t be caught by try.
Was this page helpful?