Fix orphaned channels issue again

This commit is contained in:
jude 2023-03-24 19:52:41 +00:00
parent 314c72e132
commit 094d210f64
3 changed files with 21 additions and 4 deletions

View File

@ -172,7 +172,7 @@ pub async fn import_reminders(
create_reminder(
ctx.inner(),
transaction,
pool.inner(),
GuildId(id),
UserId(user_id),
reminder,

View File

@ -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")
}
}
}

View File

@ -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();