more concise guild getting

This commit is contained in:
2025-05-17 23:31:48 -04:00
parent 6f1adcee5e
commit 1874058c45
8 changed files with 36 additions and 56 deletions

View File

@@ -1,4 +1,4 @@
use crate::common::{Context, Error}; use crate::common::{Context, Error, BigBirbError};
use crate::commands::settings; use crate::commands::settings;
use poise::serenity_prelude as serenity; use poise::serenity_prelude as serenity;
@@ -10,10 +10,7 @@ pub async fn ban(ctx: Context<'_>,
#[rest] #[rest]
reason: Option<String>) -> Result<(), Error> reason: Option<String>) -> Result<(), Error>
{ {
let Some(guild) = ctx.guild_id() else { let guild = ctx.guild_id().ok_or(BigBirbError::GuildOnly)?;
ctx.reply("This command must be ran within a guild.").await?;
return Ok(());
};
if let Some(role) = settings::get_banrole(ctx, guild).await? { if let Some(role) = settings::get_banrole(ctx, guild).await? {
let member = guild.member(&ctx, user.id).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)] #[poise::command(slash_command, prefix_command)]
pub async fn unban(ctx: Context<'_>, user: serenity::User) -> Result<(), Error> pub async fn unban(ctx: Context<'_>, user: serenity::User) -> Result<(), Error>
{ {
let Some(guild) = ctx.guild_id() else { let guild = ctx.guild_id().ok_or(BigBirbError::GuildOnly)?;
ctx.reply("This command must be ran within a guild.").await?;
return Ok(());
};
if let Some(role) = settings::get_banrole(ctx, guild).await? { if let Some(role) = settings::get_banrole(ctx, guild).await? {
let member = guild.member(&ctx, user.id).await?; let member = guild.member(&ctx, user.id).await?;

View File

@@ -1,5 +1,5 @@
use crate::common::{self, Context, Error}; use crate::common::{self, Context, Error, BigBirbError};
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use std::collections::HashMap; use std::collections::HashMap;
@@ -73,10 +73,7 @@ pub async fn color(ctx: Context<'_>,
Color::from_rgb(rgb.r, rgb.g, rgb.b) Color::from_rgb(rgb.r, rgb.g, rgb.b)
}; };
let Some(guild) = ctx.guild_id() else { let guild = ctx.guild_id().ok_or(BigBirbError::GuildOnly)?;
ctx.reply("This command must be ran within a guild.").await?;
return Ok(());
};
let user = ctx.author(); let user = ctx.author();

View File

@@ -1,14 +1,10 @@
use crate::common::{Context, Error}; use crate::common::{Context, Error, BigBirbError};
/// Remove and delete your personal role /// Remove and delete your personal role
#[poise::command(slash_command, prefix_command)] #[poise::command(slash_command, prefix_command)]
pub async fn disown(ctx: Context<'_>) -> Result<(), Error> { pub async fn disown(ctx: Context<'_>) -> Result<(), Error> {
let Some(guild) = ctx.guild_id() else { let guild = ctx.guild_id().ok_or(BigBirbError::GuildOnly)?;
ctx.reply("This command must be ran within a guild.").await?;
return Ok(());
};
let user = ctx.author(); let user = ctx.author();
let mut tx = ctx.data().database.begin().await?; let mut tx = ctx.data().database.begin().await?;

View File

@@ -1,15 +1,12 @@
use crate::common::{self, Context, Error}; use crate::common::{self, Context, Error, BigBirbError};
use poise::serenity_prelude::EditRole; use poise::serenity_prelude::EditRole;
/// Change the name of your personal role /// Change the name of your personal role
#[poise::command(slash_command, prefix_command)] #[poise::command(slash_command, prefix_command)]
pub async fn name(ctx: Context<'_>, #[rest] name: String) -> Result<(), Error> { pub async fn name(ctx: Context<'_>, #[rest] name: String) -> Result<(), Error> {
let Some(guild) = ctx.guild_id() else { let guild = ctx.guild_id().ok_or(BigBirbError::GuildOnly)?;
ctx.reply("This command must be ran within a guild.").await?;
return Ok(());
};
let user = ctx.author(); let user = ctx.author();
let mut tx = ctx.data().database.begin().await?; let mut tx = ctx.data().database.begin().await?;

View File

@@ -1,16 +1,12 @@
use crate::common::{self, Context, Error}; use crate::common::{self, Context, Error, BigBirbError};
use poise::serenity_prelude as serenity; use poise::serenity_prelude as serenity;
/// Register an existing role as a user's custom role. This deletes their current self role. /// 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")] #[poise::command(slash_command, prefix_command, required_permissions = "MANAGE_ROLES")]
pub async fn register(ctx: Context<'_>, user: serenity::User, role: serenity::Role) -> Result<(), Error> { pub async fn register(ctx: Context<'_>, user: serenity::User, role: serenity::Role) -> Result<(), Error> {
let Some(guild) = ctx.guild_id() else { let guild = ctx.guild_id().ok_or(BigBirbError::GuildOnly)?;
ctx.reply("This command must be ran within a guild.").await?;
return Ok(());
};
let mut tx = ctx.data().database.begin().await?; let mut tx = ctx.data().database.begin().await?;
if let Some(role) = super::get_user_role(user.id, guild, &mut *tx).await? { if let Some(role) = super::get_user_role(user.id, guild, &mut *tx).await? {

View File

@@ -1,15 +1,11 @@
use crate::common::{self, Context, Error}; use crate::common::{self, Context, Error, BigBirbError};
use poise::serenity_prelude as serenity; use poise::serenity_prelude as serenity;
#[poise::command(slash_command, prefix_command, required_permissions = "MANAGE_ROLES")] #[poise::command(slash_command, prefix_command, required_permissions = "MANAGE_ROLES")]
pub async fn remove(ctx: Context<'_>, user: serenity::User) -> Result<(), Error> { pub async fn remove(ctx: Context<'_>, user: serenity::User) -> Result<(), Error> {
let Some(guild) = ctx.guild_id() else { let guild = ctx.guild_id().ok_or(BigBirbError::GuildOnly)?;
ctx.reply("This command must be ran within a guild.").await?;
return Ok(());
};
let mut tx = ctx.data().database.begin().await?; let mut tx = ctx.data().database.begin().await?;
if let Some(role) = super::get_user_role(user.id, guild, &mut *tx).await? { if let Some(role) = super::get_user_role(user.id, guild, &mut *tx).await? {

View File

@@ -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 poise::serenity_prelude::{Role, RoleId, GuildId};
use sqlx::Row; use sqlx::Row;
@@ -20,10 +20,7 @@ async fn get_prefix(ctx: Context<'_>, guild: GuildId) -> Result<Option<String>,
#[poise::command(prefix_command, slash_command)] #[poise::command(prefix_command, slash_command)]
async fn prefix(ctx: Context<'_>, prefix: Option<String>) -> Result<(), Error> { async fn prefix(ctx: Context<'_>, prefix: Option<String>) -> Result<(), Error> {
let Some(guild) = ctx.guild_id() else { let guild = ctx.guild_id().ok_or(BigBirbError::GuildOnly)?;
ctx.reply("This command must be ran within a guild.").await?;
return Ok(());
};
match prefix { match prefix {
Some(prefix) => { Some(prefix) => {
@@ -71,11 +68,7 @@ pub async fn get_positional_role(ctx: Context<'_>, guild: GuildId) -> Result<Opt
#[poise::command(prefix_command, slash_command)] #[poise::command(prefix_command, slash_command)]
pub async fn position(ctx: Context<'_>, role: Option<Role>) -> Result<(), Error> { pub async fn position(ctx: Context<'_>, role: Option<Role>) -> Result<(), Error> {
let Some(guild) = ctx.guild_id() else { let guild = ctx.guild_id().ok_or(BigBirbError::GuildOnly)?;
ctx.reply("This command must be ran within a guild.").await?;
return Ok(());
};
let member = ctx.author_member().await.unwrap(); let member = ctx.author_member().await.unwrap();
if !member.permissions(ctx).iter().any(|p| p.manage_guild()) { 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<boo
#[poise::command(prefix_command, slash_command)] #[poise::command(prefix_command, slash_command)]
pub async fn hoist(ctx: Context<'_>, hoist: Option<bool>) -> Result<(), Error> { pub async fn hoist(ctx: Context<'_>, hoist: Option<bool>) -> Result<(), Error> {
let Some(guild) = ctx.guild_id() else { let guild = ctx.guild_id().ok_or(BigBirbError::GuildOnly)?;
ctx.reply("This command must be ran within a guild.").await?;
return Ok(());
};
match hoist { match hoist {
Some(hoist) => { Some(hoist) => {
@@ -186,11 +176,7 @@ pub async fn get_banrole(ctx: Context<'_>, guild: GuildId) -> Result<Option<Role
#[poise::command(prefix_command, slash_command)] #[poise::command(prefix_command, slash_command)]
pub async fn banrole(ctx: Context<'_>, role: Option<Role>) -> Result<(), Error> { pub async fn banrole(ctx: Context<'_>, role: Option<Role>) -> Result<(), Error> {
let Some(guild) = ctx.guild_id() else { let guild = ctx.guild_id().ok_or(BigBirbError::GuildOnly)?;
ctx.reply("This command must be ran within a guild.").await?;
return Ok(());
};
let member = ctx.author_member().await.unwrap(); let member = ctx.author_member().await.unwrap();
if !member.permissions(ctx).iter().any(|p| p.manage_guild()) { if !member.permissions(ctx).iter().any(|p| p.manage_guild()) {

View File

@@ -1,3 +1,4 @@
use std::{error, fmt};
use poise::serenity_prelude::GuildId; use poise::serenity_prelude::GuildId;
use poise::ReplyHandle; use poise::ReplyHandle;
use sqlx::{Pool, Postgres}; use sqlx::{Pool, Postgres};
@@ -21,3 +22,20 @@ pub async fn no_ping_reply<'a>(ctx: &'a Context<'_>, text: impl Into<String>) ->
.allowed_mentions(CreateAllowedMentions::new()) .allowed_mentions(CreateAllowedMentions::new())
).await?) ).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 {}