1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
pub use super::command::{Command, CommandGroup, CommandType, Error as CommandError};
pub(crate) use super::command::CommandOrAlias;
pub use super::create_command::CreateCommand;
pub use super::Args;
use std::default::Default;
use std::sync::Arc;
use client::Context;
use model::{Message, Permissions};
use std::collections::HashMap;
#[derive(Default)]
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
}
pub fn command<F>(mut self, command_name: &str, f: F) -> Self
where F: FnOnce(CreateCommand) -> CreateCommand {
let cmd = f(self.build_command()).0;
for n in &cmd.aliases {
if let Some(ref prefix) = self.0.prefix {
self.0.commands.insert(
format!("{} {}", prefix, n.to_owned()),
CommandOrAlias::Alias(format!("{} {}", prefix, command_name.to_string())),
);
} else {
self.0.commands.insert(
n.to_owned(),
CommandOrAlias::Alias(command_name.to_string()),
);
}
}
self.0.commands.insert(
command_name.to_owned(),
CommandOrAlias::Command(Arc::new(cmd)),
);
self
}
pub fn on(mut self, name: &str,
f: fn(&mut Context, &Message, Args) -> Result<(), CommandError>) -> Self {
let cmd = Arc::new(Command::new(f));
self.0
.commands
.insert(name.to_string(), CommandOrAlias::Command(cmd));
self
}
pub fn prefix(mut self, desc: &str) -> Self {
self.0.prefix = Some(desc.to_owned());
self
}
pub fn bucket(mut self, bucket: &str) -> Self {
self.0.bucket = Some(bucket.to_owned());
self
}
pub fn dm_only(mut self, dm_only: bool) -> Self {
self.0.dm_only = dm_only;
self
}
pub fn guild_only(mut self, guild_only: bool) -> Self {
self.0.guild_only = guild_only;
self
}
pub fn help_available(mut self, help_available: bool) -> Self {
self.0.help_available = help_available;
self
}
pub fn owners_only(mut self, owners_only: bool) -> Self {
self.0.owners_only = owners_only;
self
}
pub fn required_permissions(mut self, permissions: Permissions) -> Self {
self.0.required_permissions = permissions;
self
}
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(),
}
}
}