Skip to content

Commit

Permalink
Copy some methods from Command to Group (#164)
Browse files Browse the repository at this point in the history
  • Loading branch information
Roughsketch authored and arqunis committed Sep 11, 2017
1 parent 97e84fe commit 8e1435f
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/client/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ pub struct Context {

impl Context {
/// Create a new Context to be passed to an event handler.
pub(crate) fn new(shard: Arc<Mutex<Shard>>, data: Arc<Mutex<ShareMap>>, handle: Handle) -> Context {
pub(crate) fn new(shard: Arc<Mutex<Shard>>,
data: Arc<Mutex<ShareMap>>,
handle: Handle)
-> Context {
Context {
data,
shard,
Expand Down
9 changes: 8 additions & 1 deletion src/framework/standard/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,17 @@ pub enum CommandType {
WithCommands(Box<Help>),
}

#[derive(Default)]
pub struct CommandGroup {
pub prefix: Option<String>,
pub commands: HashMap<String, CommandOrAlias>,
/// Some fields taken from Command
pub bucket: Option<String>,
pub required_permissions: Permissions,
pub allowed_roles: Vec<String>,
pub help_available: bool,
pub dm_only: bool,
pub guild_only: bool,
pub owners_only: bool,
}

/// Command struct used to store commands internally.
Expand Down
86 changes: 84 additions & 2 deletions src/framework/standard/create_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ pub use super::Args;
use std::default::Default;
use std::sync::Arc;
use client::Context;
use model::Message;
use model::{Message, Permissions};
use std::collections::HashMap;

/// Used to create command groups
///
Expand All @@ -26,10 +27,25 @@ use model::Message;
pub struct CreateGroup(pub CommandGroup);

impl CreateGroup {
fn build_command(&self) -> CreateCommand {
let mut cmd = CreateCommand(Command::default())
.required_permissions(self.0.required_permissions)
.dm_only(self.0.dm_only)
.guild_only(self.0.guild_only)
.help_available(self.0.help_available)
.owners_only(self.0.owners_only);

if let Some(ref bucket) = self.0.bucket {
cmd = cmd.bucket(&bucket);
}
cmd.0.allowed_roles = self.0.allowed_roles.clone();
cmd
}

/// Adds a command to group.
pub fn command<F>(mut self, command_name: &str, f: F) -> Self
where F: FnOnce(CreateCommand) -> CreateCommand {
let cmd = f(CreateCommand(Command::default())).0;
let cmd = f(self.build_command()).0;

for n in &cmd.aliases {
if let Some(ref prefix) = self.0.prefix {
Expand Down Expand Up @@ -81,4 +97,70 @@ impl CreateGroup {

self
}

/// Adds a ratelimit bucket.
pub fn bucket(mut self, bucket: &str) -> Self {
self.0.bucket = Some(bucket.to_owned());

self
}

/// Whether command can be used only privately or not.
pub fn dm_only(mut self, dm_only: bool) -> Self {
self.0.dm_only = dm_only;

self
}

/// Whether command can be used only in guilds or not.
pub fn guild_only(mut self, guild_only: bool) -> Self {
self.0.guild_only = guild_only;

self
}

/// Whether command should be displayed in help list or not, used by other commands.
pub fn help_available(mut self, help_available: bool) -> Self {
self.0.help_available = help_available;

self
}

/// Whether command can be used only privately or not.
pub fn owners_only(mut self, owners_only: bool) -> Self {
self.0.owners_only = owners_only;

self
}

/// The permissions that a user must have in the contextual channel in order
/// for the command to be processed.
pub fn required_permissions(mut self, permissions: Permissions) -> Self {
self.0.required_permissions = permissions;

self
}

/// Sets roles that are allowed to use the command.
pub fn allowed_roles(mut self, allowed_roles: Vec<&str>) -> Self {
self.0.allowed_roles = allowed_roles.iter().map(|x| x.to_string()).collect();

self
}
}

impl Default for CommandGroup {
fn default() -> CommandGroup {
CommandGroup {
prefix: None,
commands: HashMap::new(),
bucket: None,
required_permissions: Permissions::empty(),
dm_only: false,
guild_only: false,
help_available: true,
owners_only: false,
allowed_roles: Vec::new(),
}
}
}

0 comments on commit 8e1435f

Please sign in to comment.