[][src]Struct serenity::client::Context

pub struct Context {
    pub data: Arc<RwLock<ShareMap>>,
    pub shard: ShardMessenger,
    pub shard_id: u64,
    pub cache: CacheRwLock,
    pub http: Arc<Http>,
}

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_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<ShareMap>>

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.

cache: CacheRwLockhttp: Arc<Http>

Methods

impl Context[src]

pub fn online(&self)[src]

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;

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]

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;

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]

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;

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]

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;

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]

"Resets" the current user's presence, by setting the activity 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_activity(&self, activity: Activity)[src]

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;

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_activity(Activity::playing(*unsafe { args.get_unchecked(1) }));
    }
}

let mut client = Client::new("token", Handler).unwrap();

client.start().unwrap();

pub fn set_game_name(&self, game_name: &str)[src]

Deprecated since 0.5.5:

Use Context::set_activity

Sets the current activity, passing in only its name. This will automatically set the current user's OnlineStatus to Online, and its ActivityType as Playing.

Use reset_presence to clear the current activity, or set_presence for more fine-grained control.

Note: Maximum length is 128.

Examples

When an Event::Ready is received, set the activity 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, activity: Option<Activity>, status: OnlineStatus)[src]

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;

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:

This example is not tested
struct Handler;

impl EventHandler for Handler {
    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::new("token", Handler).unwrap();

client.start().unwrap();

pub fn quit(&self)[src]

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

impl AsRef<Http> for Context[src]

impl AsRef<CacheRwLock> for Context[src]

impl Clone for Context[src]

fn clone_from(&mut self, source: &Self)
1.0.0
[src]

Performs copy-assignment from source. Read more

Auto Trait Implementations

impl Send for Context

impl !Sync for Context

Blanket Implementations

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T> From for T[src]

impl<T, U> Into for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> Borrow for T where
    T: ?Sized
[src]

impl<T> BorrowMut for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Same for T

type Output = T

Should always be Self

impl<T> Erased for T

impl<T, U> TryInto for T where
    U: TryFrom<T>, 

type Err = <U as TryFrom<T>>::Err

impl<T> CloneAny for T where
    T: Clone + Any
[src]

impl<T> UnsafeAny for T where
    T: Any