Begin to work on thread support
This commit is contained in:
1 parent
8e6e1a18b7
commit
dcee9e0d2a
3 files changed
+82
-59
No files matched your search
@@ -3,19 +3,13 @@ use std::collections::HashSet;
|
||||
use chrono::{Duration, NaiveDateTime, Utc};
|
||||
use chrono_tz::Tz;
|
||||
use poise::serenity_prelude::{
|
||||
http::CacheHttp,
|
||||
model::{
|
||||
channel::GuildChannel,
|
||||
id::{ChannelId, GuildId, UserId},
|
||||
webhook::Webhook,
|
||||
},
|
||||
ChannelType, CreateWebhook, Result as SerenityResult,
|
||||
model::id::{ChannelId, GuildId, UserId},
|
||||
ChannelType,
|
||||
};
|
||||
use secrecy::ExposeSecret;
|
||||
use sqlx::MySqlPool;
|
||||
|
||||
use crate::{
|
||||
consts::{DAY, DEFAULT_AVATAR, MAX_TIME, MIN_INTERVAL},
|
||||
consts::{DAY, MAX_TIME, MIN_INTERVAL},
|
||||
interval_parser::Interval,
|
||||
models::{
|
||||
channel_data::ChannelData,
|
||||
@@ -25,25 +19,23 @@ use crate::{
|
||||
Context,
|
||||
};
|
||||
|
||||
async fn create_webhook(
|
||||
ctx: impl CacheHttp,
|
||||
channel: GuildChannel,
|
||||
name: impl Into<String>,
|
||||
) -> SerenityResult<Webhook> {
|
||||
channel.create_webhook(ctx.http(), CreateWebhook::new(name).avatar(&*DEFAULT_AVATAR)).await
|
||||
#[derive(Hash, PartialEq, Eq, Copy, Clone)]
|
||||
pub struct ChannelWithThread {
|
||||
pub channel_id: u64,
|
||||
pub thread_id: Option<u64>,
|
||||
}
|
||||
|
||||
#[derive(Hash, PartialEq, Eq)]
|
||||
pub enum ReminderScope {
|
||||
User(u64),
|
||||
Channel(u64),
|
||||
Channel(ChannelWithThread),
|
||||
}
|
||||
|
||||
impl ReminderScope {
|
||||
pub fn mention(&self) -> String {
|
||||
match self {
|
||||
Self::User(id) => format!("<@{}>", id),
|
||||
Self::Channel(id) => format!("<#{}>", id),
|
||||
Self::Channel(c) => format!("<#{}>", c.channel_id),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -89,6 +81,7 @@ impl ReminderBuilder {
|
||||
INSERT INTO reminders (
|
||||
`uid`,
|
||||
`channel_id`,
|
||||
`thread_id`,
|
||||
`utc_time`,
|
||||
`timezone`,
|
||||
`interval_seconds`,
|
||||
@@ -101,11 +94,12 @@ impl ReminderBuilder {
|
||||
`attachment`,
|
||||
`set_by`
|
||||
) VALUES (
|
||||
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
|
||||
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
|
||||
)
|
||||
",
|
||||
self.uid,
|
||||
self.channel,
|
||||
self.thread_id,
|
||||
utc_time,
|
||||
self.timezone,
|
||||
self.interval_seconds,
|
||||
@@ -218,7 +212,6 @@ impl<'a> MultiReminderBuilder<'a> {
|
||||
errors.insert(ReminderError::LongInterval);
|
||||
} else {
|
||||
for scope in self.scopes {
|
||||
let thread_id = None;
|
||||
let db_channel_id = match scope {
|
||||
ReminderScope::User(user_id) => {
|
||||
if let Ok(user) = UserId::new(user_id).to_user(&self.ctx).await {
|
||||
@@ -238,34 +231,34 @@ impl<'a> MultiReminderBuilder<'a> {
|
||||
{
|
||||
Err(ReminderError::UserBlockedDm)
|
||||
} else {
|
||||
Ok(user_data.dm_channel)
|
||||
Ok((user_data.dm_channel, None))
|
||||
}
|
||||
} else {
|
||||
Ok(user_data.dm_channel)
|
||||
Ok((user_data.dm_channel, None))
|
||||
}
|
||||
} else {
|
||||
Err(ReminderError::InvalidTag)
|
||||
}
|
||||
}
|
||||
ReminderScope::Channel(channel_id) => {
|
||||
let channel =
|
||||
ChannelId::new(channel_id).to_channel(&self.ctx).await.unwrap();
|
||||
ReminderScope::Channel(channel_with_thread) => {
|
||||
let channel = ChannelId::new(channel_with_thread.channel_id)
|
||||
.to_channel(&self.ctx)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
if let Some(mut guild_channel) = channel.clone().guild() {
|
||||
if let Some(guild_channel) = channel.clone().guild() {
|
||||
if Some(guild_channel.guild_id) != self.guild_id {
|
||||
Err(ReminderError::InvalidTag)
|
||||
} else {
|
||||
let mut channel_data = if guild_channel.kind
|
||||
== ChannelType::PublicThread
|
||||
{
|
||||
// fixme jesus christ
|
||||
let parent = guild_channel
|
||||
.parent_id
|
||||
.unwrap()
|
||||
.to_channel(&self.ctx)
|
||||
.await
|
||||
.unwrap();
|
||||
guild_channel = parent.clone().guild().unwrap();
|
||||
ChannelData::from_channel(&parent, &self.ctx.data().database)
|
||||
.await
|
||||
.unwrap()
|
||||
@@ -275,28 +268,13 @@ impl<'a> MultiReminderBuilder<'a> {
|
||||
.unwrap()
|
||||
};
|
||||
|
||||
if channel_data.webhook_id.is_none()
|
||||
|| channel_data.webhook_token.is_none()
|
||||
match channel_data
|
||||
.ensure_webhook(&self.ctx, &self.ctx.data().database)
|
||||
.await
|
||||
.map_err(|e| ReminderError::DiscordError(e.to_string()))
|
||||
{
|
||||
match create_webhook(&self.ctx, guild_channel, "Reminder").await
|
||||
{
|
||||
Ok(webhook) => {
|
||||
channel_data.webhook_id =
|
||||
Some(webhook.id.get().to_owned());
|
||||
channel_data.webhook_token =
|
||||
webhook.token.map(|s| s.expose_secret().clone());
|
||||
|
||||
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)
|
||||
Ok(()) => Ok((channel_data.id, channel_with_thread.thread_id)),
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
} else {
|
||||
@@ -306,11 +284,11 @@ impl<'a> MultiReminderBuilder<'a> {
|
||||
};
|
||||
|
||||
match db_channel_id {
|
||||
Ok(c) => {
|
||||
Ok((channel, thread_id)) => {
|
||||
let builder = ReminderBuilder {
|
||||
pool: self.ctx.data().database.clone(),
|
||||
uid: generate_uid(),
|
||||
channel: c,
|
||||
channel,
|
||||
thread_id,
|
||||
utc_time: self.utc_time,
|
||||
timezone: self.timezone.to_string(),
|
||||
|
||||
Reference in new issue
Block a user