Example: dice-roll
What ships is the extension/ folder: manifest.json and main.js. The test sits beside it, outside the package. Back to all examples.
extension/manifest.json
{
"id": "com.example.dice-roll",
"name": "Dice Roll",
"version": "1.0.0",
"entry": "main.js",
"description": "Adds /roll: rolls dice with Rin Chat's dice engine and posts the result in the chat.",
"permissions": ["ui.command", "chat.send", "ui.notify"],
"author": { "name": "Example Author", "key": "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" }
}
extension/main.js
// @ts-check
/// <reference types="@rin-chat/extension-api" />
// Dice Roll: one slash command. Rin Chat rolls the dice (the extension only reads the result) and the
// extension posts it into the chat under its own name.
rin.ui.command("roll", "Roll dice, such as /roll 2d6+1 or /roll d20");
rin.on("onCommand", async ({ name, args }) => {
if (name !== "roll") return;
const expr = args || "d20";
let result;
try {
result = await rin.dice.roll(expr);
} catch {
// bad_request: not an expression the dice engine reads.
await rin.ui.notify(`"${expr.slice(0, 40)}" is not a dice roll. Try 2d6+1, d20 or 4d6kh3.`);
return;
}
await rin.chat.send(`Rolled ${result.expr}: ${result.breakdown} (total ${result.total})`);
});
dice-roll.test.mjs
Run it from the example's folder: rinext test extension dice-roll.test.mjs.
// rinext test extension dice-roll.test.mjs
import assert from "node:assert/strict";
export async function offersTheCommand({ open }) {
const chat = await open();
assert.deepEqual(chat.commands().map((c) => c.cmd), ["/roll"]);
}
export async function rollsAndPostsTheResult({ open }) {
const chat = await open();
assert.equal(await chat.command("/roll 2d6+1"), true);
assert.equal(chat.posts.length, 1);
const m = /^Rolled 2d6\+1: .* \(total (\d+)\)$/.exec(chat.posts[0].text);
assert.ok(m, chat.posts[0].text);
const total = Number(m[1]);
assert.ok(total >= 3 && total <= 13);
}
export async function defaultsToOneD20({ open }) {
const chat = await open();
await chat.command("/roll");
assert.match(chat.posts[0].text, /^Rolled d20: /);
}
export async function explainsAnExpressionItCannotRoll({ open }) {
const chat = await open();
await chat.command("/roll banana");
assert.equal(chat.posts.length, 0);
assert.match(chat.notices.join("\n"), /"banana" is not a dice roll/);
}