From 094d210f64f1ddd8d4a57538c4ddc279a876a6aa Mon Sep 17 00:00:00 2001 From: jude Date: Fri, 24 Mar 2023 19:52:41 +0000 Subject: [PATCH] Fix orphaned channels issue again --- web/src/routes/dashboard/export.rs | 2 +- web/src/routes/dashboard/guild.rs | 4 ++-- web/src/routes/dashboard/mod.rs | 19 ++++++++++++++++++- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/web/src/routes/dashboard/export.rs b/web/src/routes/dashboard/export.rs index 0c2ab06..255a05e 100644 --- a/web/src/routes/dashboard/export.rs +++ b/web/src/routes/dashboard/export.rs @@ -172,7 +172,7 @@ pub async fn import_reminders( create_reminder( ctx.inner(), - transaction, + pool.inner(), GuildId(id), UserId(user_id), reminder, diff --git a/web/src/routes/dashboard/guild.rs b/web/src/routes/dashboard/guild.rs index 641835b..9fc66d3 100644 --- a/web/src/routes/dashboard/guild.rs +++ b/web/src/routes/dashboard/guild.rs @@ -249,9 +249,9 @@ pub async fn create_reminder_template( Ok(json!({})) } Err(e) => { - warn!("Could not fetch templates from {}: {:?}", id, e); + warn!("Could not create template for {}: {:?}", id, e); - json_err!("Could not get templates") + json_err!("Could not create template") } } } diff --git a/web/src/routes/dashboard/mod.rs b/web/src/routes/dashboard/mod.rs index 611fe18..dda81d2 100644 --- a/web/src/routes/dashboard/mod.rs +++ b/web/src/routes/dashboard/mod.rs @@ -14,7 +14,7 @@ use serenity::{ http::Http, model::id::{ChannelId, GuildId, UserId}, }; -use sqlx::{types::Json, Executor, MySql, Pool}; +use sqlx::{types::Json, Executor}; use crate::{ check_guild_subscription, check_subscription, @@ -358,6 +358,23 @@ pub async fn create_reminder( user_id: UserId, reminder: Reminder, ) -> JsonResult { + // check guild in db + match sqlx::query!("SELECT 1 as A FROM guilds WHERE guild = ?", guild_id.0) + .fetch_one(pool) + .await + { + Err(sqlx::Error::RowNotFound) => { + if sqlx::query!("INSERT INTO guilds (guild) VALUES (?)", guild_id.0) + .execute(pool) + .await + .is_err() + { + return Err(json!({"error": "Guild could not be created"})); + } + } + _ => {} + } + // validate channel let channel = ChannelId(reminder.channel).to_channel_cached(&ctx); let channel_exists = channel.is_some();