From 7937025a484955cc8d74fb10004ba8b49dcc2bb0 Mon Sep 17 00:00:00 2001 From: Lakelezz <12222135+Lakelezz@users.noreply.github.com> Date: Mon, 18 Jun 2018 20:14:57 +0200 Subject: Check if a command would be visible, if yes provide help for it. --- src/framework/standard/help_commands.rs | 54 ++++++++++++++++++++++++++++----- 1 file changed, 47 insertions(+), 7 deletions(-) (limited to 'src/framework') diff --git a/src/framework/standard/help_commands.rs b/src/framework/standard/help_commands.rs index 28adb9f..bf52e90 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, }; use std::{ collections::HashMap, hash::BuildHasher, sync::Arc, - fmt::Write + fmt::Write, }; use super::command::InternalCommand; use super::{ @@ -44,7 +44,7 @@ use super::{ HelpOptions, CommandOptions, CommandError, - HelpBehaviour + HelpBehaviour, }; use utils::Colour; @@ -88,6 +88,46 @@ pub fn has_all_requirements(cmd: &Arc, msg: &Message) -> bool { !cmd.guild_only } +/// Checks whether a command would be visible, takes permissions, channel sent in, +/// and roles into consideration. +/// +/// **Note**: A command is visible when it is either normally displayed or +/// strikethrough upon requested help by a user. +#[cfg(feature = "cache")] +pub fn is_command_hidden(command_options: &Arc, msg: &Message, help_options: &HelpOptions) -> bool { + if !command_options.dm_only && !command_options.guild_only + || command_options.dm_only && msg.is_private() + || command_options.guild_only && !msg.is_private() { + + if let Some(guild) = msg.guild() { + let guild = guild.read(); + + if let Some(member) = guild.members.get(&msg.author.id) { + + if command_options.help_available { + + if has_correct_permissions(command_options, msg) { + + if has_correct_roles(command_options, &guild, &member) { + return true; + } else { + return help_options.lacking_role != HelpBehaviour::Hide; + } + } else { + return help_options.lacking_permissions != HelpBehaviour::Hide; + } + } else { + return help_options.lacking_permissions != HelpBehaviour::Hide; + } + } + } + } else { + return help_options.wrong_channel != HelpBehaviour::Hide; + } + + return false; +} + /// Posts an embed showing each individual command group and its commands. /// /// # Examples @@ -130,7 +170,7 @@ pub fn with_embeds( if name == with_prefix || name == *command_name { match *command { CommandOrAlias::Command(ref cmd) => { - if has_all_requirements(&cmd.options(), msg) { + if is_command_hidden(&cmd.options(), msg, help_options) { found = Some((command_name, cmd)); } else { break; @@ -141,7 +181,7 @@ pub fn with_embeds( match *actual_command { CommandOrAlias::Command(ref cmd) => { - if has_all_requirements(&cmd.options(), msg) { + if is_command_hidden(&cmd.options(), msg, help_options) { found = Some((name, cmd)); } else { break; @@ -379,7 +419,7 @@ pub fn plain( if name == with_prefix || name == *command_name { match *command { CommandOrAlias::Command(ref cmd) => { - if has_all_requirements(&cmd.options(), msg) { + if is_command_hidden(&cmd.options(), msg, help_options) { found = Some((command_name, cmd)); } else { @@ -391,7 +431,7 @@ pub fn plain( match *actual_command { CommandOrAlias::Command(ref cmd) => { - if has_all_requirements(&cmd.options(), msg) { + if is_command_hidden(&cmd.options(), msg, help_options) { found = Some((name, cmd)); } else { -- cgit v1.2.3 From 6e1edde4a3fe27d0d90db7ea906ca5f115a2d5fb Mon Sep 17 00:00:00 2001 From: Lakelezz <12222135+Lakelezz@users.noreply.github.com> Date: Mon, 18 Jun 2018 20:26:16 +0200 Subject: Rename from `is_command_hidden` to `is_command_visible`. --- src/framework/standard/help_commands.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src/framework') diff --git a/src/framework/standard/help_commands.rs b/src/framework/standard/help_commands.rs index bf52e90..92d801b 100644 --- a/src/framework/standard/help_commands.rs +++ b/src/framework/standard/help_commands.rs @@ -94,7 +94,7 @@ pub fn has_all_requirements(cmd: &Arc, msg: &Message) -> bool { /// **Note**: A command is visible when it is either normally displayed or /// strikethrough upon requested help by a user. #[cfg(feature = "cache")] -pub fn is_command_hidden(command_options: &Arc, msg: &Message, help_options: &HelpOptions) -> bool { +pub fn is_command_visible(command_options: &Arc, msg: &Message, help_options: &HelpOptions) -> bool { if !command_options.dm_only && !command_options.guild_only || command_options.dm_only && msg.is_private() || command_options.guild_only && !msg.is_private() { @@ -170,7 +170,7 @@ pub fn with_embeds( if name == with_prefix || name == *command_name { match *command { CommandOrAlias::Command(ref cmd) => { - if is_command_hidden(&cmd.options(), msg, help_options) { + if is_command_visible(&cmd.options(), msg, help_options) { found = Some((command_name, cmd)); } else { break; @@ -181,7 +181,7 @@ pub fn with_embeds( match *actual_command { CommandOrAlias::Command(ref cmd) => { - if is_command_hidden(&cmd.options(), msg, help_options) { + if is_command_visible(&cmd.options(), msg, help_options) { found = Some((name, cmd)); } else { break; @@ -419,7 +419,7 @@ pub fn plain( if name == with_prefix || name == *command_name { match *command { CommandOrAlias::Command(ref cmd) => { - if is_command_hidden(&cmd.options(), msg, help_options) { + if is_command_visible(&cmd.options(), msg, help_options) { found = Some((command_name, cmd)); } else { @@ -431,7 +431,7 @@ pub fn plain( match *actual_command { CommandOrAlias::Command(ref cmd) => { - if is_command_hidden(&cmd.options(), msg, help_options) { + if is_command_visible(&cmd.options(), msg, help_options) { found = Some((name, cmd)); } else { -- cgit v1.2.3 From aeb89af4eff59bb3ea9eb7623685bf7ad7520496 Mon Sep 17 00:00:00 2001 From: Lakelezz <12222135+Lakelezz@users.noreply.github.com> Date: Mon, 18 Jun 2018 20:58:40 +0200 Subject: If no help is available, command is not visible thus return false. --- src/framework/standard/help_commands.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'src/framework') diff --git a/src/framework/standard/help_commands.rs b/src/framework/standard/help_commands.rs index 92d801b..c01325e 100644 --- a/src/framework/standard/help_commands.rs +++ b/src/framework/standard/help_commands.rs @@ -116,8 +116,6 @@ pub fn is_command_visible(command_options: &Arc, msg: &Message, } else { return help_options.lacking_permissions != HelpBehaviour::Hide; } - } else { - return help_options.lacking_permissions != HelpBehaviour::Hide; } } } @@ -125,7 +123,7 @@ pub fn is_command_visible(command_options: &Arc, msg: &Message, return help_options.wrong_channel != HelpBehaviour::Hide; } - return false; + return false } /// Posts an embed showing each individual command group and its commands. -- cgit v1.2.3