aboutsummaryrefslogtreecommitdiff
path: root/src/ext/framework/mod.rs
diff options
context:
space:
mode:
authorIllia <[email protected]>2016-12-14 17:19:16 +0300
committerAustin Hellyer <[email protected]>2016-12-14 11:35:28 -0800
commita39647d3ba1650a4dd4c92bd40001959828000c7 (patch)
tree3065d93509b494622dac457aa8540982454b01c2 /src/ext/framework/mod.rs
parentRelease v0.1.2 (diff)
downloadserenity-a39647d3ba1650a4dd4c92bd40001959828000c7.tar.xz
serenity-a39647d3ba1650a4dd4c92bd40001959828000c7.zip
Framework blocks, disabled commands, improvements
Diffstat (limited to 'src/ext/framework/mod.rs')
-rw-r--r--src/ext/framework/mod.rs130
1 files changed, 99 insertions, 31 deletions
diff --git a/src/ext/framework/mod.rs b/src/ext/framework/mod.rs
index 796f1a3..e08140d 100644
--- a/src/ext/framework/mod.rs
+++ b/src/ext/framework/mod.rs
@@ -67,7 +67,7 @@ pub use self::configuration::{AccountType, Configuration};
pub use self::create_command::CreateCommand;
pub use self::create_group::CreateGroup;
-use self::command::{AfterHook, Hook};
+use self::command::{AfterHook, BeforeHook};
use std::collections::HashMap;
use std::default::Default;
use std::sync::Arc;
@@ -163,7 +163,7 @@ macro_rules! command {
pub struct Framework {
configuration: Configuration,
groups: HashMap<String, Arc<CommandGroup>>,
- before: Option<Arc<Hook>>,
+ before: Option<Arc<BeforeHook>>,
buckets: HashMap<String, Bucket>,
after: Option<Arc<AfterHook>>,
/// Whether the framework has been "initialized".
@@ -321,43 +321,106 @@ impl Framework {
};
if let Some(command) = group.commands.get(&to_check) {
- if let Some(ref bucket_name) = command.bucket {
- let rate_limit = self.ratelimit_time(bucket_name, message.author.id.0);
-
- if rate_limit > 0 {
- if let Some(ref message) = self.configuration.rate_limit_message {
- let _ = context.say(
- &message.replace("%time%", &rate_limit.to_string()));
+ let is_owner = self.configuration.owners.contains(&message.author.id);
+ // Most of the checks don't apply to owners.
+ if !is_owner {
+ if !self.configuration.allow_dm {
+ if let Some(ref message) = self.configuration.no_dm_message {
+ let _ = context.say(message);
}
return;
}
- }
- if message.is_private() {
- if command.guild_only {
- if let Some(ref message) = self.configuration.no_guild_message {
+ if let Some(guild_id) = message.guild_id() {
+ if self.configuration.blocked_guilds.contains(&guild_id) {
+ if let Some(ref message) = self.configuration.blocked_guild_message {
+ let _ = context.say(message);
+ }
+
+ return;
+ }
+ }
+
+ #[cfg(feature="cache")]
+ {
+ if let Some(guild_id) = message.guild_id() {
+ if let Some(guild) = guild_id.find() {
+ if self.configuration.blocked_users.contains(&guild.owner_id) {
+ if let Some(ref message) = self.configuration.blocked_guild_message {
+ let _ = context.say(message);
+ }
+
+ return;
+ }
+ }
+ }
+ }
+
+ if self.configuration.blocked_users.contains(&message.author.id) {
+ if let Some(ref message) = self.configuration.blocked_user_message {
let _ = context.say(message);
}
return;
}
- } else if command.dm_only {
- if let Some(ref message) = self.configuration.no_dm_message {
- let _ = context.say(message);
+
+ if self.configuration.disabled_commands.contains(&to_check) ||
+ self.configuration.disabled_commands.contains(&built) {
+ if let Some(ref message) = self.configuration.command_disabled_message {
+ let msg = message.replace("%command%", &to_check);
+
+ let _ = context.say(&msg);
+ }
+
+ return;
}
- return;
- }
+ if let Some(ref bucket_name) = command.bucket {
+ let rate_limit = self.ratelimit_time(bucket_name, message.author.id.0);
+
+ if rate_limit > 0 {
+ if let Some(ref message) = self.configuration.rate_limit_message {
+ let msg = message.replace("%time%", &rate_limit.to_string());
- for check in &command.checks {
- if !(check)(&context, &message) {
- if let Some(ref message) = self.configuration.invalid_check_message {
+ let _ = context.say(&msg);
+ }
+
+ return;
+ }
+ }
+
+ if message.is_private() {
+ if command.guild_only {
+ if let Some(ref message) = self.configuration.no_guild_message {
+ let _ = context.say(message);
+ }
+
+ return;
+ }
+ } else if command.dm_only {
+ if let Some(ref message) = self.configuration.no_dm_message {
let _ = context.say(message);
}
- continue 'outer;
+ return;
}
+
+ for check in &command.checks {
+ if !(check)(&context, &message) {
+ if let Some(ref message) = self.configuration.invalid_check_message {
+ let _ = context.say(message);
+ }
+
+ continue 'outer;
+ }
+ }
+ } else if command.owners_only {
+ if let Some(ref message) = self.configuration.invalid_permission_message {
+ let _ = context.say(message);
+ }
+
+ return;
}
let before = self.before.clone();
@@ -377,9 +440,10 @@ impl Framework {
if let Some(x) = command.min_args {
if args.len() < x as usize {
if let Some(ref message) = self.configuration.not_enough_args_message {
- let _ = context.say(
- &message.replace("%min%", &x.to_string())
- .replace("%given%", &args.len().to_string()));
+ let msg = message.replace("%min%", &x.to_string())
+ .replace("%given%", &args.len().to_string());
+
+ let _ = context.say(&msg);
}
return;
@@ -389,16 +453,17 @@ impl Framework {
if let Some(x) = command.max_args {
if args.len() > x as usize {
if let Some(ref message) = self.configuration.too_many_args_message {
- let _ = context.say(
- &message.replace("%max%", &x.to_string())
- .replace("%given%", &args.len().to_string()));
+ let msg = message.replace("%max%", &x.to_string())
+ .replace("%given%", &args.len().to_string());
+
+ let _ = context.say(&msg);
}
return;
}
}
- if !command.required_permissions.is_empty() {
+ if !is_owner && !command.required_permissions.is_empty() {
let mut permissions_fulfilled = false;
if let Some(member) = message.get_member() {
@@ -424,7 +489,9 @@ impl Framework {
thread::spawn(move || {
if let Some(before) = before {
- (before)(&context, &message, &built);
+ if !is_owner && !(before)(&context, &message, &built) {
+ return;
+ }
}
let result = match command.exec {
@@ -530,8 +597,9 @@ impl Framework {
}
/// Specify the function to be called prior to every command's execution.
+ /// If that function returns true, the command will be executed.
pub fn before<F>(mut self, f: F) -> Self
- where F: Fn(&Context, &Message, &String) + Send + Sync + 'static {
+ where F: Fn(&Context, &Message, &String) -> bool + Send + Sync + 'static {
self.before = Some(Arc::new(f));
self