#[cfg(feature = "gateway")]
use crate::client::bridge::gateway::ShardMessenger;
#[cfg(feature = "gateway")]
use crate::gateway::InterMessage;
use crate::model::prelude::*;
use std::sync::Arc;
use tokio::sync::RwLock;
use futures::channel::mpsc::UnboundedSender as Sender;
use crate::http::Http;
use typemap_rev::TypeMap;
#[cfg(feature = "cache")]
pub use crate::cache::Cache;
#[cfg(feature = "collector")]
use crate::collector::{MessageFilter, ReactionFilter};
#[derive(Clone)]
pub struct Context {
pub data: Arc<RwLock<TypeMap>>,
pub shard: ShardMessenger,
pub shard_id: u64,
pub http: Arc<Http>,
#[cfg(feature = "cache")]
pub cache: Arc<Cache>,
}
impl Context {
#[cfg(all(feature = "cache", feature = "gateway"))]
pub(crate) fn new(
data: Arc<RwLock<TypeMap>>,
runner_tx: Sender<InterMessage>,
shard_id: u64,
http: Arc<Http>,
cache: Arc<Cache>,
) -> Context {
Context {
shard: ShardMessenger::new(runner_tx),
shard_id,
data,
http,
cache,
}
}
#[cfg(all(not(feature = "cache"), not(feature = "gateway")))]
pub fn easy(
data: Arc<RwLock<TypeMap>>,
shard_id: u64,
http: Arc<Http>,
) -> Context {
Context {
shard_id,
data,
http,
}
}
#[cfg(all(not(feature = "cache"), feature = "gateway"))]
pub(crate) fn new(
data: Arc<RwLock<TypeMap>>,
runner_tx: Sender<InterMessage>,
shard_id: u64,
http: Arc<Http>,
) -> Context {
Context {
shard: ShardMessenger::new(runner_tx),
shard_id,
data,
http,
}
}
#[cfg(feature = "gateway")]
#[inline]
pub async fn online(&self) {
self.shard.set_status(OnlineStatus::Online);
}
#[cfg(feature = "gateway")]
#[inline]
pub async fn idle(&self) {
self.shard.set_status(OnlineStatus::Idle);
}
#[cfg(feature = "gateway")]
#[inline]
pub async fn dnd(&self) {
self.shard.set_status(OnlineStatus::DoNotDisturb);
}
#[cfg(feature = "gateway")]
#[inline]
pub async fn invisible(&self) {
self.shard.set_status(OnlineStatus::Invisible);
}
#[cfg(feature = "gateway")]
#[inline]
pub async fn reset_presence(&self) {
self.shard.set_presence(None::<Activity>, OnlineStatus::Online);
}
#[cfg(feature = "gateway")]
#[inline]
pub async fn set_activity(&self, activity: Activity) {
self.shard.set_presence(Some(activity), OnlineStatus::Online);
}
#[cfg(feature = "gateway")]
#[inline]
pub async fn set_presence(&self, activity: Option<Activity>, status: OnlineStatus) {
self.shard.set_presence(activity, status);
}
#[inline]
#[cfg(feature = "collector")]
pub async fn set_message_filter(&self, filter: MessageFilter) {
self.shard.set_message_filter(filter);
}
#[inline]
#[cfg(feature = "collector")]
pub async fn set_reaction_filter(&self, filter: ReactionFilter) {
self.shard.set_reaction_filter(filter);
}
}
impl AsRef<Http> for Context {
fn as_ref(&self) -> &Http { &self.http }
}
impl AsRef<Http> for Arc<Context> {
fn as_ref(&self) -> &Http { &self.http }
}
impl AsRef<Arc<Http>> for Context {
fn as_ref(&self) -> &Arc<Http> { &self.http }
}
#[cfg(feature = "cache")]
impl AsRef<Cache> for Context {
fn as_ref(&self) -> &Cache {
&self.cache
}
}
#[cfg(feature = "cache")]
impl AsRef<Cache> for Arc<Context> {
fn as_ref(&self) -> &Cache {
&*self.cache
}
}
#[cfg(feature = "cache")]
impl AsRef<Arc<Cache>> for Context {
fn as_ref(&self) -> &Arc<Cache> {
&self.cache
}
}
#[cfg(feature = "cache")]
impl AsRef<Cache> for Cache {
fn as_ref(&self) -> &Cache {
&self
}
}
#[cfg(feature = "gateway")]
impl AsRef<ShardMessenger> for Context {
fn as_ref(&self) -> &ShardMessenger {
&self.shard
}
}