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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
use std::sync::Arc;
use super::{Args, Configuration};
use client::Context;
use model::{Message, Permissions};
use std::collections::HashMap;
use std::fmt;
pub type Check = Fn(&mut Context, &Message, &mut Args, &Arc<Command>) -> bool
+ Send
+ Sync
+ 'static;
pub type Exec = Fn(&mut Context, &Message, Args) -> Result<(), Error> + Send + Sync + 'static;
pub type Help = Fn(&mut Context, &Message, HashMap<String, Arc<CommandGroup>>, Args)
-> Result<(), Error>
+ Send
+ Sync
+ 'static;
pub type BeforeHook = Fn(&mut Context, &Message, &str) -> bool + Send + Sync + 'static;
pub type AfterHook = Fn(&mut Context, &Message, &str, Result<(), Error>) + Send + Sync + 'static;
pub(crate) type InternalCommand = Arc<Command>;
pub type PrefixCheck = Fn(&mut Context, &Message) -> Option<String> + Send + Sync + 'static;
pub enum CommandOrAlias {
Alias(String),
Command(InternalCommand),
}
#[derive(Clone, Debug)]
pub struct Error(pub String);
impl<D: fmt::Display> From<D> for Error {
fn from(d: D) -> Self {
Error(format!("{}", d))
}
}
pub enum CommandType {
StringResponse(String),
Basic(Box<Exec>),
WithCommands(Box<Help>),
}
pub struct CommandGroup {
pub prefix: Option<String>,
pub commands: HashMap<String, CommandOrAlias>,
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,
}
pub struct Command {
pub checks: Vec<Box<Check>>,
pub exec: CommandType,
pub bucket: Option<String>,
pub desc: Option<String>,
pub example: Option<String>,
pub usage: Option<String>,
pub min_args: Option<i32>,
pub max_args: Option<i32>,
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,
pub(crate) aliases: Vec<String>,
}
impl Command {
pub fn new<F>(f: F) -> Self
where F: Fn(&mut Context, &Message, Args) -> Result<(), Error> + Send + Sync + 'static {
Command {
exec: CommandType::Basic(Box::new(f)),
..Command::default()
}
}
}
impl Default for Command {
fn default() -> Command {
Command {
aliases: Vec::new(),
checks: Vec::default(),
exec: CommandType::Basic(Box::new(|_, _, _| Ok(()))),
desc: None,
usage: None,
example: None,
min_args: None,
bucket: None,
max_args: None,
required_permissions: Permissions::empty(),
dm_only: false,
guild_only: false,
help_available: true,
owners_only: false,
allowed_roles: Vec::new(),
}
}
}
pub fn positions(ctx: &mut Context, msg: &Message, conf: &Configuration) -> Option<Vec<usize>> {
if !conf.prefixes.is_empty() || conf.dynamic_prefix.is_some() {
let mut positions: Vec<usize> = vec![];
if let Some(mention_end) = find_mention_end(&msg.content, conf) {
positions.push(mention_end);
return Some(positions);
} else if let Some(ref func) = conf.dynamic_prefix {
if let Some(x) = func(ctx, msg) {
if msg.content.starts_with(&x) {
positions.push(x.len());
}
} else {
for n in &conf.prefixes {
if msg.content.starts_with(n) {
positions.push(n.len());
}
}
}
} else {
for n in &conf.prefixes {
if msg.content.starts_with(n) {
positions.push(n.len());
}
}
};
if positions.is_empty() {
return None;
}
let pos = *unsafe { positions.get_unchecked(0) };
if conf.allow_whitespace {
positions.insert(0, find_end_of_prefix_with_whitespace(&msg.content, pos).unwrap_or(pos));
} else if find_end_of_prefix_with_whitespace(&msg.content, pos).is_some() {
return None;
}
Some(positions)
} else if conf.on_mention.is_some() {
find_mention_end(&msg.content, conf).map(|mention_end| {
vec![mention_end]
})
} else {
None
}
}
fn find_mention_end(content: &str, conf: &Configuration) -> Option<usize> {
conf.on_mention.as_ref().and_then(|mentions| {
mentions
.iter()
.find(|mention| content.starts_with(&mention[..]))
.map(|m| m.len())
})
}
fn find_end_of_prefix_with_whitespace(content: &str, position: usize) -> Option<usize> {
let mut ws_split = content.split_whitespace();
if let Some(cmd) = ws_split.nth(1) {
if let Some(index_of_cmd) = content.find(cmd) {
if index_of_cmd > position && index_of_cmd <= content.len() {
let slice = unsafe { content.slice_unchecked(position, index_of_cmd) }.as_bytes();
for byte in slice.iter() {
if *byte != 0x20u8 {
return None;
}
}
return Some(index_of_cmd);
}
}
}
None
}