Make permissions checking and notification separate functions

This commit is contained in:
2025-08-18 23:45:52 -03:00
parent 0af585cf9e
commit cddda58aca
6 changed files with 21 additions and 21 deletions
+4
View File
@@ -7,4 +7,8 @@ use crate::framework::commands::{CommandError, context::CommandContext};
#[async_trait] #[async_trait]
pub trait Permission<S: Send + Sync + 'static>: Debug { pub trait Permission<S: Send + Sync + 'static>: Debug {
async fn check(&self, ctx: &CommandContext<S>) -> Result<bool, CommandError>; async fn check(&self, ctx: &CommandContext<S>) -> Result<bool, CommandError>;
async fn on_handle_denied(&self, _ctx: &CommandContext<S>) -> Result<(), CommandError> {
Ok(())
}
} }
+1
View File
@@ -39,6 +39,7 @@ impl<T: Send + Sync + 'static> OnMessageHandler<T> for CommandRunner {
.await .await
.inspect_err(|e| log::error!("Permission check failed: {e}"))? .inspect_err(|e| log::error!("Permission check failed: {e}"))?
{ {
permission.on_handle_denied(&ctx).await?;
return Ok(()); return Ok(());
} }
} }
+14 -10
View File
@@ -19,19 +19,23 @@ impl Permission<State> for NotBannedPermission {
.map_err(|_| CommandError::DatabaseQueryFailed)?; .map_err(|_| CommandError::DatabaseQueryFailed)?;
if incidents.iter().any(|i| i.is_active()) { if incidents.iter().any(|i| i.is_active()) {
ctx.send()
.content("❌ You've been banned from Payphone.")
.component(CodeOfConductButton, 1)
.component(NeedHelpButton, 1)
.await
.inspect_err(|e| {
log::error!("Failed to send message: {e}");
})
.map_err(|_| CommandError::MessageSendingFailed)?;
Ok(false) Ok(false)
} else { } else {
Ok(true) Ok(true)
} }
} }
async fn on_handle_denied(&self, ctx: &CommandContext<State>) -> Result<(), CommandError> {
ctx.send()
.content("❌ You've been banned from Payphone.")
.component(CodeOfConductButton, 1)
.component(NeedHelpButton, 1)
.await
.inspect_err(|e| {
log::error!("Failed to send message: {e}");
})
.map_err(|_| CommandError::MessageSendingFailed)?;
Ok(())
}
} }
+1
View File
@@ -18,3 +18,4 @@ pub mod about;
pub mod calls; pub mod calls;
pub mod gateway; pub mod gateway;
pub mod servers; pub mod servers;
pub mod moderation;
View File
+1 -11
View File
@@ -20,17 +20,7 @@ pub struct ServerModeratorPermission;
#[async_trait] #[async_trait]
impl Permission<State> for ServerModeratorPermission { impl Permission<State> for ServerModeratorPermission {
async fn check(&self, ctx: &CommandContext<State>) -> Result<bool, CommandError> { async fn check(&self, _ctx: &CommandContext<State>) -> Result<bool, CommandError> {
let member = ctx
.rt
.http
.guild_member(ctx.event.guild_id.unwrap(), ctx.event.author.id)
.await
.map_err(CommandError::DiscordAPIFailed)?
.model()
.await
.map_err(CommandError::DiscordDeserializationFailed)?;
Ok(true) Ok(true)
} }
} }