2021-07-16 20:28:51 +00:00
|
|
|
pub mod channel_data;
|
|
|
|
pub mod guild_data;
|
2021-07-17 16:00:47 +00:00
|
|
|
pub mod reminder;
|
2021-07-16 20:28:51 +00:00
|
|
|
pub mod timer;
|
|
|
|
pub mod user_data;
|
|
|
|
|
2021-09-06 12:46:16 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2021-09-18 12:40:30 +00:00
|
|
|
use chrono_tz::Tz;
|
2021-09-02 22:38:12 +00:00
|
|
|
use serenity::{
|
|
|
|
async_trait,
|
2021-09-10 23:14:23 +00:00
|
|
|
model::id::{ChannelId, GuildId, UserId},
|
2021-09-02 22:38:12 +00:00
|
|
|
prelude::Context,
|
|
|
|
};
|
2021-07-16 20:28:51 +00:00
|
|
|
use tokio::sync::RwLock;
|
|
|
|
|
2021-09-10 23:14:23 +00:00
|
|
|
use crate::{
|
|
|
|
consts::DEFAULT_PREFIX,
|
|
|
|
models::{channel_data::ChannelData, guild_data::GuildData, user_data::UserData},
|
|
|
|
GuildDataCache, SQLPool,
|
|
|
|
};
|
2021-09-06 12:46:16 +00:00
|
|
|
|
2021-07-16 20:28:51 +00:00
|
|
|
#[async_trait]
|
2021-09-02 22:38:12 +00:00
|
|
|
pub trait CtxData {
|
2021-07-16 20:28:51 +00:00
|
|
|
async fn guild_data<G: Into<GuildId> + Send + Sync>(
|
|
|
|
&self,
|
|
|
|
guild_id: G,
|
|
|
|
) -> Result<Arc<RwLock<GuildData>>, sqlx::Error>;
|
|
|
|
|
2021-09-10 23:14:23 +00:00
|
|
|
async fn prefix<G: Into<GuildId> + Send + Sync>(&self, guild_id: Option<G>) -> String;
|
|
|
|
|
2021-09-02 22:38:12 +00:00
|
|
|
async fn user_data<U: Into<UserId> + Send + Sync>(
|
|
|
|
&self,
|
|
|
|
user_id: U,
|
|
|
|
) -> Result<UserData, Box<dyn std::error::Error + Sync + Send>>;
|
|
|
|
|
2021-09-18 12:40:30 +00:00
|
|
|
async fn timezone<U: Into<UserId> + Send + Sync>(&self, user_id: U) -> Tz;
|
|
|
|
|
2021-09-10 23:14:23 +00:00
|
|
|
async fn channel_data<C: Into<ChannelId> + Send + Sync>(
|
|
|
|
&self,
|
|
|
|
channel_id: C,
|
|
|
|
) -> Result<ChannelData, Box<dyn std::error::Error + Sync + Send>>;
|
2021-07-16 20:28:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait]
|
2021-09-02 22:38:12 +00:00
|
|
|
impl CtxData for Context {
|
2021-07-16 20:28:51 +00:00
|
|
|
async fn guild_data<G: Into<GuildId> + Send + Sync>(
|
|
|
|
&self,
|
|
|
|
guild_id: G,
|
|
|
|
) -> Result<Arc<RwLock<GuildData>>, sqlx::Error> {
|
|
|
|
let guild_id = guild_id.into();
|
|
|
|
|
2021-09-02 22:38:12 +00:00
|
|
|
let guild = guild_id.to_guild_cached(&self.cache).unwrap();
|
2021-07-16 20:28:51 +00:00
|
|
|
|
2021-09-10 23:14:23 +00:00
|
|
|
let guild_cache = self.data.read().await.get::<GuildDataCache>().cloned().unwrap();
|
2021-07-16 20:28:51 +00:00
|
|
|
|
|
|
|
let x = if let Some(guild_data) = guild_cache.get(&guild_id) {
|
|
|
|
Ok(guild_data.clone())
|
|
|
|
} else {
|
|
|
|
let pool = self.data.read().await.get::<SQLPool>().cloned().unwrap();
|
|
|
|
|
|
|
|
match GuildData::from_guild(guild, &pool).await {
|
|
|
|
Ok(d) => {
|
|
|
|
let lock = Arc::new(RwLock::new(d));
|
|
|
|
|
|
|
|
guild_cache.insert(guild_id, lock.clone());
|
|
|
|
|
|
|
|
Ok(lock)
|
|
|
|
}
|
|
|
|
|
|
|
|
Err(e) => Err(e),
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
x
|
|
|
|
}
|
|
|
|
|
2021-09-10 23:14:23 +00:00
|
|
|
async fn prefix<G: Into<GuildId> + Send + Sync>(&self, guild_id: Option<G>) -> String {
|
|
|
|
if let Some(guild_id) = guild_id {
|
|
|
|
self.guild_data(guild_id).await.unwrap().read().await.prefix.clone()
|
|
|
|
} else {
|
|
|
|
DEFAULT_PREFIX.clone()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-02 22:38:12 +00:00
|
|
|
async fn user_data<U: Into<UserId> + Send + Sync>(
|
|
|
|
&self,
|
|
|
|
user_id: U,
|
|
|
|
) -> Result<UserData, Box<dyn std::error::Error + Sync + Send>> {
|
|
|
|
let user_id = user_id.into();
|
|
|
|
let pool = self.data.read().await.get::<SQLPool>().cloned().unwrap();
|
|
|
|
|
|
|
|
let user = user_id.to_user(self).await.unwrap();
|
|
|
|
|
|
|
|
UserData::from_user(&user, &self, &pool).await
|
|
|
|
}
|
|
|
|
|
2021-09-18 12:40:30 +00:00
|
|
|
async fn timezone<U: Into<UserId> + Send + Sync>(&self, user_id: U) -> Tz {
|
|
|
|
let user_id = user_id.into();
|
|
|
|
let pool = self.data.read().await.get::<SQLPool>().cloned().unwrap();
|
|
|
|
|
|
|
|
UserData::timezone_of(user_id, &pool).await
|
|
|
|
}
|
|
|
|
|
2021-09-10 23:14:23 +00:00
|
|
|
async fn channel_data<C: Into<ChannelId> + Send + Sync>(
|
|
|
|
&self,
|
|
|
|
channel_id: C,
|
|
|
|
) -> Result<ChannelData, Box<dyn std::error::Error + Sync + Send>> {
|
|
|
|
let channel_id = channel_id.into();
|
|
|
|
let pool = self.data.read().await.get::<SQLPool>().cloned().unwrap();
|
|
|
|
|
|
|
|
let channel = channel_id.to_channel_cached(&self).unwrap();
|
|
|
|
|
|
|
|
ChannelData::from_channel(&channel, &pool).await
|
2021-07-16 20:28:51 +00:00
|
|
|
}
|
|
|
|
}
|