Add support for command permissions to the framework
This commit is contained in:
@@ -9,6 +9,7 @@ pub mod arguments;
|
|||||||
pub mod context;
|
pub mod context;
|
||||||
pub mod messages;
|
pub mod messages;
|
||||||
pub mod runner;
|
pub mod runner;
|
||||||
|
pub mod permissions;
|
||||||
|
|
||||||
#[derive(Debug, Error)]
|
#[derive(Debug, Error)]
|
||||||
pub enum CommandError {
|
pub enum CommandError {
|
||||||
@@ -37,6 +38,9 @@ pub trait Command<T>: Send + Sync + Debug {
|
|||||||
fn arguments(&self) -> &'static [Argument] {
|
fn arguments(&self) -> &'static [Argument] {
|
||||||
&[]
|
&[]
|
||||||
}
|
}
|
||||||
|
fn permissions(&self) -> &'static [Box<dyn permissions::Permission<T> + Send + Sync>] {
|
||||||
|
&[]
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns the command name + all its aliases.
|
/// Returns the command name + all its aliases.
|
||||||
///
|
///
|
||||||
|
|||||||
@@ -0,0 +1,10 @@
|
|||||||
|
use std::fmt::Debug;
|
||||||
|
|
||||||
|
use async_trait::async_trait;
|
||||||
|
|
||||||
|
use crate::framework::commands::{CommandError, context::CommandContext};
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
pub trait Permission<S: Send + Sync + 'static>: Debug {
|
||||||
|
async fn check(&self, ctx: &CommandContext<S>) -> Result<bool, CommandError>;
|
||||||
|
}
|
||||||
@@ -31,12 +31,21 @@ impl<T: Send + Sync + 'static> OnMessageHandler<T> for CommandRunner {
|
|||||||
});
|
});
|
||||||
|
|
||||||
if let Some(command) = command {
|
if let Some(command) = command {
|
||||||
command
|
let ctx = CommandContext::new(event, rt.clone(), &prefixes[0]);
|
||||||
.execute(
|
|
||||||
CommandContext::new(event, rt.clone(), &prefixes[0]),
|
for permission in command.permissions() {
|
||||||
parts.args,
|
if !permission
|
||||||
)
|
.check(&ctx)
|
||||||
.await?;
|
.await
|
||||||
|
.inspect_err(|e| log::error!("Permission check failed: {e}"))?
|
||||||
|
{
|
||||||
|
return Err(
|
||||||
|
format!("The check for permission {permission:?} failed").into()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
command.execute(ctx, parts.args).await?;
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user