use client::bridge::gateway::ShardMessenger;
use gateway::InterMessage;
use model::prelude::*;
use parking_lot::Mutex;
use std::sync::{
mpsc::Sender,
Arc
};
use typemap::ShareMap;
#[cfg(feature = "builder")]
use builder::EditProfile;
#[cfg(feature = "builder")]
use internal::prelude::*;
#[cfg(all(feature = "builder", feature = "cache"))]
use super::CACHE;
#[cfg(feature = "builder")]
use {Result, http};
#[cfg(feature = "builder")]
use utils::{self, VecMap};
#[derive(Clone)]
pub struct Context {
pub data: Arc<Mutex<ShareMap>>,
pub shard: ShardMessenger,
pub shard_id: u64,
}
impl Context {
pub(crate) fn new(
data: Arc<Mutex<ShareMap>>,
runner_tx: Sender<InterMessage>,
shard_id: u64,
) -> Context {
Context {
shard: ShardMessenger::new(runner_tx),
shard_id,
data,
}
}
#[cfg(feature = "builder")]
#[deprecated(since = "0.5.6", note = "Use the http module instead.")]
pub fn edit_profile<F: FnOnce(EditProfile) -> EditProfile>(&self, f: F) -> Result<CurrentUser> {
let mut map = VecMap::with_capacity(2);
feature_cache! {
{
let cache = CACHE.read();
map.insert("username", Value::String(cache.user.name.clone()));
if let Some(email) = cache.user.email.clone() {
map.insert("email", Value::String(email));
}
} else {
let user = http::get_current_user()?;
map.insert("username", Value::String(user.name));
if let Some(email) = user.email {
map.insert("email", Value::String(email));
}
}
}
let edited = utils::vecmap_to_json_map(f(EditProfile(map)).0);
http::edit_profile(&edited)
}
#[inline]
pub fn online(&self) {
self.shard.set_status(OnlineStatus::Online);
}
#[inline]
pub fn idle(&self) {
self.shard.set_status(OnlineStatus::Idle);
}
#[inline]
pub fn dnd(&self) {
self.shard.set_status(OnlineStatus::DoNotDisturb);
}
#[inline]
pub fn invisible(&self) {
self.shard.set_status(OnlineStatus::Invisible);
}
#[inline]
pub fn reset_presence(&self) {
self.shard.set_presence(None::<Game>, OnlineStatus::Online);
}
#[inline]
pub fn set_game<T: Into<Game>>(&self, game: T) {
self._set_game(game.into())
}
fn _set_game(&self, game: Game) {
self.shard.set_presence(Some(game), OnlineStatus::Online);
}
#[deprecated(since = "0.5.5", note = "Use Context::set_game")]
#[inline]
pub fn set_game_name<T: Into<String>>(&self, game_name: T) {
self.set_game(game_name.into())
}
#[inline]
pub fn set_presence(&self, game: Option<Game>, status: OnlineStatus) {
self.shard.set_presence(game, status);
}
#[inline]
pub fn quit(&self) {
self.shard.shutdown_clean();
}
}