API_Core - frankischilling/crust GitHub Wiki
Core And Callbacks
This page covers the shared helpers every plugin usually touches first.
c2.log(level, ...parts)
Write a line to the Crust log.
levelis one ofc2.LogLevel.Debug,Info,Warning, orCriticalpartsare converted to strings and joined with spaces
c2.register_command(name, handler, meta?)
Register a slash command.
name: command name with or without the leading/handler: function called ashandler(ctx)meta: optional table withusage,summary, andaliases
The command handler context table includes:
command: normalized command nameraw_text: original command textchannel_name: display name of the current channelchannel: channel snapshot tableaccount: current account snapshot tablewords: tokenized command wordsreply_to_msg_id: omitted when the command was not invoked as a replyreply: omitted when reply metadata is unavailable
The reply table includes:
parent_msg_idparent_user_loginparent_display_nameparent_msg_body
Return behavior:
- return
nilto do nothing - return a string to inject a local system message into the current channel
c2.register_callback(event_type, handler)
Register a Lua callback for host-driven events.
event_type: a value fromc2.EventTypeor the matching event-type stringhandler: function called ashandler(ev)
Callback payloads are documented in API_Events.
Completion callbacks are special:
- register for
c2.EventType.CompletionRequested - return
{ values = { ... }, hide_others = false }
The completion payload includes:
queryfull_text_contentcursor_positionis_first_wordchannel: omitted when completion is requested without channel context
c2.later(callback, msec)
Schedule callback() to run later on the plugin VM.
callback: zero-argument Lua functionmsec: delay in milliseconds
c2.reload_plugins()
Reload all plugins from disk.
- this is an action helper, not a fetch helper
- it causes the host to rebuild the plugin registry from the current plugin directories
- it does not add a new Crust capability beyond the existing Rust reload command
- it does not return a payload table
Read-Only Helpers
c2.plugin_dir()
Return the installed plugin directory path as a string.
c2.plugin_data_dir()
Return the writable per-plugin data directory path as a string.
c2.use_24h_timestamps()
Return true when Crust is currently configured for 24-hour timestamps.
c2.session_started_ms()
Return the current Crust session start time in Unix milliseconds.
c2.current_account()
Return the current account snapshot table.
Fields:
logged_in: always presentusername: omitted when no authenticated account is activeuser_id: omitted when no authenticated account is activedisplay_name: omitted when no authenticated account is active
username, user_id, and display_name are omitted when no authenticated
account is active.
c2.current_channel()
Return the currently active channel snapshot table, or nil when Crust does
not currently have an active channel.
In split view, this follows the focused pane. Otherwise it follows the active channel tab.
c2.channel_by_name(name)
Return a channel snapshot table for the provided channel name.
namemay be a Twitch login, Kick channel, or IRC name
Channel snapshot fields:
name: channel display name as exposed byChannelId::display_name()display_name: same value asnameplatform: one of the values listed belowid: canonical channel identifier stringis_twitchis_ircis_kickis_joined: present when the host has a snapshot for the channelis_mod: present when the host has a snapshot for the channelis_vip: present when the host has a snapshot for the channelis_broadcaster: present when the host has a snapshot for the channel
platform is one of:
twitchkickirc
Example
c2.log(c2.LogLevel.Info, "plugin loaded from", c2.plugin_dir())
c2.register_command("hello", function(ctx)
local account = c2.current_account()
local active_channel = c2.current_channel()
c2.add_system_message(
ctx.channel,
"Hello from " .. tostring(account.display_name or account.username or "Lua") ..
" in " .. tostring(active_channel and active_channel.display_name or "(no active channel)")
)
end, {
usage = "/hello",
summary = "Print a local message",
aliases = { "hi" },
})