Slash Commands¶
RustyCord registers application commands through Discord's HTTP API and dispatches INTERACTION_CREATE gateway events to a SlashCommandRegistry.
Define a command¶
use async_trait::async_trait;
use rustycord::interactions::{
Interaction, SlashCommand, SlashCommandOption, SlashCommandResult,
ApplicationCommandOptionType,
};
struct DeployCommand;
#[async_trait]
impl SlashCommand for DeployCommand {
fn name(&self) -> &str {
"deploy"
}
fn description(&self) -> &str {
"Deploy a service"
}
fn options(&self) -> Vec<SlashCommandOption> {
vec![
SlashCommandOption::new(
"service",
"Service to deploy",
ApplicationCommandOptionType::String,
)
.required(true),
SlashCommandOption::new(
"confirm",
"Confirm the deployment",
ApplicationCommandOptionType::Boolean,
)
.required(true),
]
}
async fn execute(
&self,
interaction: &Interaction,
_client: &rustycord::client::Client,
) -> SlashCommandResult {
let service = interaction
.option("service")
.and_then(|option| option.value.as_ref())
.and_then(|value| value.as_str())
.unwrap_or("unknown");
Ok(Some(format!("Deployment queued for `{}`", service)))
}
}
Returning Some(String) sends a channel interaction response. Returning Err reports the error to the registry, and returning Ok(None) is useful if a command responds manually through Client::respond_to_interaction.
Register and handle commands¶
```rust,no_run use rustycord::{client::Client, gateway::intents}; use std::sync::Arc;
async fn deploy_commands() { let mut client = Client::new(); let token = std::env::var("DISCORD_TOKEN").expect("DISCORD_TOKEN must be set"); client.login(token).await;
let registry = client.get_slash_command_registry();
registry.register(DeployCommand).await;
if let Some(application_id) = client.application_id() {
// Guild-scoped commands update rapidly and are ideal while testing.
registry
.register_guild(&client, &application_id, "YOUR_GUILD_ID")
.await
.expect("slash commands must register");
// Global commands are available everywhere but may take time to propagate.
registry
.register_global(&client, &application_id)
.await
.expect("global commands must register");
}
// Keep the gateway open so interaction events are dispatched.
client.initialize_shards(1).await;
client
.start_shards(Some(intents::ALL_INTENTS), None)
.await;
client.wait_for_shutdown().await;
}
``register_globalandregister_guilduse Discord's overwrite endpoint (PUT /applications/{application.id}/commands`), so the registry's full definition list becomes the desired final command list.
Interactions dispatched by the gateway¶
Client contains one shared registry. Register commands before starting the gateway. When Discord sends INTERACTION_CREATE, RustyCord parses it, finds the command by name, executes it, and sends the returned text through POST /interactions/{id}/{token}/callback.
Supported option types include:
- SubCommand and SubCommandGroup
- String, Integer, and Number
- Boolean
- User, Channel, Role, and Mentionable
Command receivers expose Interaction::option("name"), which returns the raw option data. IDs for user/channel/role-based options are provided by Discord in the interaction payload.
Respond manually¶
Commands that need advanced response data can call the callback directly:
rust
use serde_json::json;
client.respond_to_interaction(
&interaction.id,
&interaction.token,
json!({
"type": 4,
"data": {
"content": "Deployment started",
"flags": 64
}
}),
)
.await?;
flags: 64 marks the response as ephemeral.
Warning
Slash-command tokens expire quickly. Handle the interaction promptly, and register the bot with the bot and applications.commands OAuth2 scopes in the Discord Developer Portal.