From 09d84218861eab17bf62b47ac1e7da1563e36be4 Mon Sep 17 00:00:00 2001 From: Peanutbother <6437182+peanutbother@users.noreply.github.com> Date: Wed, 30 Nov 2022 15:20:27 +0100 Subject: [PATCH] [Feature] Allow to bypass command checks for owners (#130) * introduce skip_checks_for_owner feature * fix skip_checks_for_owners * fix missing owner check for `skip_checks_for_owners` --- examples/framework_usage/main.rs | 3 +++ src/dispatch/common.rs | 11 +++++++++-- src/structs/framework_options.rs | 3 +++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/examples/framework_usage/main.rs b/examples/framework_usage/main.rs index 15dec2f398be..7e64adeb6404 100644 --- a/examples/framework_usage/main.rs +++ b/examples/framework_usage/main.rs @@ -134,6 +134,9 @@ async fn main() { Ok(true) }) }), + /// Enforce command checks even for owners (enforced by default) + /// Set to true to bypass checks, which is useful for testing + skip_checks_for_owners: false, event_handler: |_ctx, event, _framework, _data| { Box::pin(async move { println!("Got an event in event handler: {:?}", event.name()); diff --git a/src/dispatch/common.rs b/src/dispatch/common.rs index c5e64dd93ba5..526563d1ca93 100644 --- a/src/dispatch/common.rs +++ b/src/dispatch/common.rs @@ -77,6 +77,13 @@ async fn check_permissions_and_cooldown_single<'a, U, E>( ctx: crate::Context<'a, U, E>, cmd: &'a crate::Command, ) -> Result<(), crate::FrameworkError<'a, U, E>> { + // Skip command checks if `FrameworkOptions::skip_checks_for_owners` is set to true + if ctx.framework().options.skip_checks_for_owners + && ctx.framework().options().owners.contains(&ctx.author().id) + { + return Ok(()); + } + if cmd.owners_only && !ctx.framework().options().owners.contains(&ctx.author().id) { return Err(crate::FrameworkError::NotAnOwner { ctx }); } @@ -148,8 +155,8 @@ async fn check_permissions_and_cooldown_single<'a, U, E>( None => {} } - // Only continue if command checks returns true. First perform global checks, then command - // checks (if necessary) + // Only continue if command checks returns true + // First perform global checks, then command checks (if necessary) for check in Option::iter(&ctx.framework().options().command_check).chain(&cmd.checks) { match check(ctx).await { Ok(true) => {} diff --git a/src/structs/framework_options.rs b/src/structs/framework_options.rs index c08131e82274..92979bcf9fef 100644 --- a/src/structs/framework_options.rs +++ b/src/structs/framework_options.rs @@ -23,6 +23,8 @@ pub struct FrameworkOptions { /// If individual commands add their own check, both callbacks are run and must return true. #[derivative(Debug = "ignore")] pub command_check: Option) -> BoxFuture<'_, Result>>, + /// If set to true, skips command checks if command was issued by [`FrameworkOptions::owners`] + pub skip_checks_for_owners: bool, /// Default set of allowed mentions to use for all responses /// /// By default, user pings are allowed and role pings and everyone pings are filtered @@ -101,6 +103,7 @@ where pre_command: |_| Box::pin(async {}), post_command: |_| Box::pin(async {}), command_check: None, + skip_checks_for_owners: false, allowed_mentions: Some({ let mut f = serenity::CreateAllowedMentions::default(); // Only support direct user pings by default