-
Notifications
You must be signed in to change notification settings - Fork 117
/
help.rs
427 lines (398 loc) · 15 KB
/
help.rs
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
//! Contains the built-in help command and surrounding infrastructure
use crate::{serenity_prelude as serenity, CreateReply};
use std::fmt::Write as _;
/// Optional configuration for how the help message from [`help()`] looks
pub struct HelpConfiguration<'a> {
/// Extra text displayed at the bottom of your message. Can be used for help and tips specific
/// to your bot
pub extra_text_at_bottom: &'a str,
/// Whether to make the response ephemeral if possible. Can be nice to reduce clutter
pub ephemeral: bool,
/// Whether to list context menu commands as well
pub show_context_menu_commands: bool,
/// Whether to list context menu commands as well
pub show_subcommands: bool,
/// Whether to include [`crate::Command::description`] (above [`crate::Command::help_text`]).
pub include_description: bool,
#[doc(hidden)]
pub __non_exhaustive: (),
}
impl Default for HelpConfiguration<'_> {
fn default() -> Self {
Self {
extra_text_at_bottom: "",
ephemeral: true,
show_context_menu_commands: false,
show_subcommands: false,
include_description: true,
__non_exhaustive: (),
}
}
}
/// Convenience function to align descriptions behind commands
struct TwoColumnList(Vec<(String, Option<String>)>);
impl TwoColumnList {
/// Creates a new [`TwoColumnList`]
fn new() -> Self {
Self(Vec::new())
}
/// Add a line that needs the padding between the columns
fn push_two_colums(&mut self, command: String, description: String) {
self.0.push((command, Some(description)));
}
/// Add a line that doesn't influence the first columns's width
fn push_heading(&mut self, category: &str) {
if !self.0.is_empty() {
self.0.push(("".to_string(), None));
}
let mut category = category.to_string();
category += ":";
self.0.push((category, None));
}
/// Convert the list into a string with aligned descriptions
fn into_string(self) -> String {
let longest_command = self
.0
.iter()
.filter_map(|(command, description)| {
if description.is_some() {
Some(command.len())
} else {
None
}
})
.max()
.unwrap_or(0);
let mut text = String::new();
for (command, description) in self.0 {
if let Some(description) = description {
let padding = " ".repeat(longest_command - command.len() + 3);
writeln!(text, "{}{}{}", command, padding, description).unwrap();
} else {
writeln!(text, "{}", command).unwrap();
}
}
text
}
}
/// Get the prefix from options
pub(super) async fn get_prefix_from_options<U, E>(ctx: crate::Context<'_, U, E>) -> Option<String> {
let options = &ctx.framework().options().prefix_options;
match &options.prefix {
Some(fixed_prefix) => Some(fixed_prefix.clone()),
None => match options.dynamic_prefix {
Some(dynamic_prefix_callback) => {
match dynamic_prefix_callback(crate::PartialContext::from(ctx)).await {
Ok(Some(dynamic_prefix)) => Some(dynamic_prefix),
_ => None,
}
}
None => None,
},
}
}
/// Format context menu command name
fn format_context_menu_name<U, E>(command: &crate::Command<U, E>) -> Option<String> {
let kind = match command.context_menu_action {
Some(crate::ContextMenuCommandAction::User(_)) => "user",
Some(crate::ContextMenuCommandAction::Message(_)) => "message",
Some(crate::ContextMenuCommandAction::__NonExhaustive) => unreachable!(),
None => return None,
};
Some(format!(
"{} (on {})",
command
.context_menu_name
.as_deref()
.unwrap_or(&command.name),
kind
))
}
/// Code for printing help of a specific command (e.g. `~help my_command`)
async fn help_single_command<U, E>(
ctx: crate::Context<'_, U, E>,
command_name: &str,
config: HelpConfiguration<'_>,
) -> Result<(), serenity::Error> {
let commands = &ctx.framework().options().commands;
// Try interpret the command name as a context menu command first
let mut command = commands.iter().find(|command| {
if let Some(context_menu_name) = &command.context_menu_name {
if context_menu_name.eq_ignore_ascii_case(command_name) {
return true;
}
}
false
});
// Then interpret command name as a normal command (possibly nested subcommand)
if command.is_none() {
if let Some((c, _, _)) = crate::find_command(commands, command_name, true, &mut vec![]) {
command = Some(c);
}
}
let reply = if let Some(command) = command {
let mut invocations = Vec::new();
let mut subprefix = None;
if command.slash_action.is_some() {
invocations.push(format!("`/{}`", command.name));
subprefix = Some(format!(" /{}", command.name));
}
if command.prefix_action.is_some() {
let prefix = match get_prefix_from_options(ctx).await {
Some(prefix) => prefix,
// None can happen if the prefix is dynamic, and the callback
// fails due to help being invoked with slash or context menu
// commands. Not sure there's a better way to handle this.
None => String::from("<prefix>"),
};
invocations.push(format!("`{}{}`", prefix, command.name));
if subprefix.is_none() {
subprefix = Some(format!(" {}{}", prefix, command.name));
}
}
if command.context_menu_name.is_some() && command.context_menu_action.is_some() {
// Since command.context_menu_action is Some, this unwrap is safe
invocations.push(format_context_menu_name(command).unwrap());
if subprefix.is_none() {
subprefix = Some(String::from(" "));
}
}
// At least one of the three if blocks should have triggered
assert!(subprefix.is_some());
assert!(!invocations.is_empty());
let invocations = invocations.join("\n");
let mut text = match (&command.description, &command.help_text) {
(Some(description), Some(help_text)) => {
if config.include_description {
format!("{}\n\n{}", description, help_text)
} else {
help_text.clone()
}
}
(Some(description), None) => description.to_owned(),
(None, Some(help_text)) => help_text.clone(),
(None, None) => "No help available".to_string(),
};
if !command.parameters.is_empty() {
text += "\n\n```\nParameters:\n";
let mut parameterlist = TwoColumnList::new();
for parameter in &command.parameters {
let name = parameter.name.clone();
let description = parameter.description.as_deref().unwrap_or("");
let description = format!(
"({}) {}",
if parameter.required {
"required"
} else {
"optional"
},
description,
);
parameterlist.push_two_colums(name, description);
}
text += ¶meterlist.into_string();
text += "```";
}
if !command.subcommands.is_empty() {
text += "\n\n```\nSubcommands:\n";
let mut commandlist = TwoColumnList::new();
// Subcommands can exist on context menu commands, but there's no
// hierarchy in the menu, so just display them as a list without
// subprefix.
preformat_subcommands(
&mut commandlist,
command,
&subprefix.unwrap_or_else(|| String::from(" ")),
);
text += &commandlist.into_string();
text += "```";
}
format!("**{}**\n\n{}", invocations, text)
} else {
format!("No such command `{}`", command_name)
};
let reply = CreateReply::default()
.content(reply)
.ephemeral(config.ephemeral);
ctx.send(reply).await?;
Ok(())
}
/// Recursively formats all subcommands
fn preformat_subcommands<U, E>(
commands: &mut TwoColumnList,
command: &crate::Command<U, E>,
prefix: &str,
) {
let as_context_command = command.slash_action.is_none() && command.prefix_action.is_none();
for subcommand in &command.subcommands {
let command = if as_context_command {
let name = format_context_menu_name(subcommand);
if name.is_none() {
continue;
};
name.unwrap()
} else {
format!("{} {}", prefix, subcommand.name)
};
let description = subcommand.description.as_deref().unwrap_or("").to_string();
commands.push_two_colums(command, description);
// We could recurse here, but things can get cluttered quickly.
// Instead, we show (using this function) subsubcommands when
// the user asks for help on the subcommand.
}
}
/// Preformat lines (except for padding,) like `(" /ping", "Emits a ping message")`
fn preformat_command<U, E>(
commands: &mut TwoColumnList,
config: &HelpConfiguration<'_>,
command: &crate::Command<U, E>,
indent: &str,
options_prefix: Option<&str>,
) {
let prefix = if command.slash_action.is_some() {
String::from("/")
} else if command.prefix_action.is_some() {
options_prefix.map(String::from).unwrap_or_default()
} else {
// This is not a prefix or slash command, i.e. probably a context menu only command
// This should have been filtered out in `generate_all_commands`
unreachable!();
};
let prefix = format!("{}{}{}", indent, prefix, command.name);
commands.push_two_colums(
prefix.clone(),
command.description.as_deref().unwrap_or("").to_string(),
);
if config.show_subcommands {
preformat_subcommands(commands, command, &prefix)
}
}
/// Create help text for `help_all_commands`
///
/// This is a separate function so we can have tests for it
async fn generate_all_commands<U, E>(
ctx: crate::Context<'_, U, E>,
config: &HelpConfiguration<'_>,
) -> Result<String, serenity::Error> {
let mut categories = indexmap::IndexMap::<Option<&str>, Vec<&crate::Command<U, E>>>::new();
for cmd in &ctx.framework().options().commands {
categories
.entry(cmd.category.as_deref())
.or_default()
.push(cmd);
}
let options_prefix = get_prefix_from_options(ctx).await;
let mut menu = String::from("```\n");
let mut commandlist = TwoColumnList::new();
for (category_name, commands) in categories {
let commands = commands
.into_iter()
.filter(|cmd| {
!cmd.hide_in_help && (cmd.prefix_action.is_some() || cmd.slash_action.is_some())
})
.collect::<Vec<_>>();
if commands.is_empty() {
continue;
}
commandlist.push_heading(category_name.unwrap_or("Commands"));
for command in commands {
preformat_command(
&mut commandlist,
config,
command,
" ",
options_prefix.as_deref(),
);
}
}
menu += &commandlist.into_string();
if config.show_context_menu_commands {
menu += "\nContext menu commands:\n";
for command in &ctx.framework().options().commands {
let name = format_context_menu_name(command);
if name.is_none() {
continue;
};
let _ = writeln!(menu, " {}", name.unwrap());
}
}
menu += "\n";
menu += config.extra_text_at_bottom;
menu += "\n```";
Ok(menu)
}
/// Code for printing an overview of all commands (e.g. `~help`)
async fn help_all_commands<U, E>(
ctx: crate::Context<'_, U, E>,
config: HelpConfiguration<'_>,
) -> Result<(), serenity::Error> {
let menu = generate_all_commands(ctx, &config).await?;
let reply = CreateReply::default()
.content(menu)
.ephemeral(config.ephemeral);
ctx.send(reply).await?;
Ok(())
}
/// A help command that outputs text in a code block, groups commands by categories, and annotates
/// commands with a slash if they exist as slash commands.
///
/// Example usage from Ferris, the Discord bot running in the Rust community server:
/// ```rust
/// # type Error = Box<dyn std::error::Error>;
/// # type Context<'a> = poise::Context<'a, (), Error>;
/// /// Show this menu
/// #[poise::command(prefix_command, track_edits, slash_command)]
/// pub async fn help(
/// ctx: Context<'_>,
/// #[description = "Specific command to show help about"] command: Option<String>,
/// ) -> Result<(), Error> {
/// let config = poise::builtins::HelpConfiguration {
/// extra_text_at_bottom: "\
/// Type ?help command for more info on a command.
/// You can edit your message to the bot and the bot will edit its response.",
/// ..Default::default()
/// };
/// poise::builtins::help(ctx, command.as_deref(), config).await?;
/// Ok(())
/// }
/// ```
/// Output:
/// ```text
/// Playground:
/// ?play Compile and run Rust code in a playground
/// ?eval Evaluate a single Rust expression
/// ?miri Run code and detect undefined behavior using Miri
/// ?expand Expand macros to their raw desugared form
/// ?clippy Catch common mistakes using the Clippy linter
/// ?fmt Format code using rustfmt
/// ?microbench Benchmark small snippets of code
/// ?procmacro Compile and use a procedural macro
/// ?godbolt View assembly using Godbolt
/// ?mca Run performance analysis using llvm-mca
/// ?llvmir View LLVM IR using Godbolt
/// Crates:
/// /crate Lookup crates on crates.io
/// /doc Lookup documentation
/// Moderation:
/// /cleanup Deletes the bot's messages for cleanup
/// /ban Bans another person
/// ?move Move a discussion to another channel
/// /rustify Adds the Rustacean role to members
/// Miscellaneous:
/// ?go Evaluates Go code
/// /source Links to the bot GitHub repo
/// /help Show this menu
///
/// Type ?help command for more info on a command.
/// You can edit your message to the bot and the bot will edit its response.
/// ```
pub async fn help<U, E>(
ctx: crate::Context<'_, U, E>,
command: Option<&str>,
config: HelpConfiguration<'_>,
) -> Result<(), serenity::Error> {
match command {
Some(command) => help_single_command(ctx, command, config).await,
None => help_all_commands(ctx, config).await,
}
}