2 Commits

Author SHA1 Message Date
f35c5082f1 Restructure guilds table 2025-06-18 21:03:41 +01:00
19cfacffe5 Restructure database tables 2025-05-08 20:55:17 +01:00
20 changed files with 168 additions and 12861 deletions

4
Cargo.lock generated
View File

@ -1,6 +1,6 @@
# This file is automatically @generated by Cargo. # This file is automatically @generated by Cargo.
# It is not intended for manual editing. # It is not intended for manual editing.
version = 4 version = 3
[[package]] [[package]]
name = "addr2line" name = "addr2line"
@ -2614,7 +2614,7 @@ checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c"
[[package]] [[package]]
name = "reminder-rs" name = "reminder-rs"
version = "1.7.38" version = "1.7.37"
dependencies = [ dependencies = [
"base64 0.22.1", "base64 0.22.1",
"chrono", "chrono",

View File

@ -1,6 +1,6 @@
[package] [package]
name = "reminder-rs" name = "reminder-rs"
version = "1.7.38" version = "1.7.37"
authors = ["Jude Southworth <judesouthworth@pm.me>"] authors = ["Jude Southworth <judesouthworth@pm.me>"]
edition = "2021" edition = "2021"
license = "AGPL-3.0 only" license = "AGPL-3.0 only"

12676
gb-ipv4.csv

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,83 @@
-- Drop all old tables
DROP TABLE IF EXISTS users_old;
DROP TABLE IF EXISTS messages;
DROP TABLE IF EXISTS embeds;
DROP TABLE IF EXISTS embed_fields;
DROP TABLE IF EXISTS command_aliases;
DROP TABLE IF EXISTS macro;
DROP TABLE IF EXISTS roles;
SET FOREIGN_KEY_CHECKS=0;
-- Drop columns from channels that are no longer used
ALTER TABLE channels DROP COLUMN `name`;
ALTER TABLE channels DROP COLUMN `blacklisted`;
-- Drop columns from guilds table that are no longer used and rebuild table
CREATE TABLE guilds_new (
id BIGINT UNSIGNED NOT NULL PRIMARY KEY,
ephemeral_confirmations BOOLEAN NOT NULL DEFAULT 0
);
INSERT INTO guilds_new (id, ephemeral_confirmations) SELECT guild, ephemeral_confirmations FROM guilds;
RENAME TABLE guilds TO guilds_old;
RENAME TABLE guilds_new TO guilds;
-- Update fk on channels to point at new guild table
ALTER TABLE channels
DROP FOREIGN KEY `channels_ibfk_1`,
MODIFY COLUMN guild_id BIGINT UNSIGNED,
ADD FOREIGN KEY `fk_guild_id` (`guild_id`)
REFERENCES `guilds` (`id`)
ON DELETE CASCADE
ON UPDATE CASCADE;
UPDATE channels SET guild_id = (SELECT guild FROM guilds_old WHERE id = guild_id);
-- Rebuild todos table
CREATE TABLE `todos_new` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`guild_id` BIGINT UNSIGNED DEFAULT NULL,
`channel_id` INT UNSIGNED DEFAULT NULL,
`user_id` BIGINT UNSIGNED DEFAULT NULL,
`value` VARCHAR(2000) NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY `fk_channel_id` (`channel_id`)
REFERENCES `channels` (`id`)
ON DELETE SET NULL
ON UPDATE CASCADE,
FOREIGN KEY `fk_guild_id` (`guild_id`)
REFERENCES `guilds` (`id`)
ON DELETE CASCADE
ON UPDATE CASCADE,
FOREIGN KEY `fk_user_id` (`user_id`)
REFERENCES `users` (`id`)
ON DELETE SET NULL
ON UPDATE CASCADE
);
INSERT INTO todos_new (id, guild_id, channel_id, user_id, value) SELECT id, (SELECT guild FROM guilds_old WHERE id = guild_id), channel_id, user_id, value FROM todos;
RENAME TABLE todos TO todos_old;
RENAME TABLE todos_new TO todos;
DROP TABLE todos_old;
-- Update fk on reminder_template to point at new guild table
ALTER TABLE reminder_template
DROP FOREIGN KEY `reminder_template_ibfk_1`,
MODIFY COLUMN guild_id BIGINT UNSIGNED NOT NULL,
ADD FOREIGN KEY `fk_guild_id` (`guild_id`)
REFERENCES `guilds` (`id`)
ON DELETE CASCADE
ON UPDATE CASCADE;
UPDATE reminder_template SET guild_id = (SELECT guild FROM guilds_old WHERE id = guild_id);
-- Update fk on command_macro to point at new guild table
ALTER TABLE command_macro
DROP FOREIGN KEY `command_macro_ibfk_1`,
MODIFY COLUMN guild_id BIGINT UNSIGNED NOT NULL,
ADD FOREIGN KEY `fk_guild_id` (`guild_id`)
REFERENCES `guilds` (`id`)
ON DELETE CASCADE
ON UPDATE CASCADE;
UPDATE command_macro SET guild_id = (SELECT guild FROM guilds_old WHERE id = guild_id);
DROP TABLE guilds_old;
SET FOREIGN_KEY_CHECKS=1;

View File

@ -75,8 +75,8 @@ Please select a unique name for your macro.",
CreateEmbed::new() CreateEmbed::new()
.title("Macro Recording Started") .title("Macro Recording Started")
.description( .description(
"Run up to 5 commands to record in this macro. Use `/macro finish` to stop recording at any point. "Run up to 5 commands, or type `/macro finish` to stop at any point.
Any commands performed during recording won't take any actual action- they are only captured for the macro.", Any commands ran as part of recording will be inconsequential",
) )
.color(*THEME_COLOR), .color(*THEME_COLOR),
), ),

View File

@ -21,7 +21,7 @@ impl Recordable for Options {
" "
INSERT INTO todos (guild_id, channel_id, value) INSERT INTO todos (guild_id, channel_id, value)
VALUES ( VALUES (
(SELECT id FROM guilds WHERE guild = ?), ?,
(SELECT id FROM channels WHERE channel = ?), (SELECT id FROM channels WHERE channel = ?),
? ?
) )

View File

@ -23,7 +23,7 @@ pub async fn listener(
if is_new.unwrap_or(false) { if is_new.unwrap_or(false) {
let guild_id = guild.id.get().to_owned(); let guild_id = guild.id.get().to_owned();
sqlx::query!("INSERT IGNORE INTO guilds (guild) VALUES (?)", guild_id) sqlx::query!("INSERT IGNORE INTO guilds (id) VALUES (?)", guild_id)
.execute(&data.database) .execute(&data.database)
.await?; .await?;
@ -56,7 +56,7 @@ To stay up to date on the latest features and fixes, join our [Discord](https://
} }
FullEvent::GuildDelete { incomplete, .. } => { FullEvent::GuildDelete { incomplete, .. } => {
if !incomplete.unavailable { if !incomplete.unavailable {
let _ = sqlx::query!("DELETE FROM guilds WHERE guild = ?", incomplete.id.get()) let _ = sqlx::query!("DELETE FROM guilds WHERE id = ?", incomplete.id.get())
.execute(&data.database) .execute(&data.database)
.await; .await;
} }

View File

@ -1,6 +1,4 @@
use crate::consts::THEME_COLOR; use poise::{CommandInteractionType, CreateReply};
use poise::{serenity_prelude::CreateEmbed, CommandInteractionType, CreateReply};
use serenity::builder::CreateEmbedFooter;
use crate::{consts::MACRO_MAX_COMMANDS, models::command_macro::RecordedCommand, Context, Error}; use crate::{consts::MACRO_MAX_COMMANDS, models::command_macro::RecordedCommand, Context, Error};
@ -20,18 +18,7 @@ async fn macro_check(ctx: Context<'_>) -> bool {
.send( .send(
CreateReply::default() CreateReply::default()
.ephemeral(true) .ephemeral(true)
.embed(CreateEmbed::new() .content(format!("{} commands already recorded. Please use `/macro finish` to end recording.", MACRO_MAX_COMMANDS))
.title("💾 Currently recording macro")
.description(
format!("{} commands already recorded. Please use `/macro finish` to end recording.", MACRO_MAX_COMMANDS),
)
.footer(
CreateEmbedFooter::new(
"Any commands performed during recording won't take any actual action- they are only captured for the macro"
)
)
.color(*THEME_COLOR),
),
) )
.await; .await;
} else { } else {
@ -41,19 +28,9 @@ async fn macro_check(ctx: Context<'_>) -> bool {
let _ = ctx let _ = ctx
.send( .send(
CreateReply::default().ephemeral(true).embed( CreateReply::default()
CreateEmbed::new() .ephemeral(true)
.title("💾 Currently recording macro") .content("Command recorded to macro"),
.description(
"Command recorded. Use `/macro finish` to end recording.",
)
.footer(
CreateEmbedFooter::new(
"Any commands performed during recording won't take any actual action- they are only captured for the macro"
)
)
.color(*THEME_COLOR),
),
) )
.await; .await;
} }
@ -61,18 +38,8 @@ async fn macro_check(ctx: Context<'_>) -> bool {
None => { None => {
let _ = ctx let _ = ctx
.send( .send(
CreateReply::default().ephemeral(true).embed( CreateReply::default().ephemeral(true).content(
CreateEmbed::new() "This command is not supported in macros yet.",
.title("💾 Currently recording macro")
.description(
"This command is not supported in macros, so it hasn't been recorded. Use `/macro finish` to end recording.",
)
.footer(
CreateEmbedFooter::new(
"Any commands performed during recording won't take any actual action- they are only captured for the macro"
)
)
.color(*THEME_COLOR),
), ),
) )
.await; .await;
@ -107,7 +74,6 @@ async fn check_self_permissions(ctx: Context<'_>) -> bool {
return if permissions.send_messages() return if permissions.send_messages()
&& permissions.embed_links() && permissions.embed_links()
&& manage_webhooks && manage_webhooks
&& permissions.view_channel()
{ {
true true
} else { } else {
@ -115,13 +81,12 @@ async fn check_self_permissions(ctx: Context<'_>) -> bool {
.send(CreateReply::default().content(format!( .send(CreateReply::default().content(format!(
"The bot appears to be missing some permissions: "The bot appears to be missing some permissions:
{} **View Channels**
{} **Send Message** {} **Send Message**
{} **Embed Links** {} **Embed Links**
{} **Manage Webhooks** {} **Manage Webhooks**
Please check the bot's roles, and any channel overrides. Alternatively, giving the bot \"Administrator\" will bypass permission checks", Please check the bot's roles, and any channel overrides. Alternatively, giving the bot
if permissions.view_channel() { "" } else { "" }, \"Administrator\" will bypass permission checks",
if permissions.send_messages() { "" } else { "" }, if permissions.send_messages() { "" } else { "" },
if permissions.embed_links() { "" } else { "" }, if permissions.embed_links() { "" } else { "" },
if manage_webhooks { "" } else { "" }, if manage_webhooks { "" } else { "" },
@ -135,7 +100,9 @@ Please check the bot's roles, and any channel overrides. Alternatively, giving t
manage_webhooks manage_webhooks
} }
None => true, None => {
return true;
}
} }
} }

View File

@ -8,9 +8,7 @@ use crate::{consts::DEFAULT_AVATAR, Error};
pub struct ChannelData { pub struct ChannelData {
pub id: u32, pub id: u32,
pub channel: u64, pub channel: u64,
pub name: Option<String>,
pub nudge: i16, pub nudge: i16,
pub blacklisted: bool,
pub webhook_id: Option<u64>, pub webhook_id: Option<u64>,
pub webhook_token: Option<String>, pub webhook_token: Option<String>,
pub paused: bool, pub paused: bool,
@ -27,7 +25,7 @@ impl ChannelData {
if let Ok(c) = sqlx::query_as_unchecked!( if let Ok(c) = sqlx::query_as_unchecked!(
Self, Self,
" "
SELECT id, channel, name, nudge, blacklisted, webhook_id, webhook_token, paused, SELECT id, channel, nudge, webhook_id, webhook_token, paused,
paused_until paused_until
FROM channels FROM channels
WHERE channel = ? WHERE channel = ?
@ -39,15 +37,11 @@ impl ChannelData {
{ {
Ok(c) Ok(c)
} else { } else {
let props = channel.to_owned().guild().map(|g| (g.guild_id.get().to_owned(), g.name)); let guild_id = channel.to_owned().guild().map(|g| g.guild_id.get().to_owned());
let (guild_id, channel_name) =
if let Some((a, b)) = props { (Some(a), Some(b)) } else { (None, None) };
sqlx::query!( sqlx::query!(
"INSERT IGNORE INTO channels (channel, name, guild_id) VALUES (?, ?, (SELECT id FROM guilds WHERE guild = ?))", "INSERT IGNORE INTO channels (channel, guild_id) VALUES (?, ?)",
channel_id, channel_id,
channel_name,
guild_id guild_id
) )
.execute(&pool.clone()) .execute(&pool.clone())
@ -56,7 +50,7 @@ impl ChannelData {
Ok(sqlx::query_as_unchecked!( Ok(sqlx::query_as_unchecked!(
Self, Self,
" "
SELECT id, channel, name, nudge, blacklisted, webhook_id, webhook_token, paused, paused_until SELECT id, channel, nudge, webhook_id, webhook_token, paused, paused_until
FROM channels FROM channels
WHERE channel = ? WHERE channel = ?
", ",
@ -72,18 +66,14 @@ impl ChannelData {
" "
UPDATE channels UPDATE channels
SET SET
name = ?,
nudge = ?, nudge = ?,
blacklisted = ?,
webhook_id = ?, webhook_id = ?,
webhook_token = ?, webhook_token = ?,
paused = ?, paused = ?,
paused_until = ? paused_until = ?
WHERE id = ? WHERE id = ?
", ",
self.name,
self.nudge, self.nudge,
self.blacklisted,
self.webhook_id, self.webhook_id,
self.webhook_token, self.webhook_token,
self.paused, self.paused,

View File

@ -3,7 +3,7 @@ use sqlx::MySqlPool;
pub struct GuildData { pub struct GuildData {
pub ephemeral_confirmations: bool, pub ephemeral_confirmations: bool,
pub id: u32, pub guild_id: u64,
} }
impl GuildData { impl GuildData {
@ -13,7 +13,7 @@ impl GuildData {
) -> Result<Self, Box<dyn std::error::Error + Sync + Send>> { ) -> Result<Self, Box<dyn std::error::Error + Sync + Send>> {
if let Ok(c) = sqlx::query_as_unchecked!( if let Ok(c) = sqlx::query_as_unchecked!(
Self, Self,
"SELECT id, ephemeral_confirmations FROM guilds WHERE guild = ?", "SELECT ephemeral_confirmations, id as guild_id FROM guilds WHERE id = ?",
guild_id.get() guild_id.get()
) )
.fetch_one(pool) .fetch_one(pool)
@ -21,13 +21,13 @@ impl GuildData {
{ {
Ok(c) Ok(c)
} else { } else {
sqlx::query!("INSERT IGNORE INTO guilds (guild) VALUES (?)", guild_id.get()) sqlx::query!("INSERT IGNORE INTO guilds (id) VALUES (?)", guild_id.get())
.execute(&pool.clone()) .execute(&pool.clone())
.await?; .await?;
Ok(sqlx::query_as_unchecked!( Ok(sqlx::query_as_unchecked!(
Self, Self,
"SELECT id, ephemeral_confirmations FROM guilds WHERE guild = ?", "SELECT ephemeral_confirmations, id as guild_id FROM guilds WHERE id = ?",
guild_id.get() guild_id.get()
) )
.fetch_one(pool) .fetch_one(pool)
@ -39,7 +39,7 @@ impl GuildData {
sqlx::query!( sqlx::query!(
"UPDATE guilds SET ephemeral_confirmations = ? WHERE id = ?", "UPDATE guilds SET ephemeral_confirmations = ? WHERE id = ?",
self.ephemeral_confirmations, self.ephemeral_confirmations,
self.id self.guild_id
) )
.execute(pool) .execute(pool)
.await .await

View File

@ -68,16 +68,19 @@ impl Data {
guild_id: GuildId, guild_id: GuildId,
) -> Result<Vec<CommandMacro>, Error> { ) -> Result<Vec<CommandMacro>, Error> {
let rows = sqlx::query!( let rows = sqlx::query!(
"SELECT name, description, commands FROM command_macro WHERE guild_id = (SELECT id FROM guilds WHERE guild = ?)", "SELECT name, description, commands FROM command_macro WHERE guild_id = ?",
guild_id.get() guild_id.get()
) )
.fetch_all(&self.database) .fetch_all(&self.database)
.await?.iter().map(|row| CommandMacro { .await?
.iter()
.map(|row| CommandMacro {
guild_id, guild_id,
name: row.name.clone(), name: row.name.clone(),
description: row.description.clone(), description: row.description.clone(),
commands: serde_json::from_str(&row.commands.to_string()).unwrap(), commands: serde_json::from_str(&row.commands.to_string()).unwrap(),
}).collect(); })
.collect();
Ok(rows) Ok(rows)
} }

View File

@ -262,7 +262,7 @@ impl Reminder {
channels.id = reminders.channel_id channels.id = reminders.channel_id
WHERE WHERE
`status` = 'pending' AND `status` = 'pending' AND
channels.guild_id = (SELECT id FROM guilds WHERE guild = ?) channels.guild_id = ?
", ",
guild_id.get() guild_id.get()
) )

View File

@ -92,8 +92,6 @@ enum Error {
SQLx(sqlx::Error), SQLx(sqlx::Error),
#[allow(unused)] #[allow(unused)]
Serenity(serenity::Error), Serenity(serenity::Error),
#[allow(unused)]
MissingDiscordPermission(&'static str),
} }
pub async fn initialize( pub async fn initialize(

View File

@ -305,15 +305,7 @@ pub async fn edit_reminder(
Err(e) => { Err(e) => {
warn!("`create_database_channel` returned an error code: {:?}", e); warn!("`create_database_channel` returned an error code: {:?}", e);
// Provide more specific error messages based on the error type error.push("Failed to configure channel for reminders. Please check the bot permissions".to_string());
match e {
crate::web::Error::MissingDiscordPermission(permission) => {
error.push(format!("Please ensure the bot has the \"{}\" permission in the channel", permission));
}
_ => {
error.push("Failed to configure channel for reminders. Please check the bot permissions".to_string());
}
}
} }
} }
} }

View File

@ -31,7 +31,7 @@ pub async fn get_reminder_templates(
match sqlx::query_as_unchecked!( match sqlx::query_as_unchecked!(
ReminderTemplate, ReminderTemplate,
"SELECT * FROM reminder_template WHERE guild_id = (SELECT id FROM guilds WHERE guild = ?)", "SELECT * FROM reminder_template WHERE guild_id = ?",
id id
) )
.fetch_all(pool.inner()) .fetch_all(pool.inner())
@ -87,7 +87,7 @@ pub async fn delete_reminder_template(
check_authorization(cookies, ctx.inner(), id).await?; check_authorization(cookies, ctx.inner(), id).await?;
match sqlx::query!( match sqlx::query!(
"DELETE FROM reminder_template WHERE guild_id = (SELECT id FROM guilds WHERE guild = ?) AND id = ?", "DELETE FROM reminder_template WHERE guild_id = ? AND id = ?",
id, delete_reminder_template.id id, delete_reminder_template.id
) )
.fetch_all(pool.inner()) .fetch_all(pool.inner())

View File

@ -66,7 +66,7 @@ pub async fn create_todo(
" "
INSERT INTO todos (guild_id, channel_id, value) INSERT INTO todos (guild_id, channel_id, value)
VALUES ( VALUES (
(SELECT id FROM guilds WHERE guild = ?), ?,
(SELECT id FROM channels WHERE channel = ?), (SELECT id FROM channels WHERE channel = ?),
? ?
) )
@ -88,7 +88,7 @@ pub async fn create_todo(
" "
INSERT INTO todos (guild_id, channel_id, value) INSERT INTO todos (guild_id, channel_id, value)
VALUES ( VALUES (
(SELECT id FROM guilds WHERE guild = ?), ?,
NULL, NULL,
? ?
) )
@ -130,11 +130,9 @@ pub async fn get_todo(
channels.channel AS channel_id, channels.channel AS channel_id,
value value
FROM todos FROM todos
INNER JOIN guilds
ON guilds.id = todos.guild_id
LEFT JOIN channels LEFT JOIN channels
ON channels.id = todos.channel_id ON channels.id = todos.channel_id
WHERE guilds.guild = ? WHERE todos.guild_id = ?
", ",
id id
) )
@ -167,7 +165,7 @@ pub async fn update_todo(
" "
UPDATE todos UPDATE todos
SET value = ? SET value = ?
WHERE guild_id = (SELECT id FROM guilds WHERE guild = ?) WHERE guild_id = ?
AND id = ? AND id = ?
", ",
todo.value, todo.value,
@ -202,7 +200,7 @@ pub async fn delete_todo(
sqlx::query!( sqlx::query!(
" "
DELETE FROM todos DELETE FROM todos
WHERE guild_id = (SELECT id FROM guilds WHERE guild = ?) WHERE guild_id = ?
AND id = ? AND id = ?
", ",
guild_id, guild_id,

View File

@ -65,16 +65,7 @@ pub async fn create_reminder(
if let Err(e) = channel { if let Err(e) = channel {
warn!("`create_database_channel` returned an error code: {:?}", e); warn!("`create_database_channel` returned an error code: {:?}", e);
// Provide more specific error messages based on the error type return Err(json!({"error": "Failed to configure channel for reminders."}));
let error_msg = match e {
Error::MissingDiscordPermission(permission) => format!(
"Please ensure the bot has the \"{}\" permission in the channel",
permission
),
_ => "Failed to configure channel for reminders.".to_string(),
};
return Err(json!({"error": error_msg}));
} }
let channel = channel.unwrap(); let channel = channel.unwrap();

View File

@ -39,28 +39,31 @@ pub async fn export(
match sqlx::query_as_unchecked!( match sqlx::query_as_unchecked!(
ReminderTemplateCsv, ReminderTemplateCsv,
"SELECT "
name, SELECT
attachment, name,
attachment_name, attachment,
avatar, attachment_name,
content, avatar,
embed_author, content,
embed_author_url, embed_author,
embed_color, embed_author_url,
embed_description, embed_color,
embed_footer, embed_description,
embed_footer_url, embed_footer,
embed_image_url, embed_footer_url,
embed_thumbnail_url, embed_image_url,
embed_title, embed_thumbnail_url,
embed_fields, embed_title,
interval_seconds, embed_fields,
interval_days, interval_seconds,
interval_months, interval_days,
tts, interval_months,
username tts,
FROM reminder_template WHERE guild_id = (SELECT id FROM guilds WHERE guild = ?)", username
FROM reminder_template
WHERE guild_id = ?
",
id id
) )
.fetch_all(pool.inner()) .fetch_all(pool.inner())

View File

@ -38,10 +38,11 @@ pub async fn export(
match sqlx::query_as_unchecked!( match sqlx::query_as_unchecked!(
TodoCsv, TodoCsv,
"SELECT value, CONCAT('#', channels.channel) AS channel_id FROM todos "
SELECT value, CONCAT('#', channels.channel) AS channel_id FROM todos
LEFT JOIN channels ON todos.channel_id = channels.id LEFT JOIN channels ON todos.channel_id = channels.id
INNER JOIN guilds ON todos.guild_id = guilds.id INNER JOIN guilds ON todos.guild_id = ?
WHERE guilds.guild = ?", ",
id id
) )
.fetch_all(pool.inner()) .fetch_all(pool.inner())
@ -96,7 +97,7 @@ pub async fn import(
Ok(body) => { Ok(body) => {
let mut reader = csv::Reader::from_reader(body.as_slice()); let mut reader = csv::Reader::from_reader(body.as_slice());
let query_placeholder = "(?, (SELECT id FROM channels WHERE channel = ?), (SELECT id FROM guilds WHERE guild = ?))"; let query_placeholder = "(?, (SELECT id FROM channels WHERE channel = ?), ?)";
let mut query_params = vec![]; let mut query_params = vec![];
for result in reader.deserialize::<TodoCsv>() { for result in reader.deserialize::<TodoCsv>() {

View File

@ -10,7 +10,6 @@ use rocket::{
use rocket_dyn_templates::Template; use rocket_dyn_templates::Template;
use secrecy::ExposeSecret; use secrecy::ExposeSecret;
use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
use serenity::http::HttpError;
use serenity::{ use serenity::{
all::CacheHttp, all::CacheHttp,
builder::CreateWebhook, builder::CreateWebhook,
@ -374,12 +373,12 @@ pub(crate) async fn create_reminder(
reminder: CreateReminder, reminder: CreateReminder,
) -> JsonResult { ) -> JsonResult {
// check guild in db // check guild in db
match sqlx::query!("SELECT 1 as A FROM guilds WHERE guild = ?", guild_id.get()) match sqlx::query!("SELECT 1 as A FROM guilds WHERE id = ?", guild_id.get())
.fetch_one(transaction.executor()) .fetch_one(transaction.executor())
.await .await
{ {
Err(sqlx::Error::RowNotFound) => { Err(sqlx::Error::RowNotFound) => {
if sqlx::query!("INSERT INTO guilds (guild) VALUES (?)", guild_id.get()) if sqlx::query!("INSERT INTO guilds (id) VALUES (?)", guild_id.get())
.execute(transaction.executor()) .execute(transaction.executor())
.await .await
.is_err() .is_err()
@ -405,19 +404,9 @@ pub(crate) async fn create_reminder(
if let Err(e) = channel { if let Err(e) = channel {
warn!("`create_database_channel` returned an error code: {:?}", e); warn!("`create_database_channel` returned an error code: {:?}", e);
// Provide more specific error messages based on the error type return Err(
let error_msg = match e { json!({"error": "Failed to configure channel for reminders. Please check the bot permissions"}),
Error::MissingDiscordPermission(permission) => { );
format!(
"Please ensure the bot has the \"{}\" permission in the channel",
permission
)
}
_ => "Failed to configure channel for reminders. Please check the bot permissions"
.to_string(),
};
return Err(json!({"error": error_msg}));
} }
let channel = channel.unwrap(); let channel = channel.unwrap();
@ -670,7 +659,7 @@ pub(crate) async fn create_reminder_template(
interval_months, interval_months,
tts, tts,
username username
) VALUES ((SELECT id FROM guilds WHERE guild = ?), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
?, ?, ?, ?, ?, ?, ?)", ?, ?, ?, ?, ?, ?, ?)",
guild_id.get(), guild_id.get(),
name, name,
@ -727,36 +716,13 @@ async fn create_database_channel(
match row { match row {
Ok(row) => { Ok(row) => {
let is_dm = channel let is_dm =
.to_channel(&ctx) channel.to_channel(&ctx).await.map_err(|e| Error::Serenity(e))?.private().is_some();
.await
.map_err(|e| {
if let serenity::Error::Http(http_error) = &e {
if let HttpError::UnsuccessfulRequest(response) = http_error {
if response.error.code == 50001 {
return Error::MissingDiscordPermission("View Channel");
}
}
}
Error::Serenity(e)
})?
.private()
.is_some();
if !is_dm && (row.webhook_token.is_none() || row.webhook_id.is_none()) { if !is_dm && (row.webhook_token.is_none() || row.webhook_id.is_none()) {
let webhook = channel let webhook = channel
.create_webhook(&ctx, CreateWebhook::new("Reminder").avatar(&*DEFAULT_AVATAR)) .create_webhook(&ctx, CreateWebhook::new("Reminder").avatar(&*DEFAULT_AVATAR))
.await .await
.map_err(|e| match &e { .map_err(|e| Error::Serenity(e))?;
serenity::Error::Http(HttpError::UnsuccessfulRequest(response)) => {
match response.error.code {
50001 => Error::MissingDiscordPermission("View Channel"),
50013 => Error::MissingDiscordPermission("Manage Webhooks"),
_ => Error::Serenity(e),
}
}
_ => Error::Serenity(e),
})?;
let token = webhook.token.unwrap(); let token = webhook.token.unwrap();
@ -781,16 +747,7 @@ async fn create_database_channel(
let webhook = channel let webhook = channel
.create_webhook(&ctx, CreateWebhook::new("Reminder").avatar(&*DEFAULT_AVATAR)) .create_webhook(&ctx, CreateWebhook::new("Reminder").avatar(&*DEFAULT_AVATAR))
.await .await
.map_err(|e| match &e { .map_err(|e| Error::Serenity(e))?;
serenity::Error::Http(HttpError::UnsuccessfulRequest(response)) => {
match response.error.code {
50001 => Error::MissingDiscordPermission("View Channel"),
50013 => Error::MissingDiscordPermission("Manage Webhooks"),
_ => Error::Serenity(e),
}
}
_ => Error::Serenity(e),
})?;
let token = webhook.token.unwrap(); let token = webhook.token.unwrap();