use std::fmt;
use std::sync::Arc;
use tokio::sync::RwLock;
use typemap_rev::TypeMap;
#[cfg(feature = "cache")]
pub use crate::cache::Cache;
use crate::gateway::ActivityData;
#[cfg(feature = "gateway")]
use crate::gateway::{ShardMessenger, ShardRunner};
use crate::http::Http;
use crate::model::prelude::*;
#[derive(Clone)]
pub struct Context {
pub data: Arc<RwLock<TypeMap>>,
pub shard: ShardMessenger,
pub shard_id: ShardId,
pub http: Arc<Http>,
#[cfg(feature = "cache")]
pub cache: Arc<Cache>,
}
impl fmt::Debug for Context {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Context")
.field("shard", &self.shard)
.field("shard_id", &self.shard_id)
.finish_non_exhaustive()
}
}
impl Context {
#[cfg(feature = "gateway")]
pub(crate) fn new(
data: Arc<RwLock<TypeMap>>,
runner: &ShardRunner,
shard_id: ShardId,
http: Arc<Http>,
#[cfg(feature = "cache")] cache: Arc<Cache>,
) -> Context {
Context {
shard: ShardMessenger::new(runner),
shard_id,
data,
http,
#[cfg(feature = "cache")]
cache,
}
}
#[cfg(all(not(feature = "cache"), not(feature = "gateway")))]
pub fn easy(data: Arc<RwLock<TypeMap>>, shard_id: u32, http: Arc<Http>) -> Context {
Context {
shard_id,
data,
http,
}
}
#[cfg(feature = "gateway")]
#[inline]
pub fn online(&self) {
self.shard.set_status(OnlineStatus::Online);
}
#[cfg(feature = "gateway")]
#[inline]
pub fn idle(&self) {
self.shard.set_status(OnlineStatus::Idle);
}
#[cfg(feature = "gateway")]
#[inline]
pub fn dnd(&self) {
self.shard.set_status(OnlineStatus::DoNotDisturb);
}
#[cfg(feature = "gateway")]
#[inline]
pub fn invisible(&self) {
self.shard.set_status(OnlineStatus::Invisible);
}
#[cfg(feature = "gateway")]
#[inline]
pub fn reset_presence(&self) {
self.shard.set_presence(None, OnlineStatus::Online);
}
#[cfg(feature = "gateway")]
#[inline]
pub fn set_activity(&self, activity: Option<ActivityData>) {
self.shard.set_activity(activity);
}
#[cfg(feature = "gateway")]
#[inline]
pub fn set_presence(&self, activity: Option<ActivityData>, status: OnlineStatus) {
self.shard.set_presence(activity, status);
}
pub async fn get_application_emojis(&self) -> Result<Vec<Emoji>> {
self.http.get_application_emojis().await
}
pub async fn get_application_emoji(&self, emoji_id: EmojiId) -> Result<Emoji> {
self.http.get_application_emoji(emoji_id).await
}
pub async fn create_application_emoji(&self, name: &str, image: &str) -> Result<Emoji> {
#[derive(serde::Serialize)]
struct CreateEmoji<'a> {
name: &'a str,
image: &'a str,
}
let body = CreateEmoji {
name,
image,
};
self.http.create_application_emoji(&body).await
}
pub async fn edit_application_emoji(&self, emoji_id: EmojiId, name: &str) -> Result<Emoji> {
#[derive(serde::Serialize)]
struct EditEmoji<'a> {
name: &'a str,
}
let body = EditEmoji {
name,
};
self.http.edit_application_emoji(emoji_id, &body).await
}
pub async fn delete_application_emoji(&self, emoji_id: EmojiId) -> Result<()> {
self.http.delete_application_emoji(emoji_id).await
}
}
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
}
}