reminder-bot/src/models/mod.rs

52 lines
1.5 KiB
Rust
Raw Normal View History

2021-07-16 20:28:51 +00:00
pub mod channel_data;
pub mod command_macro;
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-18 12:40:30 +00:00
use chrono_tz::Tz;
2022-02-19 14:32:03 +00:00
use poise::serenity::{async_trait, model::id::UserId};
2021-07-16 20:28:51 +00:00
use crate::{
2021-10-26 20:10:14 +00:00
models::{channel_data::ChannelData, user_data::UserData},
2022-02-19 14:32:03 +00:00
Context,
};
2021-07-16 20:28:51 +00:00
#[async_trait]
pub trait CtxData {
2022-02-19 14:32:03 +00:00
async fn user_data<U: Into<UserId> + Send>(
&self,
user_id: U,
) -> Result<UserData, Box<dyn std::error::Error + Sync + Send>>;
2022-02-19 14:32:03 +00:00
async fn author_data(&self) -> Result<UserData, Box<dyn std::error::Error + Sync + Send>>;
2021-09-18 12:40:30 +00:00
2022-02-19 14:32:03 +00:00
async fn timezone(&self) -> Tz;
async fn channel_data(&self) -> Result<ChannelData, Box<dyn std::error::Error + Sync + Send>>;
2021-07-16 20:28:51 +00:00
}
#[async_trait]
2022-02-19 14:32:03 +00:00
impl CtxData for Context<'_> {
async fn user_data<U: Into<UserId> + Send>(
&self,
user_id: U,
) -> Result<UserData, Box<dyn std::error::Error + Sync + Send>> {
2022-02-19 14:32:03 +00:00
UserData::from_user(user_id, &self.discord(), &self.data().database).await
}
2022-02-19 14:32:03 +00:00
async fn author_data(&self) -> Result<UserData, Box<dyn std::error::Error + Sync + Send>> {
UserData::from_user(&self.author().id, &self.discord(), &self.data().database).await
2021-09-18 12:40:30 +00:00
}
2022-02-19 14:32:03 +00:00
async fn timezone(&self) -> Tz {
UserData::timezone_of(self.author().id, &self.data().database).await
}
2022-02-19 14:32:03 +00:00
async fn channel_data(&self) -> Result<ChannelData, Box<dyn std::error::Error + Sync + Send>> {
let channel = self.channel_id().to_channel_cached(&self.discord()).unwrap();
2022-02-19 14:32:03 +00:00
ChannelData::from_channel(&channel, &self.data().database).await
2021-07-16 20:28:51 +00:00
}
}