aboutsummaryrefslogtreecommitdiff
path: root/src/framework
diff options
context:
space:
mode:
authoracdenisSK <[email protected]>2017-11-23 14:20:06 +0100
committeracdenisSK <[email protected]>2017-11-23 14:21:18 +0100
commit4e20277de4f164705074ba41199e4530332056b3 (patch)
tree67b94a34c5e9d0ce188caac8f14581a6ea38cfb7 /src/framework
parentUpdate dependencies (diff)
downloadserenity-4e20277de4f164705074ba41199e4530332056b3.tar.xz
serenity-4e20277de4f164705074ba41199e4530332056b3.zip
Add `before`/`after` middleware to `Command`
Diffstat (limited to 'src/framework')
-rw-r--r--src/framework/standard/command.rs31
-rw-r--r--src/framework/standard/mod.rs6
2 files changed, 36 insertions, 1 deletions
diff --git a/src/framework/standard/command.rs b/src/framework/standard/command.rs
index 4ace064..7697b3b 100644
--- a/src/framework/standard/command.rs
+++ b/src/framework/standard/command.rs
@@ -84,7 +84,6 @@ impl Default for CommandGroup {
}
}
-
pub struct CommandOptions {
/// A set of checks to be called prior to executing the command. The checks
/// will short-circuit on the first check that returns `false`.
@@ -131,6 +130,12 @@ pub trait Command: Send + Sync + 'static {
/// Called when the command gets registered.
fn init(&self) {}
+
+ /// "before" middleware. Is called alongside the global middleware in the framework.
+ fn before(&self, &mut Context, &Message) -> bool { true }
+
+ /// "after" middleware. Is called alongside the global middleware in the framework.
+ fn after(&self, &mut Context, &Message, &Result<(), Error>) { }
}
impl Command for Arc<Command> {
@@ -141,6 +146,18 @@ impl Command for Arc<Command> {
fn options(&self) -> Arc<CommandOptions> {
(**self).options()
}
+
+ fn init(&self) {
+ (**self).init()
+ }
+
+ fn before(&self, c: &mut Context, m: &Message) -> bool {
+ (**self).before(c, m)
+ }
+
+ fn after(&self, c: &mut Context, m: &Message, res: &Result<(), Error>) {
+ (**self).after(c, m, res)
+ }
}
impl Command for Box<Command> {
@@ -151,6 +168,18 @@ impl Command for Box<Command> {
fn options(&self) -> Arc<CommandOptions> {
(**self).options()
}
+
+ fn init(&self) {
+ (**self).init()
+ }
+
+ fn before(&self, c: &mut Context, m: &Message) -> bool {
+ (**self).before(c, m)
+ }
+
+ fn after(&self, c: &mut Context, m: &Message, res: &Result<(), Error>) {
+ (**self).after(c, m, res)
+ }
}
impl<F> Command for F where F: Fn(&mut Context, &Message, Args) -> Result<(), Error>
diff --git a/src/framework/standard/mod.rs b/src/framework/standard/mod.rs
index 1660c44..95e0028 100644
--- a/src/framework/standard/mod.rs
+++ b/src/framework/standard/mod.rs
@@ -1009,8 +1009,14 @@ impl Framework for StandardFramework {
}
}
+ if !command.before(&mut context, &message) {
+ return;
+ }
+
let result = command.execute(&mut context, &message, args);
+ command.after(&mut context, &message, &result);
+
if let Some(after) = after {
(after)(&mut context, &message, &built, result);
}