component models

This commit is contained in:
jude
2022-02-19 22:11:21 +00:00
parent afc376c44f
commit 06c4deeaa9
15 changed files with 269 additions and 184 deletions

View File

@ -9,7 +9,7 @@ use poise::serenity::{async_trait, model::id::UserId};
use crate::{
models::{channel_data::ChannelData, user_data::UserData},
CommandMacro, Context, Data, Error,
CommandMacro, Context, Data, Error, GuildId,
};
#[async_trait]
@ -49,13 +49,20 @@ impl CtxData for Context<'_> {
}
async fn command_macros(&self) -> Result<Vec<CommandMacro<Data, Error>>, Error> {
let guild_id = self.guild_id().unwrap();
self.data().command_macros(self.guild_id().unwrap()).await
}
}
impl Data {
pub(crate) async fn command_macros(
&self,
guild_id: GuildId,
) -> Result<Vec<CommandMacro<Data, Error>>, Error> {
let rows = sqlx::query!(
"SELECT name, description FROM macro WHERE guild_id = (SELECT id FROM guilds WHERE guild = ?)",
guild_id.0
)
.fetch_all(&self.data().database)
.fetch_all(&self.database)
.await?.iter().map(|row| CommandMacro {
guild_id,
name: row.name.clone(),

View File

@ -6,12 +6,15 @@ pub mod look_flags;
use chrono::{NaiveDateTime, TimeZone};
use chrono_tz::Tz;
use poise::serenity::model::id::{ChannelId, GuildId, UserId};
use poise::{
serenity::model::id::{ChannelId, GuildId, UserId},
serenity_prelude::Cache,
};
use sqlx::Executor;
use crate::{
models::reminder::look_flags::{LookFlags, TimeDisplayType},
Context, Data, Database,
Database,
};
#[derive(Debug, Clone)]
@ -70,7 +73,7 @@ WHERE
}
pub async fn from_channel<C: Into<ChannelId>>(
ctx: &Context<'_>,
pool: impl Executor<'_, Database = Database>,
channel_id: C,
flags: &LookFlags,
) -> Vec<Self> {
@ -111,18 +114,19 @@ ORDER BY
channel_id.as_u64(),
enabled,
)
.fetch_all(&ctx.data().database)
.fetch_all(pool)
.await
.unwrap()
}
pub async fn from_guild(
ctx: &Context<'_>,
cache: impl AsRef<Cache>,
pool: impl Executor<'_, Database = Database>,
guild_id: Option<GuildId>,
user: UserId,
) -> Vec<Self> {
if let Some(guild_id) = guild_id {
let guild_opt = guild_id.to_guild_cached(&ctx.discord());
let guild_opt = guild_id.to_guild_cached(cache);
if let Some(guild) = guild_opt {
let channels = guild
@ -163,7 +167,7 @@ WHERE
",
channels
)
.fetch_all(&ctx.data().database)
.fetch_all(pool)
.await
} else {
sqlx::query_as_unchecked!(
@ -196,7 +200,7 @@ WHERE
",
guild_id.as_u64()
)
.fetch_all(&ctx.data().database)
.fetch_all(pool)
.await
}
} else {
@ -230,7 +234,7 @@ WHERE
",
user.as_u64()
)
.fetch_all(&ctx.data().database)
.fetch_all(pool)
.await
}
.unwrap()