Struct serenity::client::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, which 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);
    }
}

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 copy 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> 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

§

type Output = T

Should always be Self
source§

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

§

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>,

§

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>,

§

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

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

§

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,