Triggers, Aliases & Timers
Functions for automation: responding to MUD output, creating shortcuts, and timed actions.
Triggers
Triggers automatically execute code when specific text patterns appear in MUD output.
addTrigger
Add a trigger that fires when text matches a pattern.
local triggerId = addTrigger(pattern, callback, options?)| Parameter | Type | Required | Description |
|---|---|---|---|
| pattern | string | Yes | Pattern to match |
| callback | function | Yes | Function to call on match |
| options | table | No | Options table (see below) |
Options Table:
| Option | Type | Default | Description |
|---|---|---|---|
| type | string | ”substring” | Match type: “substring”, “wildcard”, “regex”, “exact” |
| enabled | boolean | true | Whether trigger is active |
| priority | number | 50 | Trigger priority (lower = higher priority) |
| oneShot | boolean | false | Remove trigger after first match |
| keepEvaluating | boolean | true | Continue evaluating other triggers |
| omitFromOutput | boolean | false | Hide matching line from output |
| caseSensitive | boolean | false | Case-sensitive matching |
| matchRawText | boolean | false | Match raw text (with ANSI codes) |
| group | string | nil | Group name for batch operations |
| colorize | table | nil | Highlight matches with colors |
Callback Parameters:
The callback receives the matched line and any captured groups:
function(line, wildcards)
-- line: the full matched line
-- wildcards: table of captured groups (for regex patterns)
endExample:
-- Simple substring trigger
addTrigger("You are hungry", function()
send("eat bread")
end)
-- Regex trigger with captures
addTrigger("(\\w+) tells you '(.+)'", function(line, matches)
local sender = matches[1]
local message = matches[2]
print("Tell from " .. sender .. ": " .. message)
utilprint("$G[Tell] $C" .. sender .. "$w: " .. message) -- Green "[Tell]", Cyan sender
end, { type = "regex" })
-- One-shot trigger (removes itself after firing)
addTrigger("Welcome to the game", function()
send("look")
print("Auto-looked on login!")
utilprint("$Y[Login] $wAuto-look triggered")
end, { oneShot = true })
-- Priority trigger (fires before others)
addTrigger("DANGER:", function()
send("flee")
end, { priority = 10 })
-- Wildcard trigger (matches * and ? patterns)
addTrigger("* gives you *", function(line, matches)
local giver = matches[1]
local item = matches[2]
print("Received " .. item .. " from " .. giver)
utilprint("$G[Received] $w" .. item .. " from $C" .. giver) -- Cyan giver name
end, { type = "wildcard" })
-- Regex HP tracking
addTrigger("HP: (\\d+)/(\\d+)", function(line, matches)
local hp = tonumber(matches[1])
local maxHp = tonumber(matches[2])
setVariable("hp", hp)
setVariable("maxhp", maxHp)
end, { type = "regex" })removeTrigger
Remove a trigger by its ID.
removeTrigger(triggerId)| Parameter | Type | Required | Description |
|---|---|---|---|
| triggerId | string | Yes | The trigger ID returned from addTrigger |
Example:
local myTrigger = addTrigger("some pattern", function()
-- handler
end)
-- Later, remove it
removeTrigger(myTrigger)enableTrigger
Enable a disabled trigger.
enableTrigger(triggerId)| Parameter | Type | Required | Description |
|---|---|---|---|
| triggerId | string | Yes | The trigger ID |
Example:
enableTrigger(combatTrigger)disableTrigger
Disable a trigger without removing it.
disableTrigger(triggerId)| Parameter | Type | Required | Description |
|---|---|---|---|
| triggerId | string | Yes | The trigger ID |
Example:
-- Disable during safe areas
disableTrigger(combatTrigger)findTriggerByName
Find a trigger by its name.
local trigger = findTriggerByName(name)| Parameter | Type | Required | Description |
|---|---|---|---|
| name | string | Yes | The trigger name |
Returns: {id: string, name: string, enabled: boolean} or nil
toggleTriggerByName
Enable or disable a trigger by its name.
local success = toggleTriggerByName(name, enabled)| Parameter | Type | Required | Description |
|---|---|---|---|
| name | string | Yes | The trigger name |
| enabled | boolean | Yes | Whether to enable or disable |
Returns: boolean - Whether the operation succeeded
toggleTriggerGroup
Enable or disable all triggers in a group.
local success = toggleTriggerGroup(groupName, enabled)| Parameter | Type | Required | Description |
|---|---|---|---|
| groupName | string | Yes | The group name |
| enabled | boolean | Yes | Whether to enable or disable |
Returns: boolean - Whether the operation succeeded
Example:
-- Add triggers to a group
addTrigger("combat pattern 1", handler1, { group = "combat" })
addTrigger("combat pattern 2", handler2, { group = "combat" })
-- Disable all combat triggers at once
toggleTriggerGroup("combat", false)
-- Re-enable later
toggleTriggerGroup("combat", true)Aliases
Aliases intercept user input and transform or execute code based on patterns.
addAlias
Add an alias that matches user input.
local aliasId = addAlias(pattern, replacement, callback?)| Parameter | Type | Required | Description |
|---|---|---|---|
| pattern | string | Yes | Pattern to match in user input |
| replacement | string | Yes | Replacement text or command |
| callback | function | No | Optional callback function |
Example:
-- Simple text replacement
addAlias("^nn$", "north;north") -- "nn" expands to "north;north"
-- Alias with wildcards
addAlias("^go (.+)$", "speedwalk $1") -- "go tavern" becomes "speedwalk tavern"
-- Alias with callback
addAlias("^heal (.+)$", "", function(matches)
local target = matches[1]
send("cast 'cure light wounds' " .. target)
print("Healing " .. target .. "...")
utilprint("$G[Heal] $wCasting on " .. target)
end)removeAlias
Remove an alias by its ID.
removeAlias(aliasId)| Parameter | Type | Required | Description |
|---|---|---|---|
| aliasId | string | Yes | The alias ID returned from addAlias |
findAliasByName
Find an alias by its name.
local alias = findAliasByName(name)| Parameter | Type | Required | Description |
|---|---|---|---|
| name | string | Yes | The alias name |
Returns: {id: string, name: string, enabled: boolean} or nil
toggleAliasByName
Enable or disable an alias by its name.
local success = toggleAliasByName(name, enabled)| Parameter | Type | Required | Description |
|---|---|---|---|
| name | string | Yes | The alias name |
| enabled | boolean | Yes | Whether to enable or disable |
Returns: boolean - Whether the operation succeeded
Timers
Timers execute code after a delay or at regular intervals.
addTimer
Add a timer that fires after an interval.
local timerId = addTimer(interval, callback, repeating?)| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
| interval | number | Yes | - | Interval in milliseconds |
| callback | function | Yes | - | Function to execute |
| repeating | boolean | No | false | Whether to repeat |
Example:
-- One-shot timer (fires once after 5 seconds)
addTimer(5000, function()
print("5 seconds have passed!")
utilprint("$Y[Timer] $w5 seconds elapsed")
end)
-- Repeating timer (fires every 10 seconds)
local autoSaveTimer = addTimer(10000, function()
send("save")
print("Auto-saved!")
utilprint("$G[Save] $wAuto-saved!")
end, true)
-- Repeating timer for status updates (every minute)
addTimer(60000, function()
send("score")
end, true)removeTimer
Remove a timer by its ID.
removeTimer(timerId)| Parameter | Type | Required | Description |
|---|---|---|---|
| timerId | string | Yes | The timer ID returned from addTimer |
Example:
local myTimer = addTimer(5000, callback, true)
-- Stop the timer
removeTimer(myTimer)findTimerByName
Find a timer by its name.
local timer = findTimerByName(name)| Parameter | Type | Required | Description |
|---|---|---|---|
| name | string | Yes | The timer name |
Returns: {id: string, name: string, enabled: boolean} or nil
toggleTimerByName
Enable or disable a timer by its name.
local success = toggleTimerByName(name, enabled)| Parameter | Type | Required | Description |
|---|---|---|---|
| name | string | Yes | The timer name |
| enabled | boolean | Yes | Whether to enable or disable |
Returns: boolean - Whether the operation succeeded
JavaScript-Style Timers
Convenience functions that match JavaScript’s timer API.
setTimeout
Execute a function after a delay (one-shot timer).
local timerId = setTimeout(callback, delay)| Parameter | Type | Required | Description |
|---|---|---|---|
| callback | function | Yes | Function to execute |
| delay | number | Yes | Delay in milliseconds |
Returns: string - Timer ID for cancellation
Example:
-- Execute after 2 seconds
setTimeout(function()
print("Timer fired!")
utilprint("$Y[Timeout] $wTimer fired!")
end, 2000)
-- Store ID for potential cancellation
local timerId = setTimeout(function()
send("look")
end, 5000)clearTimeout
Cancel a timeout before it fires.
clearTimeout(timerId)| Parameter | Type | Required | Description |
|---|---|---|---|
| timerId | string | Yes | The timer ID from setTimeout |
Example:
local timerId = setTimeout(function()
send("delayed command")
end, 5000)
-- Cancel it before it fires
clearTimeout(timerId)Complete Automation Example
-- Automation plugin
print("Loading automation...")
-- Auto-heal when health is low
addTrigger("HP: (\\d+)/(\\d+)", function(line, matches)
local hp = tonumber(matches[1])
local maxHp = tonumber(matches[2])
local percent = (hp / maxHp) * 100
if percent < 30 then
colourNote("red", "", "!!! LOW HEALTH !!!")
send("drink healing potion")
end
end, { type = "regex" })
-- Auto-loot after kill
addTrigger("is DEAD", function()
setTimeout(function()
send("get all corpse")
end, 500)
end)
-- Combat triggers
local function attackHandler(line)
print("Under attack!")
utilprint("$R[Combat] $wUnder attack!")
end
addTrigger("attacks you", attackHandler, { group = "combat" })
addTrigger("swings at you", attackHandler, { group = "combat" })
-- Quick movement: /run north (runs 5 times)
addAlias("^run (.+)$", "", function(matches)
local direction = matches[1]
for i = 1, 5 do
send(direction)
end
end)
-- Quick attack: /kk goblin
addAlias("^kk (.+)$", "", function(matches)
toggleTriggerGroup("combat", true)
send("kill " .. matches[1])
end)
-- Auto-save every 5 minutes
addTimer(300000, function()
send("save")
print("Auto-saved!")
utilprint("$w[Save] Auto-saved")
end, true)
-- Status check every minute
addTimer(60000, function()
send("score")
end, true)
print("Automation loaded!")