aboutsummaryrefslogtreecommitdiff
path: root/src/ext/framework/command.rs
diff options
context:
space:
mode:
authorIllia <[email protected]>2016-12-13 21:26:29 +0200
committerzeyla <[email protected]>2016-12-13 11:26:29 -0800
commitdaf92eda815b8f539f6d759ab48cf7a70513915f (patch)
tree36145f5095e7af6fb725635dd104e9d9d3f0ea62 /src/ext/framework/command.rs
parentFix readme typo (diff)
downloadserenity-daf92eda815b8f539f6d759ab48cf7a70513915f.tar.xz
serenity-daf92eda815b8f539f6d759ab48cf7a70513915f.zip
Implement command groups and buckets
* Implement command groups * change to ref mut * Implement framework API. * Remove commands field * Make it all work * Make example use command groups * Requested changes * Implement adding buckets * Add ratelimit check function * Finish everything * Fix voice example * Actually fix it * Fix doc tests * Switch to result * Savage examples * Fix docs * Fixes * Accidental push * 👀 * Fix an example * fix some example * Small cleanup * Abstract ratelimit bucket logic
Diffstat (limited to 'src/ext/framework/command.rs')
-rw-r--r--src/ext/framework/command.rs33
1 files changed, 31 insertions, 2 deletions
diff --git a/src/ext/framework/command.rs b/src/ext/framework/command.rs
index 204c90c..c1e9197 100644
--- a/src/ext/framework/command.rs
+++ b/src/ext/framework/command.rs
@@ -6,9 +6,10 @@ use ::model::Permissions;
use std::collections::HashMap;
pub type Check = Fn(&Context, &Message) -> bool + Send + Sync + 'static;
-pub type Exec = Fn(&Context, &Message, Vec<String>) + Send + Sync + 'static;
-pub type Help = Fn(&Context, &Message, HashMap<String, Arc<Command>>, Vec<String>) + Send + Sync + 'static;
+pub type Exec = Fn(&Context, &Message, Vec<String>) -> Result<(), String> + Send + Sync + 'static;
+pub type Help = Fn(&Context, &Message, HashMap<String, Arc<CommandGroup>>, Vec<String>) -> Result<(), String> + Send + Sync + 'static;
pub type Hook = Fn(&Context, &Message, &String) + Send + Sync + 'static;
+pub type AfterHook = Fn(&Context, &Message, &String, Result<(), String>) + Send + Sync + 'static;
#[doc(hidden)]
pub type InternalCommand = Arc<Command>;
pub type PrefixCheck = Fn(&Context) -> Option<String> + Send + Sync + 'static;
@@ -21,6 +22,12 @@ pub enum CommandType {
WithCommands(Box<Help>),
}
+#[derive(Default)]
+pub struct CommandGroup {
+ pub prefix: Option<String>,
+ pub commands: HashMap<String, InternalCommand>,
+}
+
/// Command struct used to store commands internally.
pub struct Command {
/// A set of checks to be called prior to executing the command. The checks
@@ -28,6 +35,8 @@ pub struct Command {
pub checks: Vec<Box<Check>>,
/// Function called when the command is called.
pub exec: CommandType,
+ /// Ratelimit bucket.
+ pub bucket: Option<String>,
/// Command description, used by other commands.
pub desc: Option<String>,
/// Command usage schema, used by other commands.
@@ -48,6 +57,26 @@ pub struct Command {
pub guild_only: bool,
}
+impl Command {
+ pub fn new<F>(f: F) -> Self
+ where F: Fn(&Context, &Message, Vec<String>) -> Result<(), String> + Send + Sync + 'static {
+ Command {
+ checks: Vec::default(),
+ exec: CommandType::Basic(Box::new(f)),
+ desc: None,
+ usage: None,
+ use_quotes: false,
+ dm_only: false,
+ bucket: None,
+ guild_only: false,
+ help_available: true,
+ min_args: None,
+ max_args: None,
+ required_permissions: Permissions::empty(),
+ }
+ }
+}
+
pub fn positions(ctx: &Context, content: &str, conf: &Configuration) -> Option<Vec<usize>> {
if !conf.prefixes.is_empty() || conf.dynamic_prefix.is_some() {
// Find out if they were mentioned. If not, determine if the prefix