serenity_self/framework/standard/structures/
mod.rs1use std::collections::HashSet;
2use std::error::Error as StdError;
3use std::fmt;
4
5use futures::future::BoxFuture;
6
7use super::Args;
8use crate::client::Context;
9use crate::model::channel::Message;
10use crate::model::id::UserId;
11use crate::model::permissions::Permissions;
12use crate::model::Colour;
13
14pub mod buckets;
15mod check;
16
17pub use self::check::*;
18
19#[derive(Clone, Copy, Debug, Eq, PartialEq)]
20#[non_exhaustive]
21pub enum OnlyIn {
22 Dm,
23 Guild,
24 None,
25}
26
27impl Default for OnlyIn {
28 fn default() -> Self {
29 Self::None
30 }
31}
32
33#[derive(Debug, Default, PartialEq)]
34pub struct CommandOptions {
35 pub checks: &'static [&'static Check],
38 pub bucket: Option<&'static str>,
40 pub names: &'static [&'static str],
42 pub desc: Option<&'static str>,
44 pub delimiters: &'static [&'static str],
49 pub usage: Option<&'static str>,
51 pub examples: &'static [&'static str],
53 pub min_args: Option<u16>,
55 pub max_args: Option<u16>,
57 pub allowed_roles: &'static [&'static str],
59 pub required_permissions: Permissions,
61 pub help_available: bool,
63 pub only_in: OnlyIn,
65 pub owners_only: bool,
67 pub owner_privilege: bool,
69 pub sub_commands: &'static [&'static Command],
71}
72
73pub type CommandError = Box<dyn StdError + Send + Sync>;
74pub type CommandResult<T = ()> = std::result::Result<T, CommandError>;
75pub type CommandFn =
76 for<'fut> fn(&'fut Context, &'fut Message, Args) -> BoxFuture<'fut, CommandResult>;
77
78pub struct Command {
79 pub fun: CommandFn,
80 pub options: &'static CommandOptions,
81}
82
83impl fmt::Debug for Command {
84 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
85 f.debug_struct("Command").field("options", &self.options).finish_non_exhaustive()
86 }
87}
88
89impl PartialEq for Command {
90 #[inline]
91 fn eq(&self, other: &Command) -> bool {
92 (self.fun as usize == other.fun as usize) && (self.options == other.options)
93 }
94}
95
96pub type HelpCommandFn = for<'fut> fn(
97 &'fut Context,
98 &'fut Message,
99 Args,
100 &'fut HelpOptions,
101 &'fut [&'static CommandGroup],
102 HashSet<UserId>,
103) -> BoxFuture<'fut, CommandResult>;
104
105pub struct HelpCommand {
106 pub fun: HelpCommandFn,
107 pub options: &'static HelpOptions,
108}
109
110impl fmt::Debug for HelpCommand {
111 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
112 f.debug_struct("HelpCommand")
113 .field("fun", &"<function>")
114 .field("options", &self.options)
115 .finish()
116 }
117}
118
119impl PartialEq for HelpCommand {
120 #[inline]
121 fn eq(&self, other: &HelpCommand) -> bool {
122 (self.fun as usize == other.fun as usize) && (self.options == other.options)
123 }
124}
125
126#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
132#[non_exhaustive]
133pub enum HelpBehaviour {
134 Nothing,
136 Strike,
138 Hide,
140}
141
142#[derive(Clone, Debug, PartialEq, Eq)]
143pub struct HelpOptions {
144 pub names: &'static [&'static str],
147 pub suggestion_text: &'static str,
149 pub no_help_available_text: &'static str,
151 pub usage_label: &'static str,
153 pub usage_sample_label: &'static str,
155 pub ungrouped_label: &'static str,
157 pub description_label: &'static str,
159 pub grouped_label: &'static str,
161 pub aliases_label: &'static str,
163 pub guild_only_text: &'static str,
165 pub checks_label: &'static str,
167 pub sub_commands_label: &'static str,
169 pub dm_only_text: &'static str,
171 pub dm_and_guild_text: &'static str,
173 pub available_text: &'static str,
175 pub command_not_found_text: &'static str,
181 pub individual_command_tip: &'static str,
183 pub strikethrough_commands_tip_in_dm: Option<&'static str>,
189 pub strikethrough_commands_tip_in_guild: Option<&'static str>,
195 pub group_prefix: &'static str,
197 pub lacking_role: HelpBehaviour,
199 pub lacking_permissions: HelpBehaviour,
201 pub lacking_ownership: HelpBehaviour,
203 pub lacking_conditions: HelpBehaviour,
206 pub wrong_channel: HelpBehaviour,
209 pub embed_error_colour: Colour,
211 pub embed_success_colour: Colour,
213 pub max_levenshtein_distance: usize,
215 pub indention_prefix: &'static str,
218}
219
220#[derive(Debug, Default, PartialEq)]
221pub struct GroupOptions {
222 pub prefixes: &'static [&'static str],
223 pub only_in: OnlyIn,
224 pub owners_only: bool,
225 pub owner_privilege: bool,
226 pub help_available: bool,
227 pub allowed_roles: &'static [&'static str],
228 pub required_permissions: Permissions,
229 pub checks: &'static [&'static Check],
230 pub default_command: Option<&'static Command>,
231 pub description: Option<&'static str>,
232 pub summary: Option<&'static str>,
233 pub commands: &'static [&'static Command],
234 pub sub_groups: &'static [&'static CommandGroup],
235}
236
237#[derive(Debug, PartialEq)]
238pub struct CommandGroup {
239 pub name: &'static str,
240 pub options: &'static GroupOptions,
241}
242
243#[cfg(test)]
244#[cfg(all(feature = "cache", feature = "http"))]
245mod levenshtein_tests {
246 use super::HelpBehaviour;
247
248 #[test]
249 fn help_behaviour_eq() {
250 assert_eq!(HelpBehaviour::Hide, std::cmp::max(HelpBehaviour::Hide, HelpBehaviour::Hide));
251 assert_eq!(
252 HelpBehaviour::Strike,
253 std::cmp::max(HelpBehaviour::Strike, HelpBehaviour::Strike)
254 );
255 assert_eq!(
256 HelpBehaviour::Nothing,
257 std::cmp::max(HelpBehaviour::Nothing, HelpBehaviour::Nothing)
258 );
259 }
260
261 #[test]
262 fn help_behaviour_hide() {
263 assert_eq!(HelpBehaviour::Hide, std::cmp::max(HelpBehaviour::Hide, HelpBehaviour::Nothing));
264 assert_eq!(HelpBehaviour::Hide, std::cmp::max(HelpBehaviour::Hide, HelpBehaviour::Strike));
265 }
266
267 #[test]
268 fn help_behaviour_strike() {
269 assert_eq!(
270 HelpBehaviour::Strike,
271 std::cmp::max(HelpBehaviour::Strike, HelpBehaviour::Nothing)
272 );
273 assert_eq!(HelpBehaviour::Hide, std::cmp::max(HelpBehaviour::Strike, HelpBehaviour::Hide));
274 }
275
276 #[test]
277 fn help_behaviour_nothing() {
278 assert_eq!(
279 HelpBehaviour::Strike,
280 std::cmp::max(HelpBehaviour::Nothing, HelpBehaviour::Strike)
281 );
282 assert_eq!(HelpBehaviour::Hide, std::cmp::max(HelpBehaviour::Nothing, HelpBehaviour::Hide));
283 }
284}