reminder-bot/src/models/channel_data.rs

82 lines
2.3 KiB
Rust
Raw Normal View History

use chrono::NaiveDateTime;
2021-07-16 20:28:51 +00:00
use serenity::model::channel::Channel;
use sqlx::MySqlPool;
pub struct ChannelData {
pub id: u32,
pub name: Option<String>,
pub nudge: i16,
pub blacklisted: bool,
pub webhook_id: Option<u64>,
pub webhook_token: Option<String>,
pub paused: bool,
pub paused_until: Option<NaiveDateTime>,
}
impl ChannelData {
pub async fn from_channel(
channel: &Channel,
2021-07-16 20:28:51 +00:00
pool: &MySqlPool,
) -> Result<Self, Box<dyn std::error::Error + Sync + Send>> {
let channel_id = channel.id().as_u64().to_owned();
if let Ok(c) = sqlx::query_as_unchecked!(
Self,
2021-07-16 20:28:51 +00:00
"
SELECT id, name, nudge, blacklisted, webhook_id, webhook_token, paused, paused_until FROM channels WHERE channel = ?
",
channel_id
)
.fetch_one(pool)
.await
{
2021-07-16 20:28:51 +00:00
Ok(c)
} else {
let props = channel.to_owned().guild().map(|g| (g.guild_id.as_u64().to_owned(), g.name));
2021-07-16 20:28:51 +00:00
let (guild_id, channel_name) = if let Some((a, b)) = props { (Some(a), Some(b)) } else { (None, None) };
2021-07-16 20:28:51 +00:00
sqlx::query!(
"
INSERT IGNORE INTO channels (channel, name, guild_id) VALUES (?, ?, (SELECT id FROM guilds WHERE guild = ?))
",
channel_id,
channel_name,
guild_id
)
.execute(&pool.clone())
.await?;
2021-07-16 20:28:51 +00:00
Ok(sqlx::query_as_unchecked!(
Self,
2021-07-16 20:28:51 +00:00
"
SELECT id, name, nudge, blacklisted, webhook_id, webhook_token, paused, paused_until FROM channels WHERE channel = ?
",
channel_id
)
.fetch_one(pool)
.await?)
2021-07-16 20:28:51 +00:00
}
}
pub async fn commit_changes(&self, pool: &MySqlPool) {
sqlx::query!(
"
UPDATE channels SET name = ?, nudge = ?, blacklisted = ?, webhook_id = ?, webhook_token = ?, paused = ?, paused_until \
= ? WHERE id = ?
",
self.name,
self.nudge,
self.blacklisted,
self.webhook_id,
self.webhook_token,
self.paused,
self.paused_until,
self.id
)
.execute(pool)
.await
.unwrap();
2021-07-16 20:28:51 +00:00
}
}