pub struct Context {
pub data: Arc<RwLock<TypeMap>>,
pub shard: ShardMessenger,
pub shard_id: ShardId,
pub http: Arc<Http>,
pub cache: Arc<Cache>,
}
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>
Implementations§
Source§impl Context
impl Context
Sourcepub fn dnd(&self)
pub fn dnd(&self)
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();
}
}
}
Sourcepub fn invisible(&self)
pub fn invisible(&self)
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();
}
}
}
Sourcepub fn reset_presence(&self)
pub fn reset_presence(&self)
“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();
}
}
}
Sourcepub fn set_activity(&self, activity: Option<ActivityData>)
pub fn set_activity(&self, activity: Option<ActivityData>)
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)));
}
}
}
Sourcepub fn set_presence(&self, activity: Option<ActivityData>, status: OnlineStatus)
pub fn set_presence(&self, activity: Option<ActivityData>, status: OnlineStatus)
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);
}
}
Sourcepub async fn get_application_emojis(&self) -> Result<Vec<Emoji>>
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.
Sourcepub async fn get_application_emoji(&self, emoji_id: EmojiId) -> Result<Emoji>
pub async fn get_application_emoji(&self, emoji_id: EmojiId) -> Result<Emoji>
Sourcepub async fn create_application_emoji(
&self,
name: &str,
image: &str,
) -> Result<Emoji>
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.