Context

Struct Context 

Source
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

Source

pub fn new( http: Arc<HttpClient>, cache: Arc<InMemoryCache>, shard: Arc<Shard>, interaction: Option<Interaction<'static>>, ) -> Self

Source

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?;
Source

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.

Source

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.

Source

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.

Source

pub async fn edit_reply( &self, content: impl Into<String>, ) -> Result<Message<'static>, TitaniumError>

Edit the original interaction response.

Source

pub async fn followup( &self, content: impl Into<String>, ) -> Result<Message<'static>, TitaniumError>

Send a follow-up message.

Source

pub fn user(&self) -> Option<&User<'static>>

Get the user who triggered the interaction.

Source

pub fn guild_id(&self) -> Option<Snowflake>

Get the guild ID if in a guild.

Source

pub fn channel_id(&self) -> Option<Snowflake>

Get the channel ID.

Source

pub async fn success( &self, title: impl Into<String>, description: impl Into<String>, ) -> Result<Message<'static>, TitaniumError>

Reply with a success embed (green).

Source

pub async fn error( &self, title: impl Into<String>, description: impl Into<String>, ) -> Result<Message<'static>, TitaniumError>

Reply with an error embed (red).

Source

pub async fn info( &self, title: impl Into<String>, description: impl Into<String>, ) -> Result<Message<'static>, TitaniumError>

Reply with an info embed (blurple).

Source

pub async fn warning( &self, title: impl Into<String>, description: impl Into<String>, ) -> Result<Message<'static>, TitaniumError>

Reply with a warning embed (yellow).

Source

pub async fn send( &self, channel_id: impl Into<Snowflake>, content: impl Into<String>, ) -> Result<Message<'static>, HttpError>

Send a message to a specific channel (bypass interaction).

Source

pub async fn send_embed( &self, channel_id: impl Into<Snowflake>, embed: impl Into<Embed<'static>>, ) -> Result<Message<'static>, HttpError>

Send an embed to a specific channel.

Trait Implementations§

Source§

impl Clone for Context

Source§

fn clone(&self) -> Context

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more