Table of Contents

smudgy:core — Automations

Generated from smudgy v0.3.5 (smudgy-core.d.ts @ cfcea3b156f6). Index: scriptref.

Aliases, triggers, timers, and hotkeys created from scripts: the create* functions, the handles they return, and the registries. These are cleared and recreated on every script reload. For the saved automations shown in the automations window, see saved-automations.

createAlias

export function createAlias(
  name: string,
  patterns: Pattern | Pattern[],
  script: AutomationScript,
  options?: AliasOptions,
): Alias;

Create an alias: a shortcut that watches what you type and runs a script instead of sending it. patterns is one regex or several; when your input matches, script runs: a command template string, or a function that receives the Matches.

import { createAlias } from "smudgy:core";
// Typing "gt any message here" sends "guildtell any message here".
createAlias("gt", "^gt (.+)$", "guildtell $1");

The typed command is consumed by default (see capture). Aliases created this way last until the next script reload. Returns an Alias handle.

createTrigger

export function createTrigger(
  name: string,
  patterns: Pattern | TriggerPatterns,
  script: AutomationScript,
  options?: TriggerOptions,
): Trigger;

Create a trigger: it watches every line arriving from the MUD and runs a script on a match. patterns is one regex, or a TriggerPatterns object for raw/anti patterns; script is a command template string, or a function that receives the Matches.

import { createTrigger, send } from "smudgy:core";
// Congratulate, reusing the captured name.
createTrigger("gz", "^(\\w+) has advanced a level", "say Grats, $1!");
// A function body can decide what to do; named groups arrive by name.
createTrigger("lowhp", /^(?<hp>\d+)H /, ({ hp }) => {
  if (parseInt(hp) < 100) send("flee");
});

Triggers created this way last until the next script reload; see TriggerOptions for prompt matching, fire limits, and more. Returns a Trigger handle.

createTriggers

export function createTriggers(triggers: Record<string, TriggerDef>): Record<string, Trigger>;

Create several triggers in one call: pass an object mapping each name to its TriggerDef; get back the same names mapped to their Trigger handles.

createTimer

export function createTimer(name: string, options: TimerOptions, callback: () => void): Timer;

Create a timer that runs callback after intervalMs milliseconds: once by default, or repeatedly with repeat: true.

import { createTimer, send } from "smudgy:core";
// Keep sipping, every 30 seconds until deleted:
const sip = createTimer("sip", { intervalMs: 30000, repeat: true },
  () => send("drink potion"));
// later: sip.delete();

Timers are cleared on script reload. Returns a Timer handle; set enabled = false to pause it, or delete() to stop it.

createHotkey

export function createHotkey(name: string, keySpec: KeySpec, handler: () => void): Hotkey;

Bind a keyboard shortcut: handler runs whenever the KeySpec combination is pressed in this session.

import { createHotkey, send } from "smudgy:core";
createHotkey("panic", { key: "F1" }, () => send("flee"));
createHotkey("heal", { key: "h", modifiers: ["ctrl"] }, () => send("cast 'heal' self"));

Hotkeys are cleared on script reload. Returns a Hotkey handle.

Alias

export interface Alias {
  readonly name: string;
  readonly created?: boolean;
  enabled: boolean;
  readonly pattern: string;
  delete(): void;
}

A handle to a script-created alias: enable/disable it with enabled, remove it with delete(). Returned by createAlias.

Trigger

export interface Trigger {
  readonly name: string;
  readonly created?: boolean;
  enabled: boolean;
  readonly pattern: string;
  delete(): void;
}

A handle to a script-created trigger; the same shape as Alias. Returned by createTrigger.

Timer

export interface Timer {
  readonly name: string;
  enabled: boolean;
  delete(): void;
}

A handle to a script-created timer. Returned by createTimer; timers are cleared on script reload.

Hotkey

export interface Hotkey {
  readonly name: string;
  enabled: boolean;
  delete(): void;
}

A handle to a script-created hotkey. Returned by createHotkey; hotkeys are cleared on script reload.

aliases

export const aliases: AutomationRegistry<Alias>;

The registry of aliases your scripts created.

triggers

export const triggers: AutomationRegistry<Trigger>;

The registry of triggers your scripts created.

timers

export const timers: AutomationRegistry<Timer>;

The registry of timers your scripts created.

hotkeys

export const hotkeys: AutomationRegistry<Hotkey>;

The registry of hotkeys your scripts created.

AutomationRegistry

export interface AutomationRegistry<H> {
  get(name: string): H | undefined;
  list(): string[];
  exists(name: string): boolean;
}

Look up the automations of one kind that your own scripts created. Each script sees only its own; two scripts can both own a "heal" trigger without colliding.

Matches

export type Matches = {
  readonly [group: number]: string;
  readonly [name: string]: string;
};

The captures handed to a trigger or alias handler. matches[0] is the whole matched text; matches[1], matches[2], and so on are the capture groups in order. A named group like (?<who>...) can also be read by name, as matches.who, and handlers often destructure it: ({ who }) => .... Every group of the pattern that fired is present: one that matched nothing (an optional group, say) is the empty string, not undefined as in standard JavaScript regex matches.

When a trigger has several patterns, only the fired pattern's groups are present; the other patterns' groups are absent and read as undefined. "who" in matches tells you which pattern fired.

InlineTemplate

export type InlineTemplate = string;

A trigger/alias body written as a plain string instead of a function: a command template sent to the MUD after substitution.

Unknown or non-matching groups become the empty string.

TriggerPatterns

export type TriggerPatterns = {
  patterns?: Pattern[];
  rawPatterns?: Pattern[];
  antiPatterns?: Pattern[];
};

The three pattern lists a trigger can match with. Most triggers set only patterns.

TriggerDef

export type TriggerDef = TriggerPatterns & {
  script: InlineTemplate | ((matches: Matches) => string | void);
  prompt?: boolean;
  enabled?: boolean;
  singleton?: boolean;
  fireLimit?: number;
  lineLimit?: number;
};

One trigger in a createTriggers batch: its patterns, its body, and the same options as TriggerOptions.

AliasOptions

export type AliasOptions = {
  singleton?: boolean;
  fireLimit?: number;
};

Options for createAlias.

TriggerOptions

export type TriggerOptions = {
  prompt?: boolean;
  enabled?: boolean;
  singleton?: boolean;
  fireLimit?: number;
  lineLimit?: number;
};

Options for createTrigger.

TimerOptions

export type TimerOptions = {
  intervalMs: number;
  repeat?: boolean;
  fireLimit?: number;
};

Options for createTimer.

KeySpec

export type KeySpec = {
  key: string;
  modifiers?: string[];
};

The key combination for createHotkey.

Pattern

This describes a type returned from a function on this page. Typically you'll never construct it directly.

type Pattern = string | RegExp;

A match pattern: a regular expression, written either as a RegExp (/^You follow/) or as a string of regex source ("^You follow"). Strings are compiled as regexes, not matched literally.

AutomationScript

This describes a type returned from a function on this page. Typically you'll never construct it directly.

type AutomationScript = InlineTemplate | ((matches: Matches) => string | void);

Either body form an automation accepts: a command template string (see InlineTemplate), or a function called with the Matches. If the function returns a string, that string is sent to the MUD as a command (aliases apply to it).


Script API reference · smudgy:core — Sessions & output · smudgy:core — Lines & buffer · smudgy:core — Events · smudgy:core — Saved automations · smudgy:core — Panes · smudgy:widgets · Mapper · smudgy:params