pub struct Context { pub data:Arc<RwLock<TypeMap>>, pub shard:ShardMessenger, pub shard_id:ShardId, pub http:Arc<Http>, pub cache:Arc<Cache>,}
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 contextalso acts as a general high-level interface over the associatedShard
which receivedthe event, or the low-levelhttp
module.
The context contains “shortcuts”, like for interacting with the shard. Methods likeSelf::set_activity
will unlock the shard and perform an update for you to save a bit ofwork.
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 ofClient::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>
cache
only.Implementations§
Source§implContext
implContext
Sourcepub fndnd(&self)
Available oncrate featuregateway
only.
pub fndnd(&self)
gateway
only.Sets the current user as beingDoNotDisturb
. This maintains the current activity.
§Examples
Set the current user to being Do Not Disturb on the shard:
#[serenity::async_trait]implEventHandlerforHandler {async fnmessage(&self, ctx: Context, msg: Message) {ifmsg.content =="!dnd"{ ctx.dnd(); } }}
Sourcepub fnreset_presence(&self)
Available oncrate featuregateway
only.
pub fnreset_presence(&self)
gateway
only.“Resets” the current user’s presence, by setting the activity toNone
and the onlinestatus toOnline
.
UseSelf::set_presence
for fine-grained control over individual details.
§Examples
Reset the current user’s presence on the shard:
#[serenity::async_trait]implEventHandlerforHandler {async fnmessage(&self, ctx: Context, msg: Message) {ifmsg.content =="!reset_presence"{ ctx.reset_presence(); } }}
Sourcepub fnset_activity(&self, activity:Option<ActivityData>)
Available oncrate featuregateway
only.
pub fnset_activity(&self, activity:Option<ActivityData>)
gateway
only.Sets the current activity.
§Examples
Create a command named~setgame
that accepts a name of a game to be playing:
useserenity::gateway::ActivityData;#[serenity::async_trait]implEventHandlerforHandler {async fnmessage(&self, ctx: Context, msg: Message) {letmutargs = msg.content.splitn(2,' ');if let(Some("~setgame"),Some(game)) = (args.next(), args.next()) { ctx.set_activity(Some(ActivityData::playing(game))); } }}
Sourcepub fnset_presence(&self, activity:Option<ActivityData>, status:OnlineStatus)
Available oncrate featuregateway
only.
pub fnset_presence(&self, activity:Option<ActivityData>, 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 beingIdle
:
#[serenity::async_trait]implEventHandlerforHandler {async fnready(&self, ctx: Context,_: Ready) {useserenity::model::user::OnlineStatus; ctx.set_presence(None, OnlineStatus::Idle); }}
Setting the current user as playing"Heroes of the Storm"
, while beingDoNotDisturb
:
#[serenity::async_trait]implEventHandlerforHandler {async fnready(&self, context: Context,_: Ready) {useserenity::gateway::ActivityData;useserenity::model::user::OnlineStatus;letactivity = ActivityData::playing("Heroes of the Storm");letstatus = OnlineStatus::DoNotDisturb; context.set_presence(Some(activity), status); }}
Sourcepub async fnget_application_emojis(&self) ->Result<Vec<Emoji>>
pub async fnget_application_emojis(&self) ->Result<Vec<Emoji>>
Gets all emojis for the current application.
§Errors
Returns an error if the Application ID is not known.
Sourcepub async fnget_application_emoji(&self, emoji_id:EmojiId) ->Result<Emoji>
pub async fnget_application_emoji(&self, emoji_id:EmojiId) ->Result<Emoji>
Sourcepub async fncreate_application_emoji( &self, name: &str, image: &str,) ->Result<Emoji>
pub async fncreate_application_emoji( &self, name: &str, image: &str,) ->Result<Emoji>
Creates an application emoji with a name and base64-encoded image.
§Errors
SeeGuild::create_emoji
for information about name and filesize requirements. Thismethod will error if said requirements are not met.
Trait Implementations§
Source§implAsRef<ShardMessenger> forContext
Available oncrate featuregateway
only.
implAsRef<ShardMessenger> forContext
gateway
only.