Skip to content

HTTP, Discord API and timers

Plugins never block. Asynchronous functions return an ID right away and deliver the result later as an event carrying the same ID. Keep the ID in the plugin’s state and match it when the event arrives.

fn start(&mut self) {
self.request = Some(host::fetch(&HttpRequest {
method: "GET".into(),
url: "https://example.com/data.json".into(),
headers: Vec::new(),
body: None,
}));
}
fn on_event(&mut self, event: Event) {
let Event::Http((id, result)) = event else {
return;
};
if Some(id) != self.request {
return;
}
match result {
Ok(response) => self.load(&response.body),
Err(error) => host::log(LogLevel::Error, &format!("Request failed: {error}")),
}
}

Non-2xx responses are still Ok. Check response.status. Only use trusted, well-known services; see the plugin rules.

actions::request_discord calls Discord’s REST API through the client, with the user’s session. Pass the path without the version prefix. The result arrives as event.discord.

let id = actions::request_discord(&DiscordRequest {
method: "GET".into(),
path: "/users/@me/invites".into(),
body: None,
});
Function Delivers
host::set_timeout(ms) event.timer(id) once
host::set_interval(ms) event.timer(id) repeatedly
host::clear_timer(id) Nothing; cancels the timer

Timers are cancelled when the plugin stops.