Partial thread support

This commit is contained in:
2023-05-11 15:02:26 +01:00
parent 6e831c8253
commit 523ab7f03a
4 changed files with 103 additions and 13 deletions

View File

@ -5,7 +5,7 @@ pub mod timer;
pub mod user_data;
use chrono_tz::Tz;
use poise::serenity_prelude::{async_trait, model::id::UserId};
use poise::serenity_prelude::{async_trait, model::id::UserId, ChannelType};
use crate::{
models::{channel_data::ChannelData, user_data::UserData},
@ -43,7 +43,20 @@ impl CtxData for Context<'_> {
}
async fn channel_data(&self) -> Result<ChannelData, Box<dyn std::error::Error + Sync + Send>> {
let channel = self.channel_id().to_channel_cached(&self.discord()).unwrap();
// If we're in a thread, get the parent channel.
let recv_channel = self.channel_id().to_channel(&self.discord()).await?;
let channel = match recv_channel.guild() {
Some(guild_channel) => {
if guild_channel.kind == ChannelType::PublicThread {
guild_channel.parent_id.unwrap().to_channel_cached(&self.discord()).unwrap()
} else {
self.channel_id().to_channel_cached(&self.discord()).unwrap()
}
}
None => self.channel_id().to_channel_cached(&self.discord()).unwrap(),
};
ChannelData::from_channel(&channel, &self.data().database).await
}

View File

@ -36,6 +36,7 @@ async fn create_webhook(
pub enum ReminderScope {
User(u64),
Channel(u64),
Thread(u64),
}
impl ReminderScope {
@ -43,6 +44,7 @@ impl ReminderScope {
match self {
Self::User(id) => format!("<@{}>", id),
Self::Channel(id) => format!("<#{}>", id),
Self::Thread(id) => format!("<#{}>", id),
}
}
}
@ -51,6 +53,7 @@ pub struct ReminderBuilder {
pool: MySqlPool,
uid: String,
channel: u32,
thread_id: Option<u64>,
utc_time: NaiveDateTime,
timezone: String,
interval_seconds: Option<i64>,
@ -299,6 +302,66 @@ impl<'a> MultiReminderBuilder<'a> {
Err(ReminderError::InvalidTag)
}
}
ReminderScope::Thread(thread_id) => {
let thread =
ChannelId(thread_id).to_channel(&self.ctx.discord()).await.unwrap();
if let Some(guild_channel) = thread.guild() {
if Some(guild_channel.guild_id) != self.guild_id {
Err(ReminderError::InvalidTag)
} else {
match guild_channel.parent_id {
Some(parent_id) => {
let channel = parent_id
.to_channel(&self.ctx.discord())
.await
.unwrap();
let mut channel_data = ChannelData::from_channel(
&channel,
&self.ctx.data().database,
)
.await
.unwrap();
if channel_data.webhook_id.is_none()
|| channel_data.webhook_token.is_none()
{
match create_webhook(
&self.ctx.discord(),
channel.guild().unwrap(),
"Reminder",
)
.await
{
Ok(webhook) => {
channel_data.webhook_id =
Some(webhook.id.as_u64().to_owned());
channel_data.webhook_token = webhook.token;
channel_data
.commit_changes(&self.ctx.data().database)
.await;
Ok(channel_data.id)
}
Err(e) => {
Err(ReminderError::DiscordError(e.to_string()))
}
}
} else {
Ok(channel_data.id)
}
}
None => Err(ReminderError::InvalidTag),
}
}
} else {
Err(ReminderError::InvalidTag)
}
}
};
match db_channel_id {
@ -307,6 +370,7 @@ impl<'a> MultiReminderBuilder<'a> {
pool: self.ctx.data().database.clone(),
uid: generate_uid(),
channel: c,
thread_id: None,
utc_time: self.utc_time,
timezone: self.timezone.to_string(),
interval_seconds: self.interval.map(|i| i.sec as i64),