Moved stuff around since threads are ridiculous

This commit is contained in:
jude 2023-05-11 16:27:11 +01:00
parent 523ab7f03a
commit 4b42966284
2 changed files with 26 additions and 82 deletions

View File

@ -5,8 +5,7 @@ use chrono_tz::Tz;
use num_integer::Integer; use num_integer::Integer;
use poise::{ use poise::{
serenity_prelude::{ serenity_prelude::{
builder::CreateEmbed, component::ButtonStyle, model::channel::Channel, ChannelType, builder::CreateEmbed, component::ButtonStyle, model::channel::Channel, ReactionType,
ReactionType,
}, },
CreateReply, Modal, CreateReply, Modal,
}; };
@ -665,18 +664,10 @@ async fn create_reminder(
let list = channels.map(|arg| parse_mention_list(&arg)).unwrap_or_default(); let list = channels.map(|arg| parse_mention_list(&arg)).unwrap_or_default();
if list.is_empty() { if list.is_empty() {
let channel = ctx.channel_id().to_channel(&ctx.discord()).await?; if ctx.guild_id().is_some() {
vec![ReminderScope::Channel(ctx.channel_id().0)]
match channel.guild() { } else {
Some(guild_channel) => { vec![ReminderScope::User(ctx.author().id.0)]
if guild_channel.kind == ChannelType::PublicThread {
vec![ReminderScope::Thread(ctx.channel_id().0)]
} else {
vec![ReminderScope::Channel(ctx.channel_id().0)]
}
}
None => vec![ReminderScope::User(ctx.author().id.0)],
} }
} else { } else {
list list
@ -824,7 +815,6 @@ fn create_response(
embed embed
} }
// TODO process threads here
fn parse_mention_list(mentions: &str) -> Vec<ReminderScope> { fn parse_mention_list(mentions: &str) -> Vec<ReminderScope> {
REGEX_CHANNEL_USER REGEX_CHANNEL_USER
.captures_iter(mentions) .captures_iter(mentions)

View File

@ -9,7 +9,7 @@ use poise::serenity_prelude::{
id::{ChannelId, GuildId, UserId}, id::{ChannelId, GuildId, UserId},
webhook::Webhook, webhook::Webhook,
}, },
Result as SerenityResult, ChannelType, Result as SerenityResult,
}; };
use sqlx::MySqlPool; use sqlx::MySqlPool;
@ -36,7 +36,6 @@ async fn create_webhook(
pub enum ReminderScope { pub enum ReminderScope {
User(u64), User(u64),
Channel(u64), Channel(u64),
Thread(u64),
} }
impl ReminderScope { impl ReminderScope {
@ -44,7 +43,6 @@ impl ReminderScope {
match self { match self {
Self::User(id) => format!("<@{}>", id), Self::User(id) => format!("<@{}>", id),
Self::Channel(id) => format!("<#{}>", id), Self::Channel(id) => format!("<#{}>", id),
Self::Thread(id) => format!("<#{}>", id),
} }
} }
} }
@ -229,6 +227,7 @@ impl<'a> MultiReminderBuilder<'a> {
errors.insert(ReminderError::LongInterval); errors.insert(ReminderError::LongInterval);
} else { } else {
for scope in self.scopes { for scope in self.scopes {
let thread_id = None;
let db_channel_id = match scope { let db_channel_id = match scope {
ReminderScope::User(user_id) => { ReminderScope::User(user_id) => {
if let Ok(user) = UserId(user_id).to_user(&self.ctx.discord()).await { if let Ok(user) = UserId(user_id).to_user(&self.ctx.discord()).await {
@ -261,14 +260,29 @@ impl<'a> MultiReminderBuilder<'a> {
let channel = let channel =
ChannelId(channel_id).to_channel(&self.ctx.discord()).await.unwrap(); ChannelId(channel_id).to_channel(&self.ctx.discord()).await.unwrap();
if let Some(guild_channel) = channel.clone().guild() { if let Some(mut guild_channel) = channel.clone().guild() {
if Some(guild_channel.guild_id) != self.guild_id { if Some(guild_channel.guild_id) != self.guild_id {
Err(ReminderError::InvalidTag) Err(ReminderError::InvalidTag)
} else { } else {
let mut channel_data = let mut channel_data = if guild_channel.kind
ChannelData::from_channel(&channel, &self.ctx.data().database) == ChannelType::PublicThread
{
// fixme jesus christ
let parent = guild_channel
.parent_id
.unwrap()
.to_channel(&self.ctx.discord())
.await .await
.unwrap(); .unwrap();
guild_channel = parent.clone().guild().unwrap();
ChannelData::from_channel(&parent, &self.ctx.data().database)
.await
.unwrap()
} else {
ChannelData::from_channel(&channel, &self.ctx.data().database)
.await
.unwrap()
};
if channel_data.webhook_id.is_none() if channel_data.webhook_id.is_none()
|| channel_data.webhook_token.is_none() || channel_data.webhook_token.is_none()
@ -302,66 +316,6 @@ impl<'a> MultiReminderBuilder<'a> {
Err(ReminderError::InvalidTag) 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 { match db_channel_id {
@ -370,7 +324,7 @@ impl<'a> MultiReminderBuilder<'a> {
pool: self.ctx.data().database.clone(), pool: self.ctx.data().database.clone(),
uid: generate_uid(), uid: generate_uid(),
channel: c, channel: c,
thread_id: None, thread_id,
utc_time: self.utc_time, utc_time: self.utc_time,
timezone: self.timezone.to_string(), timezone: self.timezone.to_string(),
interval_seconds: self.interval.map(|i| i.sec as i64), interval_seconds: self.interval.map(|i| i.sec as i64),