aboutsummaryrefslogtreecommitdiff
path: root/src/framework
diff options
context:
space:
mode:
authoracdenisSK <[email protected]>2017-08-24 15:26:49 +0200
committeracdenisSK <[email protected]>2017-08-24 16:36:01 +0200
commitb3a5bc89ad1c09290fb1c15ca3b36fe17c3796f3 (patch)
tree315e16f7b252d22b5f832302e722a85c9e6a9b6e /src/framework
parentAllow FromStr for User to use REST (#147) (diff)
downloadserenity-b3a5bc89ad1c09290fb1c15ca3b36fe17c3796f3.tar.xz
serenity-b3a5bc89ad1c09290fb1c15ca3b36fe17c3796f3.zip
Revamp `RwLock` usage in the lib
Also not quite sure if they goofed rustfmt or something, but its changes it did were a bit bizarre.
Diffstat (limited to 'src/framework')
-rw-r--r--src/framework/standard/buckets.rs6
-rw-r--r--src/framework/standard/command.rs5
-rw-r--r--src/framework/standard/configuration.rs48
-rw-r--r--src/framework/standard/create_command.rs11
-rw-r--r--src/framework/standard/create_group.rs11
-rw-r--r--src/framework/standard/help_commands.rs18
-rw-r--r--src/framework/standard/mod.rs82
7 files changed, 68 insertions, 113 deletions
diff --git a/src/framework/standard/buckets.rs b/src/framework/standard/buckets.rs
index f2c4486..4719b85 100644
--- a/src/framework/standard/buckets.rs
+++ b/src/framework/standard/buckets.rs
@@ -31,9 +31,9 @@ pub(crate) struct Bucket {
impl Bucket {
pub fn take(&mut self, user_id: u64) -> i64 {
let time = Utc::now().timestamp();
- let user = self.users.entry(user_id).or_insert_with(
- MemberRatelimit::default,
- );
+ let user = self.users
+ .entry(user_id)
+ .or_insert_with(MemberRatelimit::default);
if let Some((timespan, limit)) = self.ratelimit.limit {
if (user.tickets + 1) > limit {
diff --git a/src/framework/standard/command.rs b/src/framework/standard/command.rs
index 22a82ce..8c23ca0 100644
--- a/src/framework/standard/command.rs
+++ b/src/framework/standard/command.rs
@@ -6,10 +6,7 @@ use std::collections::HashMap;
pub type Check = Fn(&mut Context, &Message, &mut Args, &Arc<Command>) -> bool + 'static;
pub type Exec = Fn(&mut Context, &Message, Args) -> Result<(), String> + 'static;
-pub type Help = Fn(&mut Context,
- &Message,
- HashMap<String, Arc<CommandGroup>>,
- Args)
+pub type Help = Fn(&mut Context, &Message, HashMap<String, Arc<CommandGroup>>, Args)
-> Result<(), String>
+ 'static;
pub type BeforeHook = Fn(&mut Context, &Message, &str) -> bool + 'static;
diff --git a/src/framework/standard/configuration.rs b/src/framework/standard/configuration.rs
index 491bbc4..dd1c7e7 100644
--- a/src/framework/standard/configuration.rs
+++ b/src/framework/standard/configuration.rs
@@ -33,32 +33,19 @@ use model::{GuildId, Message, UserId};
/// [`Client`]: ../../client/struct.Client.html
/// [`Framework`]: struct.Framework.html
pub struct Configuration {
- #[doc(hidden)]
- pub allow_dm: bool,
- #[doc(hidden)]
- pub allow_whitespace: bool,
- #[doc(hidden)]
- pub blocked_guilds: HashSet<GuildId>,
- #[doc(hidden)]
- pub blocked_users: HashSet<UserId>,
- #[doc(hidden)]
- pub depth: usize,
- #[doc(hidden)]
- pub disabled_commands: HashSet<String>,
- #[doc(hidden)]
- pub dynamic_prefix: Option<Box<PrefixCheck>>,
- #[doc(hidden)]
- pub ignore_bots: bool,
- #[doc(hidden)]
- pub ignore_webhooks: bool,
- #[doc(hidden)]
- pub on_mention: Option<Vec<String>>,
- #[doc(hidden)]
- pub owners: HashSet<UserId>,
- #[doc(hidden)]
- pub prefixes: Vec<String>,
- #[doc(hidden)]
- pub delimiters: Vec<String>,
+ #[doc(hidden)] pub allow_dm: bool,
+ #[doc(hidden)] pub allow_whitespace: bool,
+ #[doc(hidden)] pub blocked_guilds: HashSet<GuildId>,
+ #[doc(hidden)] pub blocked_users: HashSet<UserId>,
+ #[doc(hidden)] pub depth: usize,
+ #[doc(hidden)] pub disabled_commands: HashSet<String>,
+ #[doc(hidden)] pub dynamic_prefix: Option<Box<PrefixCheck>>,
+ #[doc(hidden)] pub ignore_bots: bool,
+ #[doc(hidden)] pub ignore_webhooks: bool,
+ #[doc(hidden)] pub on_mention: Option<Vec<String>>,
+ #[doc(hidden)] pub owners: HashSet<UserId>,
+ #[doc(hidden)] pub prefixes: Vec<String>,
+ #[doc(hidden)] pub delimiters: Vec<String>,
}
impl Configuration {
@@ -269,8 +256,8 @@ impl Configuration {
if let Ok(current_user) = http::get_current_user() {
self.on_mention = Some(vec![
- format!("<@{}>", current_user.id), // Regular mention
- format!("<@!{}>", current_user.id) /* Nickname mention */,
+ format!("<@{}>", current_user.id), // Regular mention
+ format!("<@!{}>", current_user.id), // Nickname mention
]);
}
@@ -415,9 +402,8 @@ impl Configuration {
/// ```
pub fn delimiters(mut self, delimiters: Vec<&str>) -> Self {
self.delimiters.clear();
- self.delimiters.extend(
- delimiters.into_iter().map(|s| s.to_string()),
- );
+ self.delimiters
+ .extend(delimiters.into_iter().map(|s| s.to_string()));
self
}
diff --git a/src/framework/standard/create_command.rs b/src/framework/standard/create_command.rs
index 0a467e1..97bf092 100644
--- a/src/framework/standard/create_command.rs
+++ b/src/framework/standard/create_command.rs
@@ -11,9 +11,9 @@ pub struct CreateCommand(pub Command);
impl CreateCommand {
/// Adds multiple aliases.
pub fn batch_known_as(mut self, names: Vec<&str>) -> Self {
- self.0.aliases.extend(
- names.into_iter().map(|n| n.to_owned()),
- );
+ self.0
+ .aliases
+ .extend(names.into_iter().map(|n| n.to_owned()));
self
}
@@ -115,10 +115,7 @@ impl CreateCommand {
///
/// You can return `Err(string)` if there's an error.
pub fn exec_help<F>(mut self, f: F) -> Self
- where F: Fn(&mut Context,
- &Message,
- HashMap<String, Arc<CommandGroup>>,
- Args)
+ where F: Fn(&mut Context, &Message, HashMap<String, Arc<CommandGroup>>, Args)
-> Result<(), String>
+ 'static {
self.0.exec = CommandType::WithCommands(Box::new(f));
diff --git a/src/framework/standard/create_group.rs b/src/framework/standard/create_group.rs
index 013ba67..d533c6a 100644
--- a/src/framework/standard/create_group.rs
+++ b/src/framework/standard/create_group.rs
@@ -35,9 +35,7 @@ impl CreateGroup {
if let Some(ref prefix) = self.0.prefix {
self.0.commands.insert(
format!("{} {}", prefix, n.to_owned()),
- CommandOrAlias::Alias(
- format!("{} {}", prefix, command_name.to_string()),
- ),
+ CommandOrAlias::Alias(format!("{} {}", prefix, command_name.to_string())),
);
} else {
self.0.commands.insert(
@@ -61,10 +59,9 @@ impl CreateGroup {
where F: Fn(&mut Context, &Message, Args) -> Result<(), String> + Send + Sync + 'static {
let cmd = Arc::new(Command::new(f));
- self.0.commands.insert(
- command_name.to_owned(),
- CommandOrAlias::Command(cmd),
- );
+ self.0
+ .commands
+ .insert(command_name.to_owned(), CommandOrAlias::Command(cmd));
self
}
diff --git a/src/framework/standard/help_commands.rs b/src/framework/standard/help_commands.rs
index f081e1a..75c5b0b 100644
--- a/src/framework/standard/help_commands.rs
+++ b/src/framework/standard/help_commands.rs
@@ -116,19 +116,15 @@ pub fn with_embeds(_: &mut Context,
if let Some(ref usage) = command.usage {
embed = embed.field(|f| {
- f.name("Usage").value(
- &format!("`{} {}`", command_name, usage),
- )
+ f.name("Usage")
+ .value(&format!("`{} {}`", command_name, usage))
});
}
if let Some(ref example) = command.example {
embed = embed.field(|f| {
- f.name("Sample usage").value(&format!(
- "`{} {}`",
- command_name,
- example
- ))
+ f.name("Sample usage")
+ .value(&format!("`{} {}`", command_name, example))
});
}
@@ -297,10 +293,8 @@ pub fn plain(_: &mut Context,
}
}
- let _ = msg.channel_id.say(&format!(
- "**Error**: Command `{}` not found.",
- name
- ));
+ let _ = msg.channel_id
+ .say(&format!("**Error**: Command `{}` not found.", name));
return Ok(());
}
diff --git a/src/framework/standard/mod.rs b/src/framework/standard/mod.rs
index 0a29b59..3aaee92 100644
--- a/src/framework/standard/mod.rs
+++ b/src/framework/standard/mod.rs
@@ -25,6 +25,7 @@ use super::Framework;
use model::{ChannelId, GuildId, Message, UserId};
use model::permissions::Permissions;
use tokio_core::reactor::Handle;
+use internal::RwLockExt;
#[cfg(feature = "cache")]
use client::CACHE;
@@ -395,18 +396,15 @@ impl StandardFramework {
#[cfg(feature = "cache")]
fn is_blocked_guild(&self, message: &Message) -> bool {
if let Some(Channel::Guild(channel)) = CACHE.read().unwrap().channel(message.channel_id) {
- let guild_id = channel.read().unwrap().guild_id;
+ let guild_id = channel.with(|g| g.guild_id);
if self.configuration.blocked_guilds.contains(&guild_id) {
return true;
}
if let Some(guild) = guild_id.find() {
- return self.configuration.blocked_users.contains(
- &guild
- .read()
- .unwrap()
- .owner_id,
- );
+ return self.configuration
+ .blocked_users
+ .contains(&guild.with(|g| g.owner_id));
}
}
@@ -417,10 +415,8 @@ impl StandardFramework {
fn has_correct_permissions(&self, command: &Arc<Command>, message: &Message) -> bool {
if !command.required_permissions.is_empty() {
if let Some(guild) = message.guild() {
- let perms = guild.read().unwrap().permissions_for(
- message.channel_id,
- message.author.id,
- );
+ let perms = guild
+ .with(|g| g.permissions_for(message.channel_id, message.author.id));
return perms.contains(command.required_permissions);
}
@@ -450,8 +446,7 @@ impl StandardFramework {
let rate_limit = bucket.take(message.author.id.0);
match bucket.check {
Some(ref check) => {
- let apply =
- feature_cache! {{
+ let apply = feature_cache! {{
let guild_id = message.guild_id();
(check)(context, guild_id, message.channel_id, message.author.id)
} else {
@@ -462,10 +457,8 @@ impl StandardFramework {
return Some(DispatchError::RateLimited(rate_limit));
}
},
- None => {
- if rate_limit > 0i64 {
- return Some(DispatchError::RateLimited(rate_limit));
- }
+ None => if rate_limit > 0i64 {
+ return Some(DispatchError::RateLimited(rate_limit));
},
}
}
@@ -515,18 +508,19 @@ impl StandardFramework {
if command.owners_only {
Some(DispatchError::OnlyForOwners)
- } else if self.configuration.blocked_users.contains(
- &message.author.id,
- ) {
+ } else if self.configuration
+ .blocked_users
+ .contains(&message.author.id) {
Some(DispatchError::BlockedUser)
} else if self.configuration.disabled_commands.contains(to_check) {
Some(DispatchError::CommandDisabled(to_check.to_owned()))
} else if self.configuration.disabled_commands.contains(built) {
Some(DispatchError::CommandDisabled(built.to_owned()))
} else {
- let all_passed = command.checks.iter().all(|check| {
- check(&mut context, message, args, command)
- });
+ let all_passed = command
+ .checks
+ .iter()
+ .all(|check| check(&mut context, message, args, command));
if all_passed {
None
@@ -580,21 +574,16 @@ impl StandardFramework {
pub fn on<F, S>(mut self, command_name: S, f: F) -> Self
where F: Fn(&mut Context, &Message, Args) -> Result<(), String> + 'static, S: Into<String> {
{
- let ungrouped = self.groups.entry("Ungrouped".to_owned()).or_insert_with(
- || {
- Arc::new(CommandGroup::default())
- },
- );
+ let ungrouped = self.groups
+ .entry("Ungrouped".to_owned())
+ .or_insert_with(|| Arc::new(CommandGroup::default()));
if let Some(ref mut group) = Arc::get_mut(ungrouped) {
let name = command_name.into();
- group.commands.insert(
- name,
- CommandOrAlias::Command(
- Arc::new(Command::new(f)),
- ),
- );
+ group
+ .commands
+ .insert(name, CommandOrAlias::Command(Arc::new(Command::new(f))));
}
}
@@ -617,11 +606,9 @@ impl StandardFramework {
pub fn command<F, S>(mut self, command_name: S, f: F) -> Self
where F: FnOnce(CreateCommand) -> CreateCommand, S: Into<String> {
{
- let ungrouped = self.groups.entry("Ungrouped".to_owned()).or_insert_with(
- || {
- Arc::new(CommandGroup::default())
- },
- );
+ let ungrouped = self.groups
+ .entry("Ungrouped".to_owned())
+ .or_insert_with(|| Arc::new(CommandGroup::default()));
if let Some(ref mut group) = Arc::get_mut(ungrouped) {
let cmd = f(CreateCommand(Command::default())).0;
@@ -636,17 +623,15 @@ impl StandardFramework {
}
} else {
for v in &cmd.aliases {
- group.commands.insert(
- v.to_owned(),
- CommandOrAlias::Alias(name.clone()),
- );
+ group
+ .commands
+ .insert(v.to_owned(), CommandOrAlias::Alias(name.clone()));
}
}
- group.commands.insert(
- name,
- CommandOrAlias::Command(Arc::new(cmd)),
- );
+ group
+ .commands
+ .insert(name, CommandOrAlias::Command(Arc::new(cmd)));
}
}
@@ -859,8 +844,7 @@ impl Framework for StandardFramework {
for group in groups.values() {
let command_length = built.len();
- if let Some(&CommandOrAlias::Alias(ref points_to)) =
- group.commands.get(&built) {
+ if let Some(&CommandOrAlias::Alias(ref points_to)) = group.commands.get(&built) {
built = points_to.to_owned();
}