fix bug related to a nonexistent row in the settings table

This commit is contained in:
2025-02-20 23:30:47 -05:00
parent 5115636f60
commit c19215113f
2 changed files with 32 additions and 12 deletions

View File

@@ -1,4 +1,4 @@
use crate::common::{Context, Error}; use crate::common::{self, Context, Error};
use poise::serenity_prelude::{Role, RoleId, GuildId}; use poise::serenity_prelude::{Role, RoleId, GuildId};
use sqlx::Row; use sqlx::Row;
@@ -6,9 +6,14 @@ use sqlx::Row;
async fn get_prefix(ctx: Context<'_>, guild: GuildId) -> Result<Option<String>, Error> { async fn get_prefix(ctx: Context<'_>, guild: GuildId) -> Result<Option<String>, Error> {
let db = &ctx.data().database; let db = &ctx.data().database;
let prefix: Option<String> = sqlx::query("SELECT prefix FROM settings WHERE guildid = $1") let prefix: Option<String> = match sqlx::query("SELECT prefix FROM settings WHERE guildid = $1")
.bind(guild.get() as i64) .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())) Ok(prefix.or(ctx.data().prefix.clone()))
} }
@@ -55,9 +60,14 @@ async fn prefix(ctx: Context<'_>, prefix: Option<String>) -> Result<(), Error> {
pub async fn get_positional_role(ctx: Context<'_>, guild: GuildId) -> Result<Option<RoleId>, Error> { pub async fn get_positional_role(ctx: Context<'_>, guild: GuildId) -> Result<Option<RoleId>, Error> {
let db = &ctx.data().database; let db = &ctx.data().database;
let role: Option<i64> = sqlx::query("SELECT positional_role FROM settings WHERE guildid = $1") let role: Option<i64> = match sqlx::query("SELECT positional_role FROM settings WHERE guildid = $1")
.bind(guild.get() as i64) .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))) Ok(role.map(|sf| RoleId::new(sf as u64)))
} }
@@ -90,7 +100,7 @@ pub async fn position(ctx: Context<'_>, role: Option<Role>) -> Result<(), Error>
tx.commit().await?; 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 => { None => {
let s = match get_positional_role(ctx, guild).await? { let s = match get_positional_role(ctx, guild).await? {
@@ -108,9 +118,14 @@ pub async fn position(ctx: Context<'_>, role: Option<Role>) -> Result<(), Error>
pub async fn get_hoist_selfroles(ctx: Context<'_>, guild: GuildId) -> Result<bool, Error> { pub async fn get_hoist_selfroles(ctx: Context<'_>, guild: GuildId) -> Result<bool, Error> {
let db = &ctx.data().database; let db = &ctx.data().database;
let hoist: Option<bool> = sqlx::query("SELECT hoist_selfroles FROM settings WHERE guildid = $1") let hoist: Option<bool> = match sqlx::query("SELECT hoist_selfroles FROM settings WHERE guildid = $1")
.bind(guild.get() as i64) .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)) Ok(hoist.unwrap_or(false))
} }

View File

@@ -62,9 +62,14 @@ async fn get_prefix(ctx: PartialContext<'_, Data, Error>) -> Result<Option<Strin
let db = &ctx.data.database; let db = &ctx.data.database;
let prefix: Option<String> = 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) .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())) Ok(prefix.or(ctx.data.prefix.clone()))
} }