Skip to content

Rust

Rust is the language PhoenixCord and its bundled plugins are written in. The phoenixcord-sdk crate contains the bindings generated from the WIT definition plus a few conveniences. Its crate documentation is generated from the same doc comments as the API reference.

A plugin is a cdylib built for the wasm32-wasip2 target, which produces a WebAssembly component directly.

[lib]
crate-type = ["cdylib"]
[dependencies]
phoenixcord-sdk = { path = "../../crates/sdk" }

Implement Plugin and export it with phoenixcord_sdk::plugin!. The struct is created with Default when the plugin starts and dropped when it stops. Every hook has a default implementation, so implement only the hooks listed in PluginInfo::hooks.

use phoenixcord_sdk::types::{Hook, OutgoingMessage, PluginInfo, SendDecision};
use phoenixcord_sdk::Plugin;
#[derive(Default)]
struct Shout;
impl Plugin for Shout {
fn info() -> PluginInfo {
PluginInfo {
hooks: vec![Hook::MessagePreSend],
..PluginInfo::new("Shout", "Sends every message in capitals", &["you"])
}
}
fn pre_send(&mut self, message: OutgoingMessage) -> SendDecision {
SendDecision::Replace(message.content.to_uppercase())
}
}
phoenixcord_sdk::plugin!(Shout);

WIT names map to Rust names the usual way:

WIT Rust
host.get-setting phoenixcord_sdk::host::get_setting
record plugin-info phoenixcord_sdk::types::PluginInfo
variant send-decision { replace(string) } SendDecision::Replace(String)
option<T> Option<T>
list<T> Vec<T>

Imported functions take strings, lists and records by reference: host::fetch(&request).

Crates that compile to wasm32-wasip2 work, including serde_json, regex and url. Anything that needs threads, raw sockets or the file system does not; use the host’s HTTP and storage functions instead.