Skip to content

Commit

Permalink
[Feature] Allow to bypass command checks for owners (#130)
Browse files Browse the repository at this point in the history
* introduce skip_checks_for_owner feature

* fix skip_checks_for_owners

* fix missing owner check for `skip_checks_for_owners`
  • Loading branch information
peanutbother authored Nov 30, 2022
1 parent a89c442 commit 09d8421
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
3 changes: 3 additions & 0 deletions examples/framework_usage/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
11 changes: 9 additions & 2 deletions src/dispatch/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ async fn check_permissions_and_cooldown_single<'a, U, E>(
ctx: crate::Context<'a, U, E>,
cmd: &'a crate::Command<U, E>,
) -> 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 });
}
Expand Down Expand Up @@ -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) => {}
Expand Down
3 changes: 3 additions & 0 deletions src/structs/framework_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ pub struct FrameworkOptions<U, E> {
/// If individual commands add their own check, both callbacks are run and must return true.
#[derivative(Debug = "ignore")]
pub command_check: Option<fn(crate::Context<'_, U, E>) -> BoxFuture<'_, Result<bool, E>>>,
/// 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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 09d8421

Please sign in to comment.