Kharat Bot Docs

KAL v2 overview

What the Kharat Automation Language is, how scripts compile and run, and the shape every program follows.

KAL (the Kharat Automation Language) is the programming language behind custom commands. Version 2 compiles to bytecode and runs in a sandboxed virtual machine inside the bot β€” deterministic, resource-capped, and safe to let any administrator run.

A complete program#

kal
KAL VERSION 2.0;
AUTOMATION status_report {

    WHEN COMMAND status {
        REPLY "All systems operational.";
    }
}

That is a working command: run /status (or ,status) and the bot replies. Two structural rules apply to every program:

  1. Line 1 must be exactly KAL VERSION 2.0; β€” the compiler rejects anything else with E001_MALFORMED_VERSION_HEADER. There is no v1 fallback; v1 was discontinued.
  2. Everything lives inside one AUTOMATION name { … } block.

The mental model#

text
source (.kal text)
   β”‚  compile  β†’ bytecode + validation (static checks, context rules)
   β–Ό
registered on the bot (per server, per script)
   β”‚  trigger fires (command / event / interaction / schedule)
   β–Ό
VM runs the bytecode  β†’  Discord actions  β†’  state written or rolled back

Compilation happens on save. Runtime errors cannot corrupt your server: each run has a budget (instructions, wall clock, Discord actions), and if a run crashes, its state changes are rolled back.

What a program can contain#

Building blockStatementChapter
Server settingsCONFIGSyntax
DataDECLARE, SET, CONSTANT, FUNCTIONSyntax
DecisionsIF, WHILE, FOR EACH, TRYControl flow
TriggersWHEN COMMAND, WHEN EVENT, WHEN INTERACTION, SCHEDULETriggers
EffectsREPLY, SEND EMBED, TIMEOUT, >30 moreActions
UIbuttons, select menus, modalsComponents
MemorySTATE GET/PUT/APPEND/…State
LibrariesIMPORT { … } FROM "…";Modules

A taste of each trigger#

kal
KAL VERSION 2.0;
AUTOMATION tour {

    WHEN COMMAND greet (name AS STRING) {
        REPLY "Hello, " + name + "!";
    }

    WHEN EVENT member_join {
        LOG "New member: " + member.name;
    }

    WHEN INTERACTION verify_button {
        REPLY "You are verified!" EPHEMERAL;
    }
}

All three triggers can coexist in one program. Scheduled jobs are declared slightly differently (SCHEDULE EVERY … { … }) and get a smaller context β€” see Triggers.

The sandbox, in one table#

ResourceCap
Source size200,000 characters
Instructions per run50,000
Wall clock per run5 seconds
Discord actions per run5
State value size2,048 bytes (100 keys per script)
Call depth64

The full limit table and every error code live in Diagnostics & limits.

How this documentation is validated#

Every ```kal example in these guides is compiled against the real compiler as part of the site's test suite. If an example is shown as KAL code, it builds. Syntax fragments that are not complete programs are marked kal (fragment) in the code bar and are intentionally not compiled.