From 1874058c45d1ca2328dbb64d88649df26e3543a6 Mon Sep 17 00:00:00 2001 From: minneelyyyy Date: Sat, 17 May 2025 23:31:48 -0400 Subject: [PATCH] more concise guild getting --- src/commands/administration/ban.rs | 12 +++--------- src/commands/self_roles/color.rs | 7 ++----- src/commands/self_roles/disown.rs | 8 ++------ src/commands/self_roles/name.rs | 7 ++----- src/commands/self_roles/register.rs | 8 ++------ src/commands/self_roles/remove.rs | 8 ++------ src/commands/settings.rs | 24 +++++------------------- src/common.rs | 18 ++++++++++++++++++ 8 files changed, 36 insertions(+), 56 deletions(-) diff --git a/src/commands/administration/ban.rs b/src/commands/administration/ban.rs index ce6fbc4..717bf97 100644 --- a/src/commands/administration/ban.rs +++ b/src/commands/administration/ban.rs @@ -1,4 +1,4 @@ -use crate::common::{Context, Error}; +use crate::common::{Context, Error, BigBirbError}; use crate::commands::settings; use poise::serenity_prelude as serenity; @@ -10,10 +10,7 @@ pub async fn ban(ctx: Context<'_>, #[rest] reason: Option) -> Result<(), Error> { - let Some(guild) = ctx.guild_id() else { - ctx.reply("This command must be ran within a guild.").await?; - return Ok(()); - }; + let guild = ctx.guild_id().ok_or(BigBirbError::GuildOnly)?; if let Some(role) = settings::get_banrole(ctx, guild).await? { let member = guild.member(&ctx, user.id).await?; @@ -32,10 +29,7 @@ pub async fn ban(ctx: Context<'_>, #[poise::command(slash_command, prefix_command)] pub async fn unban(ctx: Context<'_>, user: serenity::User) -> Result<(), Error> { - let Some(guild) = ctx.guild_id() else { - ctx.reply("This command must be ran within a guild.").await?; - return Ok(()); - }; + let guild = ctx.guild_id().ok_or(BigBirbError::GuildOnly)?; if let Some(role) = settings::get_banrole(ctx, guild).await? { let member = guild.member(&ctx, user.id).await?; diff --git a/src/commands/self_roles/color.rs b/src/commands/self_roles/color.rs index e8e93e5..4b0996b 100644 --- a/src/commands/self_roles/color.rs +++ b/src/commands/self_roles/color.rs @@ -1,5 +1,5 @@ -use crate::common::{self, Context, Error}; +use crate::common::{self, Context, Error, BigBirbError}; use once_cell::sync::Lazy; use std::collections::HashMap; @@ -73,10 +73,7 @@ pub async fn color(ctx: Context<'_>, Color::from_rgb(rgb.r, rgb.g, rgb.b) }; - let Some(guild) = ctx.guild_id() else { - ctx.reply("This command must be ran within a guild.").await?; - return Ok(()); - }; + let guild = ctx.guild_id().ok_or(BigBirbError::GuildOnly)?; let user = ctx.author(); diff --git a/src/commands/self_roles/disown.rs b/src/commands/self_roles/disown.rs index 1fbe8b3..8539ee2 100644 --- a/src/commands/self_roles/disown.rs +++ b/src/commands/self_roles/disown.rs @@ -1,14 +1,10 @@ -use crate::common::{Context, Error}; +use crate::common::{Context, Error, BigBirbError}; /// Remove and delete your personal role #[poise::command(slash_command, prefix_command)] pub async fn disown(ctx: Context<'_>) -> Result<(), Error> { - let Some(guild) = ctx.guild_id() else { - ctx.reply("This command must be ran within a guild.").await?; - return Ok(()); - }; - + let guild = ctx.guild_id().ok_or(BigBirbError::GuildOnly)?; let user = ctx.author(); let mut tx = ctx.data().database.begin().await?; diff --git a/src/commands/self_roles/name.rs b/src/commands/self_roles/name.rs index c0ee3c5..68ee214 100644 --- a/src/commands/self_roles/name.rs +++ b/src/commands/self_roles/name.rs @@ -1,15 +1,12 @@ -use crate::common::{self, Context, Error}; +use crate::common::{self, Context, Error, BigBirbError}; use poise::serenity_prelude::EditRole; /// Change the name of your personal role #[poise::command(slash_command, prefix_command)] pub async fn name(ctx: Context<'_>, #[rest] name: String) -> Result<(), Error> { - let Some(guild) = ctx.guild_id() else { - ctx.reply("This command must be ran within a guild.").await?; - return Ok(()); - }; + let guild = ctx.guild_id().ok_or(BigBirbError::GuildOnly)?; let user = ctx.author(); let mut tx = ctx.data().database.begin().await?; diff --git a/src/commands/self_roles/register.rs b/src/commands/self_roles/register.rs index 57140e6..359e4f7 100644 --- a/src/commands/self_roles/register.rs +++ b/src/commands/self_roles/register.rs @@ -1,16 +1,12 @@ -use crate::common::{self, Context, Error}; +use crate::common::{self, Context, Error, BigBirbError}; use poise::serenity_prelude as serenity; /// Register an existing role as a user's custom role. This deletes their current self role. #[poise::command(slash_command, prefix_command, required_permissions = "MANAGE_ROLES")] pub async fn register(ctx: Context<'_>, user: serenity::User, role: serenity::Role) -> Result<(), Error> { - let Some(guild) = ctx.guild_id() else { - ctx.reply("This command must be ran within a guild.").await?; - return Ok(()); - }; - + let guild = ctx.guild_id().ok_or(BigBirbError::GuildOnly)?; let mut tx = ctx.data().database.begin().await?; if let Some(role) = super::get_user_role(user.id, guild, &mut *tx).await? { diff --git a/src/commands/self_roles/remove.rs b/src/commands/self_roles/remove.rs index c0f941f..b0cbef3 100644 --- a/src/commands/self_roles/remove.rs +++ b/src/commands/self_roles/remove.rs @@ -1,15 +1,11 @@ -use crate::common::{self, Context, Error}; +use crate::common::{self, Context, Error, BigBirbError}; use poise::serenity_prelude as serenity; #[poise::command(slash_command, prefix_command, required_permissions = "MANAGE_ROLES")] pub async fn remove(ctx: Context<'_>, user: serenity::User) -> Result<(), Error> { - let Some(guild) = ctx.guild_id() else { - ctx.reply("This command must be ran within a guild.").await?; - return Ok(()); - }; - + let guild = ctx.guild_id().ok_or(BigBirbError::GuildOnly)?; let mut tx = ctx.data().database.begin().await?; if let Some(role) = super::get_user_role(user.id, guild, &mut *tx).await? { diff --git a/src/commands/settings.rs b/src/commands/settings.rs index 8ea13eb..27a89ec 100644 --- a/src/commands/settings.rs +++ b/src/commands/settings.rs @@ -1,4 +1,4 @@ -use crate::common::{self, Context, Error}; +use crate::common::{self, Context, Error, BigBirbError}; use poise::serenity_prelude::{Role, RoleId, GuildId}; use sqlx::Row; @@ -20,10 +20,7 @@ async fn get_prefix(ctx: Context<'_>, guild: GuildId) -> Result, #[poise::command(prefix_command, slash_command)] async fn prefix(ctx: Context<'_>, prefix: Option) -> Result<(), Error> { - let Some(guild) = ctx.guild_id() else { - ctx.reply("This command must be ran within a guild.").await?; - return Ok(()); - }; + let guild = ctx.guild_id().ok_or(BigBirbError::GuildOnly)?; match prefix { Some(prefix) => { @@ -71,11 +68,7 @@ pub async fn get_positional_role(ctx: Context<'_>, guild: GuildId) -> Result, role: Option) -> Result<(), Error> { - let Some(guild) = ctx.guild_id() else { - ctx.reply("This command must be ran within a guild.").await?; - return Ok(()); - }; - + let guild = ctx.guild_id().ok_or(BigBirbError::GuildOnly)?; let member = ctx.author_member().await.unwrap(); if !member.permissions(ctx).iter().any(|p| p.manage_guild()) { @@ -126,10 +119,7 @@ pub async fn get_hoist_selfroles(ctx: Context<'_>, guild: GuildId) -> Result, hoist: Option) -> Result<(), Error> { - let Some(guild) = ctx.guild_id() else { - ctx.reply("This command must be ran within a guild.").await?; - return Ok(()); - }; + let guild = ctx.guild_id().ok_or(BigBirbError::GuildOnly)?; match hoist { Some(hoist) => { @@ -186,11 +176,7 @@ pub async fn get_banrole(ctx: Context<'_>, guild: GuildId) -> Result, role: Option) -> Result<(), Error> { - let Some(guild) = ctx.guild_id() else { - ctx.reply("This command must be ran within a guild.").await?; - return Ok(()); - }; - + let guild = ctx.guild_id().ok_or(BigBirbError::GuildOnly)?; let member = ctx.author_member().await.unwrap(); if !member.permissions(ctx).iter().any(|p| p.manage_guild()) { diff --git a/src/common.rs b/src/common.rs index 17dcff1..46cc755 100644 --- a/src/common.rs +++ b/src/common.rs @@ -1,3 +1,4 @@ +use std::{error, fmt}; use poise::serenity_prelude::GuildId; use poise::ReplyHandle; use sqlx::{Pool, Postgres}; @@ -21,3 +22,20 @@ pub async fn no_ping_reply<'a>(ctx: &'a Context<'_>, text: impl Into) -> .allowed_mentions(CreateAllowedMentions::new()) ).await?) } + +#[derive(Debug, Clone, Copy)] +pub enum BigBirbError { + GuildOnly, +} + +impl fmt::Display for BigBirbError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let s = match self { + Self::GuildOnly => "This command must be run inside of a guild.", + }; + + write!(f, "{s}") + } +} + +impl error::Error for BigBirbError {}