aboutsummaryrefslogtreecommitdiff
path: root/src/framework/standard/help_commands.rs
diff options
context:
space:
mode:
authorLakelezz <[email protected]>2017-09-05 15:16:05 +0200
committeralex <[email protected]>2017-09-05 15:16:05 +0200
commitd925f926c0f9f5b8010a998570441258417fc89a (patch)
tree3496f5f6f8ef0310a91ed90a8366e0ecb555181e /src/framework/standard/help_commands.rs
parentmatch to map/? (diff)
downloadserenity-d925f926c0f9f5b8010a998570441258417fc89a.tar.xz
serenity-d925f926c0f9f5b8010a998570441258417fc89a.zip
Allow commands to be limited to certain roles (#157)
Diffstat (limited to 'src/framework/standard/help_commands.rs')
-rw-r--r--src/framework/standard/help_commands.rs70
1 files changed, 61 insertions, 9 deletions
diff --git a/src/framework/standard/help_commands.rs b/src/framework/standard/help_commands.rs
index f081e1a..c7004d5 100644
--- a/src/framework/standard/help_commands.rs
+++ b/src/framework/standard/help_commands.rs
@@ -29,7 +29,7 @@ use std::fmt::Write;
use super::command::InternalCommand;
use super::{Args, Command, CommandGroup, CommandOrAlias};
use client::Context;
-use model::{ChannelId, Message};
+use model::{ChannelId, Guild, Member, Message};
use utils::Colour;
fn error_embed(channel_id: &ChannelId, input: &str) {
@@ -50,6 +50,17 @@ fn remove_aliases(cmds: &HashMap<String, CommandOrAlias>) -> HashMap<&String, &I
result
}
+fn right_roles(cmd: &Command, guild: &Guild, member: &Member) -> bool {
+ if cmd.allowed_roles.len() > 0 {
+ cmd.allowed_roles
+ .iter()
+ .flat_map(|r| guild.role_by_name(&r))
+ .any(|g| member.roles.contains(&g.id))
+ } else {
+ true
+ }
+}
+
/// Posts an embed showing each individual command group and its commands.
///
/// # Examples
@@ -89,6 +100,19 @@ pub fn with_embeds(_: &mut Context,
if name == with_prefix || name == *command_name {
match *command {
CommandOrAlias::Command(ref cmd) => {
+ if cmd.allowed_roles.len() > 0 {
+ 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() &&
+ !right_roles(&cmd, &guild, &member) {
+ break;
+ }
+ }
+ }
+ }
+ }
found = Some((command_name, cmd));
},
CommandOrAlias::Alias(ref name) => {
@@ -188,17 +212,25 @@ pub fn with_embeds(_: &mut Context,
let cmd = &commands[name];
if cmd.help_available {
- let _ = write!(desc, "`{}`\n", name);
-
- has_commands = true;
+ 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 &&
+ (right_roles(&cmd, &guild, &member) ||
+ permissions.administrator()) {
+ let _ = write!(desc, "`{}`\n", name);
+ has_commands = true;
+ }
+ }
+ }
+ }
}
}
-
if has_commands {
e = e.field(|f| f.name(group_name).value(&desc));
}
}
-
e
})
});
@@ -245,6 +277,19 @@ pub fn plain(_: &mut Context,
if name == with_prefix || name == *command_name {
match *command {
CommandOrAlias::Command(ref cmd) => {
+ if cmd.allowed_roles.len() > 0 {
+ 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() &&
+ !right_roles(&cmd, &guild, &member) {
+ break;
+ }
+ }
+ }
+ }
+ }
found = Some((command_name, cmd));
},
CommandOrAlias::Alias(ref name) => {
@@ -322,9 +367,16 @@ pub fn plain(_: &mut Context,
for name in command_names {
let cmd = &commands[name];
-
- if cmd.help_available {
- let _ = write!(group_help, "`{}` ", 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() || right_roles(&cmd, &guild, &member)) {
+ let _ = write!(group_help, "`{}` ", name);
+ }
+ }
+ }
}
}