[−][src]Struct serenity::prelude::Context
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 messenger to communicate with the shard runner.
The ID of the shard this context is related to.
Methods
impl Context
[src]
impl Context
pub fn edit_profile<F: FnOnce(EditProfile) -> EditProfile>(
&self,
f: F
) -> Result<CurrentUser>
[src]
pub fn edit_profile<F: FnOnce(EditProfile) -> EditProfile>(
&self,
f: F
) -> Result<CurrentUser>
: Use the http module instead.
Edits the current user's profile settings.
Refer to EditProfile
's documentation for its methods.
Examples
Change the current user's username:
struct Handler; impl EventHandler for Handler { fn message(&self, ctx: Context, msg: Message) { if msg.content == "!changename" { ctx.edit_profile(|e| e.username("Edward Elric")); } } } let mut client = Client::new("token", Handler).unwrap(); client.start().unwrap();
pub fn online(&self)
[src]
pub 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:
struct Handler; impl EventHandler for Handler { fn message(&self, ctx: Context, msg: Message) { if msg.content == "!online" { ctx.online(); } } } let mut client = Client::new("token", Handler).unwrap(); client.start().unwrap();
pub fn idle(&self)
[src]
pub 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:
struct Handler; impl EventHandler for Handler { fn message(&self, ctx: Context, msg: Message) { if msg.content == "!idle" { ctx.idle(); } } } let mut client = Client::new("token", Handler).unwrap(); client.start().unwrap();
pub fn dnd(&self)
[src]
pub 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:
struct Handler; impl EventHandler for Handler { fn message(&self, ctx: Context, msg: Message) { if msg.content == "!dnd" { ctx.dnd(); } } } let mut client = Client::new("token", Handler).unwrap(); client.start().unwrap();
pub fn invisible(&self)
[src]
pub 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:
struct Handler; impl EventHandler for Handler { fn ready(&self, ctx: Context, _: Ready) { ctx.invisible(); } } let mut client = Client::new("token", Handler).unwrap(); client.start().unwrap();
pub fn reset_presence(&self)
[src]
pub 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:
struct Handler; impl EventHandler for Handler { fn resume(&self, ctx: Context, _: ResumedEvent) { ctx.reset_presence(); } } let mut client = Client::new("token", Handler).unwrap(); client.start().unwrap();
pub fn set_game<T: Into<Game>>(&self, game: T)
[src]
pub fn set_game<T: Into<Game>>(&self, game: T)
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::gateway::Game; struct Handler; impl EventHandler for Handler { fn message(&self, ctx: Context, msg: Message) { 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) })); } } let mut client = Client::new("token", Handler).unwrap(); client.start().unwrap();
pub fn set_game_name<T: Into<String>>(&self, game_name: T)
[src]
pub fn set_game_name<T: Into<String>>(&self, game_name: T)
: Use Context::set_game
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"
:
struct Handler; impl EventHandler for Handler { fn ready(&self, ctx: Context, _: Ready) { ctx.set_game_name("test"); } } let mut client = Client::new("token", Handler).unwrap(); client.start().unwrap();
pub fn set_presence(&self, game: Option<Game>, status: OnlineStatus)
[src]
pub fn set_presence(&self, game: Option<Game>, status: OnlineStatus)
Sets the current user's presence, providing all fields to be passed.
Examples
Setting the current user as having no game and being Idle
:
struct Handler; impl EventHandler for Handler { fn ready(&self, ctx: Context, _: Ready) { use serenity::model::user::OnlineStatus; ctx.set_presence(None, OnlineStatus::Idle); } } let mut client = Client::new("token", Handler).unwrap(); client.start().unwrap();
Setting the current user as playing "Heroes of the Storm"
, while being
DoNotDisturb
:
struct Handler; impl EventHandler for Handler { fn ready(&self, context: Context, _: Ready) { use serenity::model::gateway::Game; use serenity::model::user::OnlineStatus; let game = Game::playing("Heroes of the Storm"); let status = OnlineStatus::DoNotDisturb; context.set_presence(Some(game), status); } } let mut client = Client::new("token", Handler).unwrap(); client.start().unwrap();
pub fn quit(&self)
[src]
pub fn quit(&self)
Disconnects the shard from the websocket, essentially "quiting" it.
Note however that this will only exit the one which the Context
was given.
If it's just one shard that's on, then serenity will stop any further actions
until Client::start
and vice versa are called again.
Trait Implementations
Auto Trait Implementations
Blanket Implementations
impl<T, U> Into for T where
U: From<T>,
[src]
impl<T, U> Into for T where
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
impl<T> ToOwned for T where
T: Clone,
type Owned = T
fn to_owned(&self) -> T
[src]
fn to_owned(&self) -> T
Creates owned data from borrowed data, usually by cloning. Read more
fn clone_into(&self, target: &mut T)
[src]
fn clone_into(&self, target: &mut T)
🔬 This is a nightly-only experimental API. (toowned_clone_into
)
recently added
Uses borrowed data to replace owned data, usually by cloning. Read more
impl<T> From for T
[src]
impl<T> From for T
impl<T, U> TryFrom for T where
T: From<U>,
[src]
impl<T, U> TryFrom for T where
T: From<U>,
type Error = !
try_from
)The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
try_from
)Performs the conversion.
impl<T> Borrow for T where
T: ?Sized,
[src]
impl<T> Borrow for T where
T: ?Sized,
impl<T> BorrowMut for T where
T: ?Sized,
[src]
impl<T> BorrowMut for T where
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<T, U> TryInto for T where
U: TryFrom<T>,
[src]
impl<T, U> TryInto for T where
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
try_from
)The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
try_from
)Performs the conversion.
impl<T> Any for T where
T: 'static + ?Sized,
[src]
impl<T> Any for T where
T: 'static + ?Sized,
fn get_type_id(&self) -> TypeId
[src]
fn get_type_id(&self) -> TypeId
🔬 This is a nightly-only experimental API. (get_type_id
)
this method will likely be replaced by an associated static
Gets the TypeId
of self
. Read more
impl<T> Erased for T
impl<T> Erased for T
impl<T> Typeable for T where
T: Any,
impl<T> Typeable for T where
T: Any,
impl<T> CloneAny for T where
T: Clone + Any,
[src]
impl<T> CloneAny for T where
T: Clone + Any,
fn clone_any(&self) -> Box<dyn CloneAny + 'static>
[src]
fn clone_any(&self) -> Box<dyn CloneAny + 'static>
fn clone_any_send(&self) -> Box<dyn CloneAny + 'static + Send> where
T: Send,
[src]
fn clone_any_send(&self) -> Box<dyn CloneAny + 'static + Send> where
T: Send,
fn clone_any_sync(&self) -> Box<dyn CloneAny + 'static + Sync> where
T: Sync,
[src]
fn clone_any_sync(&self) -> Box<dyn CloneAny + 'static + Sync> where
T: Sync,
fn clone_any_send_sync(&self) -> Box<dyn CloneAny + 'static + Sync + Send> where
T: Send + Sync,
[src]
fn clone_any_send_sync(&self) -> Box<dyn CloneAny + 'static + Sync + Send> where
T: Send + Sync,
impl<T> UnsafeAny for T where
T: Any,
impl<T> UnsafeAny for T where
T: Any,