pub struct Context {
pub http: Arc<HttpClient>,
pub cache: Arc<InMemoryCache>,
pub shard: Arc<Shard>,
pub interaction: Option<Arc<Interaction<'static>>>,
/* private fields */
}Expand description
Context for Discord interaction handling.
This struct is passed to event handlers and provides access to:
- The HTTP client for making API requests
- The cache for looking up cached entities
- The shard that received the event
- The interaction data (for slash commands, buttons, etc.)
§Thread Safety
Context is Clone and can be safely shared between tasks. The response
tracking uses AtomicBool to avoid holding locks during async operations.
§Response Flow
Discord interactions must receive a response within 3 seconds. If your
operation takes longer, use Context::defer first, then Context::reply.
The context automatically tracks this and uses the correct API endpoint.
Fields§
§http: Arc<HttpClient>HTTP client for making Discord API requests.
cache: Arc<InMemoryCache>In-memory cache for guilds, channels, users, etc.
shard: Arc<Shard>The shard that received this event.
interaction: Option<Arc<Interaction<'static>>>The interaction data (if this context is for an interaction).
Implementations§
Source§impl Context
impl Context
pub fn new( http: Arc<HttpClient>, cache: Arc<InMemoryCache>, shard: Arc<Shard>, interaction: Option<Interaction<'static>>, ) -> Self
Sourcepub async fn defer(&self, ephemeral: bool) -> Result<(), TitaniumError>
pub async fn defer(&self, ephemeral: bool) -> Result<(), TitaniumError>
Defer the interaction response.
This sends a “Thinking…” state to Discord, giving you up to 15 minutes to send the actual response. You must call this within 3 seconds if your operation takes longer.
§Arguments
ephemeral- If true, the “Thinking…” and subsequent reply will only be visible to the user who invoked the command.
§Example
// Defer for a long operation
ctx.defer(false).await?;
// Do expensive work...
tokio::time::sleep(std::time::Duration::from_secs(5)).await;
// Reply (automatically edits the deferred message)
ctx.reply("Done!").await?;Sourcepub async fn reply(
&self,
content: impl Into<String>,
) -> Result<Message<'static>, TitaniumError>
pub async fn reply( &self, content: impl Into<String>, ) -> Result<Message<'static>, TitaniumError>
Reply to the command.
This smarter method checks if we have deferred.
If NOT deferred -> calls create_interaction_response
If DEFERRED -> calls edit_original_interaction_response
This solves the “3 second rule” complexity for the user! Reply to the interaction or message.
§Errors
Returns TitaniumError if the HTTP request fails.
Sourcepub async fn reply_embed(
&self,
embed: impl Into<Embed<'static>>,
) -> Result<Message<'static>, TitaniumError>
pub async fn reply_embed( &self, embed: impl Into<Embed<'static>>, ) -> Result<Message<'static>, TitaniumError>
Reply with an embed.
Works like Context::reply but sends an embed instead of text.
Automatically handles deferred interactions.
§Example
let embed = EmbedBuilder::new()
.title("Hello!")
.description("This is an embed")
.color(0x5865F2)
.build();
ctx.reply_embed(embed).await?;Reply with an embed.
§Errors
Returns TitaniumError if the HTTP request fails.
Sourcepub async fn reply_ephemeral(
&self,
content: impl Into<String>,
) -> Result<(), TitaniumError>
pub async fn reply_ephemeral( &self, content: impl Into<String>, ) -> Result<(), TitaniumError>
Reply with an ephemeral message (only visible to user). Reply with an ephemeral message (only visible to user).
§Errors
Returns TitaniumError if the HTTP request fails.
Sourcepub async fn edit_reply(
&self,
content: impl Into<String>,
) -> Result<Message<'static>, TitaniumError>
pub async fn edit_reply( &self, content: impl Into<String>, ) -> Result<Message<'static>, TitaniumError>
Edit the original interaction response.
Sourcepub async fn followup(
&self,
content: impl Into<String>,
) -> Result<Message<'static>, TitaniumError>
pub async fn followup( &self, content: impl Into<String>, ) -> Result<Message<'static>, TitaniumError>
Send a follow-up message.
Sourcepub fn channel_id(&self) -> Option<Snowflake>
pub fn channel_id(&self) -> Option<Snowflake>
Get the channel ID.
Sourcepub async fn success(
&self,
title: impl Into<String>,
description: impl Into<String>,
) -> Result<Message<'static>, TitaniumError>
pub async fn success( &self, title: impl Into<String>, description: impl Into<String>, ) -> Result<Message<'static>, TitaniumError>
Reply with a success embed (green).
Sourcepub async fn error(
&self,
title: impl Into<String>,
description: impl Into<String>,
) -> Result<Message<'static>, TitaniumError>
pub async fn error( &self, title: impl Into<String>, description: impl Into<String>, ) -> Result<Message<'static>, TitaniumError>
Reply with an error embed (red).
Sourcepub async fn info(
&self,
title: impl Into<String>,
description: impl Into<String>,
) -> Result<Message<'static>, TitaniumError>
pub async fn info( &self, title: impl Into<String>, description: impl Into<String>, ) -> Result<Message<'static>, TitaniumError>
Reply with an info embed (blurple).
Sourcepub async fn warning(
&self,
title: impl Into<String>,
description: impl Into<String>,
) -> Result<Message<'static>, TitaniumError>
pub async fn warning( &self, title: impl Into<String>, description: impl Into<String>, ) -> Result<Message<'static>, TitaniumError>
Reply with a warning embed (yellow).