Context

Struct Context 

Source
pub struct Context {
    pub data: Arc<RwLock<TypeMap>>,
    pub shard: ShardMessenger,
    pub shard_id: ShardId,
    pub http: Arc<Http>,
    pub cache: Arc<Cache>,
}
Available on crate feature client only.
Expand description

The context is a general utility struct provided on event dispatches.

The Context helps with dealing with the current “context” of the event dispatch. The context also acts as a general high-level interface over the associated Shard which received the event, or the low-level http module.

The context contains “shortcuts”, like for interacting with the shard. Methods like Self::set_activity will unlock the shard and perform an update for you to save a bit of work.

A context will only live for the event it was dispatched for. After the event handler finished, it is destroyed and will not be re-used.

Fields§

§data: Arc<RwLock<TypeMap>>

A clone of Client::data. Refer to its documentation for more information.

§shard: ShardMessenger

The messenger to communicate with the shard runner.

§shard_id: ShardId

The ID of the shard this context is related to.

§http: Arc<Http>§cache: Arc<Cache>
Available on crate feature cache only.

Implementations§

Source§

impl Context

Source

pub fn online(&self)

Available on crate feature gateway only.

Sets the current user as being Online. This maintains the current activity.

§Examples

Set the current user to being online on the shard:


#[serenity::async_trait]
impl EventHandler for Handler {
    async fn message(&self, ctx: Context, msg: Message) {
        if msg.content == "!online" {
            ctx.online();
        }
    }
}
Source

pub fn idle(&self)

Available on crate feature gateway only.

Sets the current user as being Idle. This maintains the current activity.

§Examples

Set the current user to being idle on the shard:


#[serenity::async_trait]
impl EventHandler for Handler {
    async fn message(&self, ctx: Context, msg: Message) {
        if msg.content == "!idle" {
            ctx.idle();
        }
    }
}
Source

pub fn dnd(&self)

Available on crate feature gateway only.

Sets the current user as being DoNotDisturb. This maintains the current activity.

§Examples

Set the current user to being Do Not Disturb on the shard:


#[serenity::async_trait]
impl EventHandler for Handler {
    async fn message(&self, ctx: Context, msg: Message) {
        if msg.content == "!dnd" {
            ctx.dnd();
        }
    }
}
Source

pub fn invisible(&self)

Available on crate feature gateway only.

Sets the current user as being Invisible. This maintains the current activity.

§Examples

Set the current user to being invisible on the shard:


#[serenity::async_trait]
impl EventHandler for Handler {
    async fn message(&self, ctx: Context, msg: Message) {
        if msg.content == "!invisible" {
            ctx.invisible();
        }
    }
}
Source

pub fn reset_presence(&self)

Available on crate feature gateway only.

“Resets” the current user’s presence, by setting the activity to None and the online status to Online.

Use Self::set_presence for fine-grained control over individual details.

§Examples

Reset the current user’s presence on the shard:


#[serenity::async_trait]
impl EventHandler for Handler {
    async fn message(&self, ctx: Context, msg: Message) {
        if msg.content == "!reset_presence" {
            ctx.reset_presence();
        }
    }
}
Source

pub fn set_activity(&self, activity: Option<ActivityData>)

Available on crate feature gateway only.

Sets the current activity.

§Examples

Create a command named ~setgame that accepts a name of a game to be playing:


use serenity::gateway::ActivityData;

#[serenity::async_trait]
impl EventHandler for Handler {
    async fn message(&self, ctx: Context, msg: Message) {
        let mut args = msg.content.splitn(2, ' ');

        if let (Some("~setgame"), Some(game)) = (args.next(), args.next()) {
            ctx.set_activity(Some(ActivityData::playing(game)));
        }
    }
}
Source

pub fn set_presence(&self, activity: Option<ActivityData>, status: OnlineStatus)

Available on crate feature gateway only.

Sets the current user’s presence, providing all fields to be passed.

§Examples

Setting the current user as having no activity and being Idle:


#[serenity::async_trait]
impl EventHandler for Handler {
    async fn ready(&self, ctx: Context, _: Ready) {
        use serenity::model::user::OnlineStatus;

        ctx.set_presence(None, OnlineStatus::Idle);
    }
}

Setting the current user as playing "Heroes of the Storm", while being DoNotDisturb:


#[serenity::async_trait]
impl EventHandler for Handler {
    async fn ready(&self, context: Context, _: Ready) {
        use serenity::gateway::ActivityData;
        use serenity::model::user::OnlineStatus;

        let activity = ActivityData::playing("Heroes of the Storm");
        let status = OnlineStatus::DoNotDisturb;

        context.set_presence(Some(activity), status);
    }
}
Source

pub async fn get_application_emojis(&self) -> Result<Vec<Emoji>>

Gets all emojis for the current application.

§Errors

Returns an error if the Application ID is not known.

Source

pub async fn get_application_emoji(&self, emoji_id: EmojiId) -> Result<Emoji>

Gets information about an application emoji.

§Errors

Returns an error if the emoji does not exist.

Source

pub async fn create_application_emoji( &self, name: &str, image: &str, ) -> Result<Emoji>

Creates an application emoji with a name and base64-encoded image.

§Errors

See Guild::create_emoji for information about name and filesize requirements. This method will error if said requirements are not met.

Source

pub async fn edit_application_emoji( &self, emoji_id: EmojiId, name: &str, ) -> Result<Emoji>

Changes the name of an application emoji.

§Errors

Returns an error if the emoji does not exist.

Source

pub async fn delete_application_emoji(&self, emoji_id: EmojiId) -> Result<()>

Deletes an application emoji.

§Errors

Returns an error if the emoji does not exist.

Trait Implementations§

Source§

impl AsRef<Arc<Cache>> for Context

Available on crate feature cache only.
Source§

fn as_ref(&self) -> &Arc<Cache>

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl AsRef<Arc<Http>> for Context

Source§

fn as_ref(&self) -> &Arc<Http>

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl AsRef<Cache> for Context

Available on crate feature cache only.
Source§

fn as_ref(&self) -> &Cache

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl AsRef<Http> for Context

Source§

fn as_ref(&self) -> &Http

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl AsRef<ShardMessenger> for Context

Available on crate feature gateway only.
Source§

fn as_ref(&self) -> &ShardMessenger

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl CacheHttp for Context

Available on crate feature http only.
Source§

fn http(&self) -> &Http

Source§

fn cache(&self) -> Option<&Arc<Cache>>

Available on crate feature cache only.
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
Source§

impl Debug for Context

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. 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> CloneDebuggableStorage for T

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> CloneableStorage for T
where T: Any + Send + Sync + Clone,

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> 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
Source§

impl<T> DebuggableStorage for T
where T: Any + Send + Sync + Debug,

Source§

impl<T> ErasedDestructor for T
where T: 'static,