reminder-bot/src/models/guild_data.rs

49 lines
1.2 KiB
Rust

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();
}
}