diff options
| author | Brayden Banks <[email protected]> | 2018-02-17 23:38:10 -0800 |
|---|---|---|
| committer | Zeyla Hellyer <[email protected]> | 2018-05-27 14:28:01 -0700 |
| commit | 5b8120ea33331a55ccc5bb743fbf7303b76b978f (patch) | |
| tree | 9c3ca7d683241c35b83bdd9cbf52d8a395e56031 /src/framework/standard | |
| parent | Use `len_quoted()` for `min_args` and `max_args` (#282) (diff) | |
| download | serenity-5b8120ea33331a55ccc5bb743fbf7303b76b978f.tar.xz serenity-5b8120ea33331a55ccc5bb743fbf7303b76b978f.zip | |
Add `lacking_ownership` to `CreateHelpCommand`
Diffstat (limited to 'src/framework/standard')
| -rw-r--r-- | src/framework/standard/command.rs | 23 | ||||
| -rw-r--r-- | src/framework/standard/create_help_command.rs | 12 | ||||
| -rw-r--r-- | src/framework/standard/help_commands.rs | 80 | ||||
| -rw-r--r-- | src/framework/standard/mod.rs | 49 |
4 files changed, 91 insertions, 73 deletions
diff --git a/src/framework/standard/command.rs b/src/framework/standard/command.rs index 8c35451..cd7656c 100644 --- a/src/framework/standard/command.rs +++ b/src/framework/standard/command.rs @@ -1,13 +1,13 @@ use client::Context; use model::{ channel::Message, + id::UserId, Permissions }; use std::{ - collections::HashMap, - fmt, - fmt::{Debug, Formatter}, - sync::Arc + collections::{HashMap, HashSet}, + fmt::{self, Debug, Formatter}, + sync::Arc, }; use utils::Colour; use super::{Args, Configuration, HelpBehaviour}; @@ -17,7 +17,7 @@ pub type Check = Fn(&mut Context, &Message, &mut Args, &CommandOptions) -> bool + Sync + 'static; -pub type HelpFunction = fn(&mut Context, &Message, &HelpOptions, HashMap<String, Arc<CommandGroup>>, &Args) +pub type HelpFunction = fn(&mut Context, &Message, &HelpOptions, HashMap<String, Arc<CommandGroup>>, HashSet<UserId>, &Args) -> Result<(), Error>; pub struct Help(pub HelpFunction, pub Arc<HelpOptions>); @@ -29,8 +29,8 @@ impl Debug for Help { } impl HelpCommand for Help { - fn execute(&self, c: &mut Context, m: &Message, ho: &HelpOptions,hm: HashMap<String, Arc<CommandGroup>>, a: &Args) -> Result<(), Error> { - (self.0)(c, m, ho, hm, a) + fn execute(&self, c: &mut Context, m: &Message, ho: &HelpOptions, gs: HashMap<String, Arc<CommandGroup>>, os: HashSet<UserId>, a: &Args) -> Result<(), Error> { + (self.0)(c, m, ho, gs, os, a) } } @@ -179,6 +179,8 @@ pub struct HelpOptions { pub lacking_role: HelpBehaviour, /// If a user lacks permissions, this will treat how these commands will be displayed. pub lacking_permissions: HelpBehaviour, + /// If a user isn't the owner, this will treat how these commands will be displayed. + pub lacking_ownership: HelpBehaviour, /// If a user is using the help-command in a channel where a command is not available, /// this behaviour will be executed. pub wrong_channel: HelpBehaviour, @@ -189,7 +191,7 @@ pub struct HelpOptions { } pub trait HelpCommand: Send + Sync + 'static { - fn execute(&self, &mut Context, &Message, &HelpOptions, HashMap<String, Arc<CommandGroup>>, &Args) -> Result<(), Error>; + fn execute(&self, &mut Context, &Message, &HelpOptions, HashMap<String, Arc<CommandGroup>>, HashSet<UserId>, &Args) -> Result<(), Error>; fn options(&self) -> Arc<CommandOptions> { Arc::clone(&DEFAULT_OPTIONS) @@ -197,8 +199,8 @@ pub trait HelpCommand: Send + Sync + 'static { } impl HelpCommand for Arc<HelpCommand> { - fn execute(&self, c: &mut Context, m: &Message, ho: &HelpOptions, hm: HashMap<String, Arc<CommandGroup>>, a: &Args) -> Result<(), Error> { - (**self).execute(c, m, ho, hm, a) + fn execute(&self, c: &mut Context, m: &Message, ho: &HelpOptions, gs: HashMap<String, Arc<CommandGroup>>, os: HashSet<UserId>, a: &Args) -> Result<(), Error> { + (**self).execute(c, m, ho, gs, os, a) } } @@ -225,6 +227,7 @@ impl Default for HelpOptions { striked_commands_tip_in_guild: Some(String::new()), lacking_role: HelpBehaviour::Strike, lacking_permissions: HelpBehaviour::Strike, + lacking_ownership: HelpBehaviour::Hide, wrong_channel: HelpBehaviour::Strike, embed_error_colour: Colour::dark_red(), embed_success_colour: Colour::rosewater(), diff --git a/src/framework/standard/create_help_command.rs b/src/framework/standard/create_help_command.rs index 12a0b55..08fc6e9 100644 --- a/src/framework/standard/create_help_command.rs +++ b/src/framework/standard/create_help_command.rs @@ -20,7 +20,6 @@ use std::{ pub struct CreateHelpCommand(pub HelpOptions, pub HelpFunction); impl CreateHelpCommand { - /// Sets a message displaying if input could not be found /// but a similar command is available. /// @@ -141,7 +140,7 @@ impl CreateHelpCommand { self } - /// Sets how a command requiring permission, that a user is lacking, + /// Sets how a command requiring permissions, that a user is lacking, /// shall be appear in the help-menu. pub fn lacking_permissions(mut self, behaviour: HelpBehaviour) -> Self { self.0.lacking_permissions = behaviour; @@ -149,6 +148,14 @@ impl CreateHelpCommand { self } + /// Sets how a command requiring ownership, that a user is lacking, + /// shall be appear in the help-menu. + pub fn lacking_ownership(mut self, behaviour: HelpBehaviour) -> Self { + self.0.lacking_ownership = behaviour; + + self + } + /// Sets how a command requiring to be sent in either via DM /// or a guild should be treated in the help-menu. pub fn wrong_channel(mut self, behaviour: HelpBehaviour) -> Self { @@ -264,7 +271,6 @@ impl CreateHelpCommand { /// If `Some(String)` was set as `striked_commands_tip` and the `String` is empty, /// the creator will substitute content based on the `HelpBehaviour`-settings. pub(crate) fn finish(mut self) -> Arc<Help> { - if &self.0.striked_commands_tip_in_dm == &Some(String::new()) { self.0.striked_commands_tip_in_dm = self.produce_strike_text("direct messages"); } diff --git a/src/framework/standard/help_commands.rs b/src/framework/standard/help_commands.rs index 28adb9f..8c0e1b4 100644 --- a/src/framework/standard/help_commands.rs +++ b/src/framework/standard/help_commands.rs @@ -28,13 +28,13 @@ use client::Context; use framework::standard::{has_correct_roles, has_correct_permissions}; use model::{ channel::Message, - id::ChannelId + id::{ChannelId, UserId}, }; use std::{ - collections::HashMap, + collections::{HashMap, HashSet}, + fmt::Write, hash::BuildHasher, sync::Arc, - fmt::Write }; use super::command::InternalCommand; use super::{ @@ -112,6 +112,7 @@ pub fn with_embeds<H: BuildHasher>( msg: &Message, help_options: &HelpOptions, groups: HashMap<String, Arc<CommandGroup>, H>, + owners: HashSet<UserId>, args: &Args ) -> Result<(), CommandError> { if !args.is_empty() { @@ -259,6 +260,8 @@ pub fn with_embeds<H: BuildHasher>( let cmd = &commands[name]; let cmd = cmd.options(); + let mut display = HelpBehaviour::Nothing; + if !cmd.dm_only && !cmd.guild_only || cmd.dm_only && msg.is_private() || cmd.guild_only && !msg.is_private() { if cmd.help_available && has_correct_permissions(&cmd, msg) { @@ -268,61 +271,33 @@ pub fn with_embeds<H: BuildHasher>( if let Some(member) = guild.members.get(&msg.author.id) { - if has_correct_roles(&cmd, &guild, &member) { - let _ = write!(desc, "`{}`\n", name); - has_commands = true; - } else { - match help_options.lacking_role { - HelpBehaviour::Strike => { - let name = format!("~~`{}`~~", &name); - let _ = write!(desc, "{}\n", name); - has_commands = true; - }, - HelpBehaviour::Nothing => { - let _ = write!(desc, "`{}`\n", name); - has_commands = true; - }, - HelpBehaviour::Hide => { - continue; - }, - } + if !has_correct_roles(&cmd, &guild, &member) { + display = help_options.lacking_role; } } - } else { - let _ = write!(desc, "`{}`\n", name); - has_commands = true; } } else { - match help_options.lacking_permissions { - HelpBehaviour::Strike => { - let name = format!("~~`{}`~~", &name); - let _ = write!(desc, "{}\n", name); - has_commands = true; - }, - HelpBehaviour::Nothing => { - let _ = write!(desc, "`{}`\n", name); - has_commands = true; - }, - HelpBehaviour::Hide => { - continue; - }, - } + display = help_options.lacking_permissions; } } else { - match help_options.wrong_channel { - HelpBehaviour::Strike => { - let name = format!("~~`{}`~~", &name); - let _ = write!(desc, "{}\n", name); - has_commands = true; - }, - HelpBehaviour::Nothing => { - let _ = write!(desc, "`{}`\n", name); - has_commands = true; - }, - HelpBehaviour::Hide => { - continue; - }, - } + display = help_options.wrong_channel; + } + + if cmd.owners_only && !owners.contains(&msg.author.id) { + display += help_options.lacking_ownership; + } + + match display { + HelpBehaviour::Strike => { + let name = format!("~~`{}`~~", &name); + let _ = write!(desc, "{}\n", name); + has_commands = true; + }, + HelpBehaviour::Nothing => { + let _ = write!(desc, "`{}`\n", name); + has_commands = true; + }, + HelpBehaviour::Hide => {} } } @@ -361,6 +336,7 @@ pub fn plain<H: BuildHasher>( msg: &Message, help_options: &HelpOptions, groups: HashMap<String, Arc<CommandGroup>, H>, + _owners: HashSet<UserId>, args: &Args ) -> Result<(), CommandError> { if !args.is_empty() { diff --git a/src/framework/standard/mod.rs b/src/framework/standard/mod.rs index 6a7237d..c9e6bcb 100644 --- a/src/framework/standard/mod.rs +++ b/src/framework/standard/mod.rs @@ -1,4 +1,3 @@ - pub mod help_commands; mod command; @@ -42,7 +41,8 @@ use self::command::{AfterHook, BeforeHook, UnrecognisedCommandHook}; use std::{ collections::HashMap, default::Default, - sync::Arc + ops, + sync::Arc, }; use super::Framework; use threadpool::ThreadPool; @@ -1056,6 +1056,7 @@ impl Framework for StandardFramework { if let Some(help) = help { let groups = self.groups.clone(); + let owners = self.configuration.owners.clone(); threadpool.execute(move || { if let Some(before) = before { @@ -1065,7 +1066,7 @@ impl Framework for StandardFramework { } } - let result = (help.0)(&mut context, &message, &help.1, groups, &args); + let result = (help.0)(&mut context, &message, &help.1, groups, owners, &args); if let Some(after) = after { (after)(&mut context, &message, &built, result); @@ -1157,11 +1158,11 @@ pub fn has_correct_roles(cmd: &Arc<CommandOptions>, guild: &Guild, member: &Memb } /// Describes the behaviour the help-command shall execute once it encounters -/// a command which the user or command fails to meet following criteria : -/// Lacking required permissions to execute the command. -/// Lacking required roles to execute the command. -/// The command can't be used in the current channel (as in `DM only` or `guild only`). -#[derive(PartialEq, Debug)] +/// a command which the user or command fails to meet following criteria: +/// - Lacking required permissions to execute the command. +/// - Lacking required roles to execute the command. +/// - The command can't be used in the current channel (as in "DM only" or "guild only"). +#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)] pub enum HelpBehaviour { /// Strikes a command by applying `~~{comand_name}~~`. Strike, @@ -1180,3 +1181,35 @@ impl fmt::Display for HelpBehaviour { } } } + +/// Compares two help behaviours and merges them, preserving stronger behaviour +/// (`Hide` being stronger than `Strike`, and `Strike` being stronger than +/// `Nothing`). +impl ops::Add for HelpBehaviour { + type Output = HelpBehaviour; + fn add(self, other: HelpBehaviour) -> HelpBehaviour { + match self { + HelpBehaviour::Strike => match other { + HelpBehaviour::Strike => HelpBehaviour::Strike, + HelpBehaviour::Nothing => HelpBehaviour::Strike, + HelpBehaviour::Hide => HelpBehaviour::Hide, + } + HelpBehaviour::Nothing => other, + HelpBehaviour::Hide => HelpBehaviour::Hide, + } + } +} + +impl ops::AddAssign for HelpBehaviour { + fn add_assign(&mut self, other: HelpBehaviour) { + match *self { + HelpBehaviour::Strike => match other { + HelpBehaviour::Strike => (), + HelpBehaviour::Nothing => *self = HelpBehaviour::Strike, + HelpBehaviour::Hide => (), + } + HelpBehaviour::Nothing => *self = other, + HelpBehaviour::Hide => (), + } + } +} |