Struct serenity::client::Context
[−]
[src]
pub struct Context { pub data: Arc<Mutex<ShareMap>>, pub shard: Arc<Mutex<Shard>>, }
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 set_game
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<Mutex<ShareMap>>
A clone of Client::data
. Refer to its documentation for more
information.
The associated shard which dispatched the event handler.
Note that if you are sharding, in relevant terms, this is the shard which received the event being dispatched.
Methods
impl Context
[src]
fn edit_profile<F: FnOnce(EditProfile) -> EditProfile>(
&self,
f: F
) -> Result<CurrentUser>
&self,
f: F
) -> Result<CurrentUser>
Edits the current user's profile settings.
Refer to EditProfile
's documentation for its methods.
Examples
Change the current user's username:
ctx.edit_profile(|p| p.username("Hakase"));
fn online(&self)
Sets the current user as being Online
. This maintains the current
game.
Examples
Set the current user to being online on the shard:
client.on_message(|ctx, msg| { if msg.content == "!online" { ctx.online(); } });
fn idle(&self)
Sets the current user as being Idle
. This maintains the current
game.
Examples
Set the current user to being idle on the shard:
client.on_message(|ctx, msg| { if msg.content == "!idle" { ctx.idle(); } });
fn dnd(&self)
Sets the current user as being DoNotDisturb
. This maintains the
current game.
Examples
Set the current user to being Do Not Disturb on the shard:
client.on_message(|ctx, msg| { if msg.content == "!dnd" { ctx.dnd(); } });
fn invisible(&self)
Sets the current user as being Invisible
. This maintains the current
game.
Examples
Set the current user to being invisible on the shard when an
Event::Ready
is received:
client.on_ready(|ctx, _| { ctx.invisible(); });
fn reset_presence(&self)
"Resets" the current user's presence, by setting the game to None
and
the online status to Online
.
Use set_presence
for fine-grained control over individual details.
Examples
Reset the presence when an Event::Resumed
is received:
client.on_resume(|ctx, _| { ctx.reset_presence(); });
fn set_game(&self, game: Game)
Sets the current game, 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::Game; client.on_message(|ctx, msg| { let args = msg.content.splitn(2, ' ').collect::<Vec<&str>>(); if args.len() < 2 || *unsafe { args.get_unchecked(0) } != "~setgame" { return; } ctx.set_game(Game::playing(*unsafe { args.get_unchecked(1) })); });
fn set_game_name(&self, game_name: &str)
Sets the current game, passing in only its name. This will automatically
set the current user's OnlineStatus
to Online
, and its
GameType
as Playing
.
Use reset_presence
to clear the current game, or set_presence
for more fine-grained control.
Note: Maximum length is 128.
Examples
When an Event::Ready
is received, set the game name to "test"
:
client.on_ready(|ctx, _| { ctx.set_game_name("test"); });
fn set_presence(&self, game: Option<Game>, status: OnlineStatus, afk: bool)
Sets the current user's presence, providing all fields to be passed.
Examples
Setting the current user as having no game and being Idle
:
use serenity::model::OnlineStatus; ctx.set_presence(None, OnlineStatus::Idle, false);
Setting the current user as playing "Heroes of the Storm"
, while being
DoNotDisturb
:
use serenity::model::{Game, OnlineStatus}; let game = Game::playing("Heroes of the Storm"); let status = OnlineStatus::DoNotDisturb; context.set_presence(Some(game), status, false);
Trait Implementations
impl Clone for Context
[src]
fn clone(&self) -> Context
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0
Performs copy-assignment from source
. Read more