From c19215113fdaa658a90a87df63e31f4afb244707 Mon Sep 17 00:00:00 2001 From: minneelyyyy Date: Thu, 20 Feb 2025 23:30:47 -0500 Subject: [PATCH] fix bug related to a nonexistent row in the settings table --- src/commands/settings.rs | 35 +++++++++++++++++++++++++---------- src/main.rs | 9 +++++++-- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/src/commands/settings.rs b/src/commands/settings.rs index b4d3ada..1a2ed78 100644 --- a/src/commands/settings.rs +++ b/src/commands/settings.rs @@ -1,4 +1,4 @@ -use crate::common::{Context, Error}; +use crate::common::{self, Context, Error}; use poise::serenity_prelude::{Role, RoleId, GuildId}; use sqlx::Row; @@ -6,9 +6,14 @@ use sqlx::Row; async fn get_prefix(ctx: Context<'_>, guild: GuildId) -> Result, Error> { let db = &ctx.data().database; - let prefix: Option = sqlx::query("SELECT prefix FROM settings WHERE guildid = $1") + let prefix: Option = match sqlx::query("SELECT prefix FROM settings WHERE guildid = $1") .bind(guild.get() as i64) - .fetch_one(db).await?.get(0); + .fetch_one(db).await + { + Ok(r) => r.get(0), + Err(sqlx::Error::RowNotFound) => None, + Err(e) => return Err(Box::new(e)), + }; Ok(prefix.or(ctx.data().prefix.clone())) } @@ -38,9 +43,9 @@ async fn prefix(ctx: Context<'_>, prefix: Option) -> Result<(), Error> { .bind(guild.get() as i64) .bind(&prefix) .execute(&mut *tx).await?; - + tx.commit().await?; - + ctx.reply(format!("This server's custom prefix has been updated to `{prefix}`.")).await?; } None => { @@ -55,9 +60,14 @@ async fn prefix(ctx: Context<'_>, prefix: Option) -> Result<(), Error> { pub async fn get_positional_role(ctx: Context<'_>, guild: GuildId) -> Result, Error> { let db = &ctx.data().database; - let role: Option = sqlx::query("SELECT positional_role FROM settings WHERE guildid = $1") + let role: Option = match sqlx::query("SELECT positional_role FROM settings WHERE guildid = $1") .bind(guild.get() as i64) - .fetch_one(db).await?.get(0); + .fetch_one(db).await + { + Ok(r) => r.get(0), + Err(sqlx::Error::RowNotFound) => None, + Err(e) => return Err(Box::new(e)), + }; Ok(role.map(|sf| RoleId::new(sf as u64))) } @@ -90,7 +100,7 @@ pub async fn position(ctx: Context<'_>, role: Option) -> Result<(), Error> tx.commit().await?; - ctx.reply(format!("The bot will now place newly created self roles below `{role}`.")).await?; + common::no_ping_reply(&ctx, format!("The bot will now place newly created self roles below {role}.")).await?; } None => { let s = match get_positional_role(ctx, guild).await? { @@ -108,9 +118,14 @@ pub async fn position(ctx: Context<'_>, role: Option) -> Result<(), Error> pub async fn get_hoist_selfroles(ctx: Context<'_>, guild: GuildId) -> Result { let db = &ctx.data().database; - let hoist: Option = sqlx::query("SELECT hoist_selfroles FROM settings WHERE guildid = $1") + let hoist: Option = match sqlx::query("SELECT hoist_selfroles FROM settings WHERE guildid = $1") .bind(guild.get() as i64) - .fetch_one(db).await?.get(0); + .fetch_one(db).await + { + Ok(r) => r.get(0), + Err(sqlx::Error::RowNotFound) => None, + Err(e) => return Err(Box::new(e)), + }; Ok(hoist.unwrap_or(false)) } diff --git a/src/main.rs b/src/main.rs index 82f20a5..bc1c710 100644 --- a/src/main.rs +++ b/src/main.rs @@ -62,9 +62,14 @@ async fn get_prefix(ctx: PartialContext<'_, Data, Error>) -> Result = sqlx::query("SELECT prefix FROM settings WHERE guildid = $1") + let prefix = match sqlx::query("SELECT prefix FROM settings WHERE guildid = $1") .bind(guild.get() as i64) - .fetch_one(db).await?.get(0); + .fetch_one(db).await + { + Ok(r) => r.get(0), + Err(sqlx::Error::RowNotFound) => None, + Err(e) => return Err(Box::new(e)), + }; Ok(prefix.or(ctx.data.prefix.clone())) }