Support ephemeral reminder confirmations

This commit is contained in:
jude
2023-05-11 19:40:33 +01:00
parent 4b42966284
commit aa931328b0
8 changed files with 171 additions and 25 deletions

View File

@ -22,9 +22,7 @@ impl ChannelData {
if let Ok(c) = sqlx::query_as_unchecked!(
Self,
"
SELECT id, name, nudge, blacklisted, webhook_id, webhook_token, paused, paused_until FROM channels WHERE channel = ?
",
"SELECT id, name, nudge, blacklisted, webhook_id, webhook_token, paused, paused_until FROM channels WHERE channel = ?",
channel_id
)
.fetch_one(pool)
@ -37,9 +35,7 @@ SELECT id, name, nudge, blacklisted, webhook_id, webhook_token, paused, paused_u
let (guild_id, channel_name) = if let Some((a, b)) = props { (Some(a), Some(b)) } else { (None, None) };
sqlx::query!(
"
INSERT IGNORE INTO channels (channel, name, guild_id) VALUES (?, ?, (SELECT id FROM guilds WHERE guild = ?))
",
"INSERT IGNORE INTO channels (channel, name, guild_id) VALUES (?, ?, (SELECT id FROM guilds WHERE guild = ?))",
channel_id,
channel_name,
guild_id

48
src/models/guild_data.rs Normal file
View File

@ -0,0 +1,48 @@
use poise::serenity_prelude::GuildId;
use sqlx::MySqlPool;
pub struct GuildData {
pub ephemeral_confirmations: bool,
pub id: u32,
}
impl GuildData {
pub async fn from_guild(
guild_id: GuildId,
pool: &MySqlPool,
) -> Result<Self, Box<dyn std::error::Error + Sync + Send>> {
if let Ok(c) = sqlx::query_as_unchecked!(
Self,
"SELECT id, ephemeral_confirmations FROM guilds WHERE guild = ?",
guild_id.0
)
.fetch_one(pool)
.await
{
Ok(c)
} else {
sqlx::query!("INSERT IGNORE INTO guilds (guild) VALUES (?)", guild_id.0)
.execute(&pool.clone())
.await?;
Ok(sqlx::query_as_unchecked!(
Self,
"SELECT id, ephemeral_confirmations FROM guilds WHERE guild = ?",
guild_id.0
)
.fetch_one(pool)
.await?)
}
}
pub async fn commit_changes(&self, pool: &MySqlPool) {
sqlx::query!(
"UPDATE guilds SET ephemeral_confirmations = ? WHERE id = ?",
self.ephemeral_confirmations,
self.id
)
.execute(pool)
.await
.unwrap();
}
}

View File

@ -1,5 +1,6 @@
pub mod channel_data;
pub mod command_macro;
pub mod guild_data;
pub mod reminder;
pub mod timer;
pub mod user_data;
@ -8,7 +9,7 @@ use chrono_tz::Tz;
use poise::serenity_prelude::{async_trait, model::id::UserId, ChannelType};
use crate::{
models::{channel_data::ChannelData, user_data::UserData},
models::{channel_data::ChannelData, guild_data::GuildData, user_data::UserData},
CommandMacro, Context, Data, Error, GuildId,
};
@ -18,6 +19,8 @@ pub trait CtxData {
async fn author_data(&self) -> Result<UserData, Error>;
async fn guild_data(&self) -> Option<Result<GuildData, Error>>;
async fn timezone(&self) -> Tz;
async fn channel_data(&self) -> Result<ChannelData, Error>;
@ -27,17 +30,22 @@ pub trait CtxData {
#[async_trait]
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>> {
async fn user_data<U: Into<UserId> + Send>(&self, user_id: U) -> Result<UserData, Error> {
UserData::from_user(user_id, &self.discord(), &self.data().database).await
}
async fn author_data(&self) -> Result<UserData, Box<dyn std::error::Error + Sync + Send>> {
async fn author_data(&self) -> Result<UserData, Error> {
UserData::from_user(&self.author().id, &self.discord(), &self.data().database).await
}
async fn guild_data(&self) -> Option<Result<GuildData, Error>> {
if let Some(guild_id) = self.guild_id() {
Some(GuildData::from_guild(guild_id, &self.data().database).await)
} else {
None
}
}
async fn timezone(&self) -> Tz {
UserData::timezone_of(self.author().id, &self.data().database).await
}