pub struct Context {
pub data: Arc<RwLock<TypeMap>>,
pub shard: ShardMessenger,
pub shard_id: u64,
pub http: Arc<Http>,
pub cache: Arc<Cache>,
}
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: u64
The ID of the shard this context is related to.
http: Arc<Http>
cache: Arc<Cache>
cache
only.Implementations
sourceimpl Context
impl Context
sourcepub async fn online(&self)
Available on crate feature gateway
only.
pub async fn online(&self)
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:
struct Handler;
#[serenity::async_trait]
impl EventHandler for Handler {
async fn message(&self, ctx: Context, msg: Message) {
if msg.content == "!online" {
ctx.online().await;
}
}
}
let mut client =
Client::builder("token", GatewayIntents::default()).event_handler(Handler).await?;
client.start().await?;
sourcepub async fn idle(&self)
Available on crate feature gateway
only.
pub async fn idle(&self)
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:
struct Handler;
#[serenity::async_trait]
impl EventHandler for Handler {
async fn message(&self, ctx: Context, msg: Message) {
if msg.content == "!idle" {
ctx.idle().await;
}
}
}
let mut client =
Client::builder("token", GatewayIntents::default()).event_handler(Handler).await?;
client.start().await?;
sourcepub async fn dnd(&self)
Available on crate feature gateway
only.
pub async fn dnd(&self)
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:
struct Handler;
#[serenity::async_trait]
impl EventHandler for Handler {
async fn message(&self, ctx: Context, msg: Message) {
if msg.content == "!dnd" {
ctx.dnd().await;
}
}
}
let mut client =
Client::builder("token", GatewayIntents::default()).event_handler(Handler).await?;
client.start().await?;
sourcepub async fn invisible(&self)
Available on crate feature gateway
only.
pub async fn invisible(&self)
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 when an
Event::Ready
is received:
struct Handler;
#[serenity::async_trait]
impl EventHandler for Handler {
async fn ready(&self, ctx: Context, _: Ready) {
ctx.invisible().await;
}
}
let mut client =
Client::builder("token", GatewayIntents::default()).event_handler(Handler).await?;
client.start().await?;
sourcepub async fn reset_presence(&self)
Available on crate feature gateway
only.
pub async fn reset_presence(&self)
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 presence when an Event::Resumed
is received:
struct Handler;
#[serenity::async_trait]
impl EventHandler for Handler {
async fn resume(&self, ctx: Context, _: ResumedEvent) {
ctx.reset_presence().await;
}
}
let mut client =
Client::builder("token", GatewayIntents::default()).event_handler(Handler).await?;
client.start().await?;
sourcepub async fn set_activity(&self, activity: Activity)
Available on crate feature gateway
only.
pub async fn set_activity(&self, activity: Activity)
gateway
only.Sets the current activity, defaulting to an online status of Online
.
Examples
Create a command named ~setgame
that accepts a name of a game to be
playing:
use serenity::model::gateway::Activity;
struct Handler;
#[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(Activity::playing(game)).await;
}
}
}
let mut client =
Client::builder("token", GatewayIntents::default()).event_handler(Handler).await?;
client.start().await?;
sourcepub async fn set_presence(&self, activity: Option<Activity>, status: OnlineStatus)
Available on crate feature gateway
only.
pub async fn set_presence(&self, activity: Option<Activity>, status: OnlineStatus)
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
:
struct Handler;
#[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);
}
}
let mut client =
Client::builder("token", GatewayIntents::default()).event_handler(Handler).await?;
client.start().await?;
Setting the current user as playing "Heroes of the Storm"
, while being
DoNotDisturb
:
struct Handler;
#[serenity::async_trait]
impl EventHandler for Handler {
async fn ready(&self, context: Context, _: Ready) {
use serenity::model::gateway::Activity;
use serenity::model::user::OnlineStatus;
let activity = Activity::playing("Heroes of the Storm");
let status = OnlineStatus::DoNotDisturb;
context.set_presence(Some(activity), status);
}
}
let mut client =
Client::builder("token", GatewayIntents::default()).event_handler(Handler).await?;
client.start().await?;
sourcepub async fn set_message_filter(&self, filter: MessageFilter)
Available on crate feature collector
only.
pub async fn set_message_filter(&self, filter: MessageFilter)
collector
only.Sets a new filter
for the shard to check if a message event shall be
sent back to filter
’s paired receiver.
sourcepub async fn set_reaction_filter(&self, filter: ReactionFilter)
Available on crate feature collector
only.
pub async fn set_reaction_filter(&self, filter: ReactionFilter)
collector
only.Sets a new filter
for the shard to check if a reaction event shall be
sent back to filter
’s paired receiver.
sourcepub async fn set_component_interaction_filter(
&self,
filter: ComponentInteractionFilter
)
Available on crate feature collector
only.
pub async fn set_component_interaction_filter(
&self,
filter: ComponentInteractionFilter
)
collector
only.Sets a new filter
for the shard to check if an interaction event shall be
sent back to filter
’s paired receiver.
Trait Implementations
sourceimpl AsRef<ShardMessenger> for Context
Available on crate feature gateway
only.
impl AsRef<ShardMessenger> for Context
gateway
only.sourcefn as_ref(&self) -> &ShardMessenger
fn as_ref(&self) -> &ShardMessenger
Converts this type into a shared reference of the (usually inferred) input type.
Auto Trait Implementations
impl !RefUnwindSafe for Context
impl Send for Context
impl Sync for Context
impl Unpin for Context
impl !UnwindSafe for Context
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
fn vzip(self) -> V
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
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
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more