Skip to content

Commit

Permalink
Help-features display Aliases and list information for Aliases. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Lakelezz authored and arqunis committed Oct 12, 2017
1 parent 483b069 commit 0d1c0f1
Showing 1 changed file with 80 additions and 60 deletions.
140 changes: 80 additions & 60 deletions src/framework/standard/help_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use std::fmt::Write;
use super::command::InternalCommand;
use super::{Args, Command, CommandGroup, CommandOrAlias, CommandError};
use client::Context;
use model::{ChannelId, Guild, Member, Message};
use model::{ChannelId, Message};
use utils::Colour;
use framework::standard::{has_correct_roles, has_correct_permissions};

Expand All @@ -51,14 +51,25 @@ fn remove_aliases(cmds: &HashMap<String, CommandOrAlias>) -> HashMap<&String, &I
result
}

/// Checks whether a user is member of required roles
/// Checks whether a user is member of required roles
/// and given the required permissions.
pub fn has_all_requirements(cmd: &Command, guild: &Guild, member: &Member, msg: &Message) -> bool {
if cmd.allowed_roles.is_empty() {
has_correct_permissions(cmd, msg)
} else {
has_correct_roles(cmd, guild, member) && has_correct_permissions(cmd, msg)
pub fn has_all_requirements(cmd: &Command, msg: &Message) -> bool {
if let Some(guild) = msg.guild() {
let guild = guild.read().unwrap();

if let Some(member) = guild.members.get(&msg.author.id) {

if let Ok(permissions) = member.permissions() {

if cmd.allowed_roles.is_empty() {
return permissions.administrator() || has_correct_permissions(&cmd, &msg);
} else {
return permissions.administrator() || (has_correct_roles(&cmd, &guild, &member) && has_correct_permissions(cmd, msg));
}
}
}
}
false
}

/// Posts an embed showing each individual command group and its commands.
Expand Down Expand Up @@ -100,25 +111,31 @@ pub fn with_embeds(_: &mut Context,
if name == with_prefix || name == *command_name {
match *command {
CommandOrAlias::Command(ref cmd) => {
if !cmd.allowed_roles.is_empty() {
if let Some(guild) = msg.guild() {
let guild = guild.read().unwrap();
if let Some(member) = guild.members.get(&msg.author.id) {
if let Ok(permissions) = member.permissions() {
if !permissions.administrator() &&
!has_all_requirements(cmd, &guild, member, msg) {
break;
}
}
}
}
if has_all_requirements(&cmd, &msg) {
found = Some((command_name, cmd));
}
found = Some((command_name, cmd));
else {
break;
}
},
CommandOrAlias::Alias(ref name) => {
error_embed(&msg.channel_id, &format!("Did you mean \"{}\"?", name));
let actual_command = group.commands.get(name).unwrap();

return Ok(());
match *actual_command {
CommandOrAlias::Command(ref cmd) => {
if has_all_requirements(&cmd, &msg) {
found = Some((name, cmd));
}
else {
break;
}
},

CommandOrAlias::Alias(ref name) => {
let _ = msg.channel_id.say(&format!("Did you mean {:?}?", name));
return Ok(());
},
}
},
}
}
Expand All @@ -134,6 +151,7 @@ pub fn with_embeds(_: &mut Context,
let _ = msg.channel_id.send_message(|m| {
m.embed(|e| {
let mut embed = e.colour(Colour::rosewater()).title(command_name);

if let Some(ref desc) = command.desc {
embed = embed.description(desc);
}
Expand All @@ -156,6 +174,11 @@ pub fn with_embeds(_: &mut Context,
embed = embed.field(|f| f.name("Group").value(&group_name));
}

if !command.aliases.is_empty() {
let aliases = command.aliases.join(", ");
embed = embed.field(|f| f.name("Aliases").value(&aliases));
}

let available = if command.dm_only {
"Only in DM"
} else if command.guild_only {
Expand Down Expand Up @@ -208,21 +231,14 @@ pub fn with_embeds(_: &mut Context,
let cmd = &commands[name];

if cmd.help_available {
if let Some(guild) = msg.guild() {
let guild = guild.read().unwrap();
if let Some(member) = guild.members.get(&msg.author.id) {
if let Ok(permissions) = member.permissions() {
if cmd.help_available &&
(has_all_requirements(cmd, &guild, member, msg) ||
permissions.administrator()) {
let _ = write!(desc, "`{}`\n", name);
has_commands = true;
}
}
}

if cmd.help_available && has_all_requirements(&cmd, &msg) {
let _ = write!(desc, "`{}`\n", name);
has_commands = true;
}
}
}

if has_commands {
e = e.field(|f| f.name(group_name).value(&desc));
}
Expand Down Expand Up @@ -252,7 +268,6 @@ pub fn with_embeds(_: &mut Context,
/// client.with_framework(StandardFramework::new()
/// .command("help", |c| c.exec_help(help_commands::plain)));
/// ```
#[cfg_attr(feature = "cargo-clippy", allow(cyclomatic_complexity))]
pub fn plain(_: &mut Context,
msg: &Message,
groups: HashMap<String, Arc<CommandGroup>>,
Expand All @@ -274,24 +289,31 @@ pub fn plain(_: &mut Context,
if name == with_prefix || name == *command_name {
match *command {
CommandOrAlias::Command(ref cmd) => {
if !cmd.allowed_roles.is_empty() {
if let Some(guild) = msg.guild() {
let guild = guild.read().unwrap();
if let Some(member) = guild.members.get(&msg.author.id) {
if let Ok(permissions) = member.permissions() {
if !permissions.administrator() &&
!has_all_requirements(cmd, &guild, member, msg) {
break;
}
}
}
}
if has_all_requirements(&cmd, &msg) {
found = Some((command_name, cmd));
}
else {
break;
}
found = Some((command_name, cmd));
},
CommandOrAlias::Alias(ref name) => {
let _ = msg.channel_id.say(&format!("Did you mean {:?}?", name));
return Ok(());
let actual_command = group.commands.get(name).unwrap();

match *actual_command {
CommandOrAlias::Command(ref cmd) => {
if has_all_requirements(&cmd, &msg) {
found = Some((name, cmd));
}
else {
break;
}
},

CommandOrAlias::Alias(ref name) => {
let _ = msg.channel_id.say(&format!("Did you mean {:?}?", name));
return Ok(());
},
}
},
}
}
Expand All @@ -305,6 +327,11 @@ pub fn plain(_: &mut Context,

let mut result = format!("**{}**\n", command_name);

if !command.aliases.is_empty() {
let aliases = command.aliases.join("`, `");
let _ = write!(result, "**Aliases:** `{}`\n", aliases);
}

if let Some(ref desc) = command.desc {
let _ = write!(result, "**Description:** {}\n", desc);
}
Expand Down Expand Up @@ -362,16 +389,9 @@ pub fn plain(_: &mut Context,

for name in command_names {
let cmd = &commands[name];
if let Some(guild) = msg.guild() {
let guild = guild.read().unwrap();
if let Some(member) = guild.members.get(&msg.author.id) {
if let Ok(permissions) = member.permissions() {
if cmd.help_available &&
(permissions.administrator() || has_all_requirements(cmd, &guild, member, msg)) {
let _ = write!(group_help, "`{}` ", name);
}
}
}

if cmd.help_available && has_all_requirements(&cmd, &msg) {
let _ = write!(group_help, "`{}` ", name);
}
}

Expand Down

0 comments on commit 0d1c0f1

Please sign in to comment.