Begin to work on thread support
This commit is contained in:
@ -1,9 +1,13 @@
|
||||
use chrono::NaiveDateTime;
|
||||
use poise::serenity_prelude::model::channel::Channel;
|
||||
use poise::serenity_prelude::{model::channel::Channel, CacheHttp, ChannelId, CreateWebhook};
|
||||
use secrecy::ExposeSecret;
|
||||
use sqlx::MySqlPool;
|
||||
|
||||
use crate::{consts::DEFAULT_AVATAR, Error};
|
||||
|
||||
pub struct ChannelData {
|
||||
pub id: u32,
|
||||
pub channel: u64,
|
||||
pub name: Option<String>,
|
||||
pub nudge: i16,
|
||||
pub blacklisted: bool,
|
||||
@ -22,7 +26,12 @@ 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, channel, name, nudge, blacklisted, webhook_id, webhook_token, paused,
|
||||
paused_until
|
||||
FROM channels
|
||||
WHERE channel = ?
|
||||
",
|
||||
channel_id
|
||||
)
|
||||
.fetch_one(pool)
|
||||
@ -32,7 +41,8 @@ impl ChannelData {
|
||||
} else {
|
||||
let props = channel.to_owned().guild().map(|g| (g.guild_id.get().to_owned(), g.name));
|
||||
|
||||
let (guild_id, channel_name) = if let Some((a, b)) = props { (Some(a), Some(b)) } else { (None, None) };
|
||||
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 = ?))",
|
||||
@ -46,7 +56,9 @@ impl ChannelData {
|
||||
Ok(sqlx::query_as_unchecked!(
|
||||
Self,
|
||||
"
|
||||
SELECT id, name, nudge, blacklisted, webhook_id, webhook_token, paused, paused_until FROM channels WHERE channel = ?
|
||||
SELECT id, channel, name, nudge, blacklisted, webhook_id, webhook_token, paused, paused_until
|
||||
FROM channels
|
||||
WHERE channel = ?
|
||||
",
|
||||
channel_id
|
||||
)
|
||||
@ -58,8 +70,16 @@ SELECT id, name, nudge, blacklisted, webhook_id, webhook_token, paused, paused_u
|
||||
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 = ?
|
||||
UPDATE channels
|
||||
SET
|
||||
name = ?,
|
||||
nudge = ?,
|
||||
blacklisted = ?,
|
||||
webhook_id = ?,
|
||||
webhook_token = ?,
|
||||
paused = ?,
|
||||
paused_until = ?
|
||||
WHERE id = ?
|
||||
",
|
||||
self.name,
|
||||
self.nudge,
|
||||
@ -74,4 +94,24 @@ UPDATE channels SET name = ?, nudge = ?, blacklisted = ?, webhook_id = ?, webhoo
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
pub async fn ensure_webhook(
|
||||
&mut self,
|
||||
ctx: impl CacheHttp,
|
||||
pool: &MySqlPool,
|
||||
) -> Result<(), Error> {
|
||||
if self.webhook_id.is_none() || self.webhook_token.is_none() {
|
||||
let guild_channel = ChannelId::new(self.channel);
|
||||
|
||||
let webhook = guild_channel
|
||||
.create_webhook(ctx.http(), CreateWebhook::new("Reminder").avatar(&*DEFAULT_AVATAR))
|
||||
.await?;
|
||||
|
||||
self.webhook_id = Some(webhook.id.get().to_owned());
|
||||
self.webhook_token = webhook.token.map(|s| s.expose_secret().clone());
|
||||
self.commit_changes(pool).await;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user