/reload

Scripting guide

Make IRChatterbox do more: answer triggers, add your own commands and menus, react to joins and kicks, fetch from the web. Write it in JavaScript, or bring your mIRC scripts.

Getting started

Scripts live in the plugins folder inside IRChatterbox's data folder. The quickest way there is Settings → Plugins → Open data folder, then open plugins.

System Plugins folder
Windows %APPDATA%\IRChatterbox\plugins
macOS ~/Library/Application Support/IRChatterbox/plugins
Linux ~/.config/IRChatterbox/plugins

1. Create a file in that folder, for example hello.js:

on("message", function (e) {
  if (e.text === "!hello") {
    reply(prefix + "Hello, " + e.nick + "!");
  }
});

2. Type /reload in IRChatterbox. The server window lists every script that loaded, and any errors with their line numbers.

3. When someone types !hello, you answer [Bot] Hello, bob!.

Every .js and .mrc file in the folder loads at startup and on /reload. Files whose names start with _ are skipped: _example.js and _example.mrc are ready-made examples to copy and rename. All scripts can be switched off at once with Settings → Plugins → Script plugins.

Scripts run in the desktop app (Windows, macOS and Linux).

Two languages

Both kinds sit in the same folder and run side by side.

How scripts run

Events

on("eventName", function (e) { ... });

Every event object has these fields:

Field Meaning
e.server The network name from the server editor, e.g. Libera.Chat
e.serverId IRChatterbox's internal id for that server
e.me Your current nick on that network
e.target Where reply() sends to: the channel, or the person in a private chat
Event When Extra fields
message Anyone, including you, sends a normal message channel, nick, text (formatting removed), raw (with colour codes), self, isChannel, isPM
action Someone else sends a /me action same as message
notice A user sends a NOTICE same as message
join Someone joins a channel channel, nick, self (it was you)
part Someone leaves a channel channel, nick, reason, self
quit Someone disconnects nick, reason, channels (the channels you shared)
kick Someone is kicked channel, nick (who kicked), victim, reason, self (you were kicked)
nick Someone changes nick oldNick, newNick, self, channels
topic A topic changes channel, nick, topic, self
mode Channel modes change channel, nick, modes (e.g. +o bob)
invite Someone invites you channel, nick, invited
connect You finish connecting to a network nick
disconnect You're disconnected reason
raw Every line from the server (advanced) command (e.g. PRIVMSG, 001), params (array), nick, prefix, tags
load The script has just been loaded or reloaded —

Your own messages arrive as message events with e.self set to true. Return early so your bot doesn't answer itself:

on("message", (e) => {
  if (e.self) return;
  // ...
});

Commands

Add your own slash commands. They show up in the / autocomplete list.

command("slap", function (e) {
  // typed: /slap bob with a trout
  // e.args = "bob with a trout", e.argv = ["bob", "with", "a", "trout"]
  if (!e.argv.length) return log("usage: /slap nick");
  action(e.channel, "slaps " + e.argv[0] + " around a bit with a large trout");
});

The command event has command, args, argv, channel (the window you typed in), server, me and isChannel. Built-in commands such as /join and /kick win over a script command with the same name.

Add items to IRChatterbox's right-click menus. They appear at the bottom, after a separator.

menu("nicklist", "Slap", function (e) {
  action(e.channel, "slaps " + e.nick + " around a bit with a large trout");
});

menu("nicklist,query", "Fun/Hug", function (e) {   // "Fun/Hug" makes a Fun submenu
  action(e.channel || e.nick, "hugs " + e.nick);
});

menu("message", "Quote", function (e) {
  reply("> " + e.nick + ": " + e.text);
});
Menu Opens when you right-click Event has
nicklist a nick, in the user list or the chat nick, channel
channel a channel in the sidebar channel
query a private chat in the sidebar nick, channel (the private chat)
status a server in the sidebar —
message a message in the chat nick, text, channel

Every menu event also has menu, server, me and isChannel. reply() answers in the window the menu was opened from. List several menus with commas: menu("nicklist,query", …).

Sending

Function What it does
reply(text) Sends to wherever the event happened (a channel or a private chat)
say(target, text) Sends a message to a channel or nick
action(target, text) Sends a /me action
notice(target, text) Sends a NOTICE
run("/command args") Runs any command as if you typed it, e.g. run("/join #test")
raw("IRC LINE") Sends a raw IRC line, e.g. raw("MODE #chan +m")

Long messages are split automatically, and everything goes through the same flood protection as your own typing. Sending always uses the network the event came from.

Information

Function Returns
me() Your current nick
channels() The channels you're in on this network, e.g. ["#linux", "#neowin"]
users("#chan") [{ nick, prefix, away }, …], where prefix is "@", "+", "@+" and so on
isOp("#chan", nick) true if that nick is a channel operator or higher
stripFormatting(text) The text without IRC colour and bold codes
prefix Your bot reply prefix from Settings (a string, not a function)

Talking to you

Function What it does
log(text) Prints [script.js] text in the server window. Only you see it.
notify(title, body) Shows a desktop notification

Timers

const id = setTimeout(() => reply("5 seconds later"), 5000);
clearTimeout(id);

// every 10 minutes (intervals run at most once a second)
const t = setInterval(() => say("#mychan", "Remember to hydrate 💧"), 10 * 60 * 1000);
clearInterval(t);

A timer remembers where it was created, so reply() inside it goes to the same channel. All of a script's timers are cancelled when it's reloaded.

Storage

Each script gets its own storage that survives restarts:

store.set("count", 5);
store.get("count", 0);   // 5 (the second argument is the default when the key is missing)
store.delete("count");
store.keys();            // ["…", …]

Values can be numbers, strings, booleans, arrays or plain objects. They're kept in plugins/data/<script name>.json, which you can open and edit.

Web requests

const html = httpGet("https://example.com");     // the body as a string, "" on error
const data = httpGetJSON("https://api.github.com/repos/phpandrew/IRChatterbox"); // parsed JSON, or null
if (data) reply(prefix + "⭐ " + data.stargazers_count + " stars");

Requests time out after 12 seconds, and responses are capped at 512 KB for text and 2 MB for JSON. For a simple "type !x, fetch a URL, post the result" command you don't need a script: use Settings → Plugins → URL commands.

Helpers

Function Returns
toF(celsius) The temperature in Fahrenheit
toC(fahrenheit) The temperature in Celsius

Examples

Greeter

on("join", (e) => {
  if (!e.self && e.channel === "#mychan") {
    say(e.channel, `${prefix}Welcome to ${e.channel}, ${e.nick}! 👋`);
  }
});

Karma: nick++ and nick--

on("message", (e) => {
  const m = e.text.match(/^(\S+)(\+\+|--)$/);
  if (!m || m[1].toLowerCase() === e.nick.toLowerCase()) return; // no self-karma
  const key = "karma:" + m[1].toLowerCase();
  const n = store.get(key, 0) + (m[2] === "++" ? 1 : -1);
  store.set(key, n);
  reply(`${prefix}${m[1]} now has ${n} karma`);
});

command("karma", (e) => {
  const who = (e.argv[0] || me()).toLowerCase();
  log(`${who}: ${store.get("karma:" + who, 0)} karma`);
});

Seen

on("message", (e) => {
  store.set("seen:" + e.nick.toLowerCase(), { when: Date.now(), where: e.channel, text: e.text });
  const m = e.text.match(/^!seen (\S+)/);
  if (!m) return;
  const s = store.get("seen:" + m[1].toLowerCase(), null);
  if (!s) return reply(`${prefix}I haven't seen ${m[1]}.`);
  const mins = Math.round((Date.now() - s.when) / 60000);
  reply(`${prefix}${m[1]} was last seen ${mins} min ago in ${s.where}: "${s.text}"`);
});

Auto-op your friends

const friends = ["alice", "bob"];
on("join", (e) => {
  if (!e.self && friends.includes(e.nick.toLowerCase()) && isOp(e.channel, me())) {
    run(`/op ${e.nick}`);
  }
});

Rejoin when kicked

on("kick", (e) => {
  if (e.self) setTimeout(() => run("/join " + e.channel), 3000);
});

Answer a server numeric

on("raw", (e) => {
  if (e.command === "001") log("Registered on " + e.server); // RPL_WELCOME
});

mIRC scripts

Copy a .mrc file into the plugins folder and type /reload. IRChatterbox isn't a full mIRC: it covers the parts most shared scripts use, and skips anything that touches files, programs or windows. The server window lists anything a script uses that was skipped.

alias hello {
  if ($1 == $null) { echo -a Usage: /hello nick | return }
  msg $chan Hello $1 $+ , from $me!
}

on *:TEXT:!dice:#:msg $chan $nick rolled a $rand(1,6)

on !*:JOIN:#:notice $nick Welcome to $chan $+ , $nick $+ !

Definitions

alias name { … } Becomes /name, and $name(args) returns its /return value. alias -l works too.
on <level>:<EVENT>:…:{ … } Events below. Levels: *, 1, me:, ! (not for you), @ (only when you're an op), $ (regex match)
raw <numeric>:<match>:{ … } Server numerics, e.g. raw 332:*: or raw *:*:
menu nicklist,channel,query,status { … } Right-click menu items
#group on … #group end Accepted (the group lines are ignored)
; comments, /* … */ blocks Ignored

Events: TEXT, ACTION, NOTICE (match the text and #, ?, * or a channel list), JOIN, PART, KICK, TOPIC, INVITE, RAWMODE, INPUT, QUIT, NICK, CONNECT, DISCONNECT, START, LOAD.

TEXT, ACTION and NOTICE run for other people's messages, as in mIRC. Matches use mIRC wildcards (*, ?, &); with the $ level the match is a regex like /^!w(eather)?\s/i.

on INPUT runs on what you type in a channel. /halt or /haltdef stops the line being sent, so a script can rewrite it:

on *:INPUT:#:{ if ($1 == !shout) { say $upper($2-) | halt } }

Commands

Identifiers

Operators in if and while: ==, ===, !=, <, >, <=, >=, //, \\, isin, isincs, iswm, iswmcs, isnum, isalpha, isalnum, isletter, islower, isupper, ison, isop, ishop, isvoice, isreg, ischan. Put ! in front to negate (!isin), and combine with &&, || and !( … ).

Right-click menus in mIRC scripts

menu nicklist {
  Whois:whois $$1
  -
  Fun
  .Slap:me slaps $$1 around a bit with a large trout
  .Hug:{
    me hugs $$1
    inc %hugs
  }
  $iif($me isop $chan, Op tools)
  .Kick:kick $chan $$1
}

Not supported

These are skipped when a script loads, or do nothing when called:

A script that uses these mostly still loads; only those parts don't run.

Good to know

Troubleshooting

Found something the API can't do yet? Open an issue on GitHub.