Skip to content

Commit 6040d86

Browse files
committed
refactor(util): migrate ping and help to components v2
1 parent 4b62c0c commit 6040d86

1 file changed

Lines changed: 104 additions & 12 deletions

File tree

src/command/util.rs

Lines changed: 104 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::abstraction::command::{CommandContext, CommandResult};
2-
use poise::CreateReply;
2+
use crate::abstraction::components_v2::{self, Status};
33

44
/// Show this menu
55
#[poise::command(
@@ -11,12 +11,99 @@ pub async fn help(
1111
ctx: CommandContext<'_>,
1212
#[description = "Specific command to show help about"] command: Option<String>,
1313
) -> CommandResult {
14-
let config = poise::builtins::HelpConfiguration {
15-
extra_text_at_bottom: "\
16-
Type /help command for more info on a command.",
17-
..Default::default()
18-
};
19-
poise::builtins::help(ctx, command.as_deref(), config).await?;
14+
let commands = &ctx.framework().options().commands;
15+
16+
if let Some(query) = command.as_deref() {
17+
let query_lc = query.to_lowercase();
18+
let found = commands.iter().find(|c| {
19+
c.name.eq_ignore_ascii_case(&query_lc)
20+
|| c.qualified_name.eq_ignore_ascii_case(&query_lc)
21+
});
22+
23+
if let Some(cmd) = found {
24+
let params = if cmd.parameters.is_empty() {
25+
"—".to_string()
26+
} else {
27+
cmd.parameters
28+
.iter()
29+
.map(|p| {
30+
let req = if p.required { "" } else { "?" };
31+
format!("`{}{}`", p.name, req)
32+
})
33+
.collect::<Vec<_>>()
34+
.join(" ")
35+
};
36+
37+
let category = cmd
38+
.category
39+
.as_deref()
40+
.map(|s| s.to_string())
41+
.unwrap_or_else(|| "—".to_string());
42+
let description = cmd
43+
.description
44+
.as_deref()
45+
.map(|s| s.to_string())
46+
.unwrap_or_else(|| "—".to_string());
47+
48+
let rows = vec![
49+
("Category".to_string(), category),
50+
("Description".to_string(), description),
51+
("Parameters".to_string(), params),
52+
];
53+
54+
ctx.send(components_v2::card_reply(
55+
Status::Info,
56+
format!("/{}", cmd.qualified_name),
57+
rows,
58+
))
59+
.await?;
60+
} else {
61+
ctx.send(components_v2::status_reply(
62+
Status::Error,
63+
format!("No command named `{query}` found."),
64+
))
65+
.await?;
66+
}
67+
68+
return Ok(());
69+
}
70+
71+
let mut by_category: std::collections::BTreeMap<String, Vec<String>> =
72+
std::collections::BTreeMap::new();
73+
for cmd in commands {
74+
if cmd.hide_in_help {
75+
continue;
76+
}
77+
let category = cmd
78+
.category
79+
.as_deref()
80+
.map(|s| s.to_string())
81+
.unwrap_or_else(|| "Uncategorized".to_string());
82+
let line = match cmd.description.as_deref() {
83+
Some(desc) => format!("`/{}` — {desc}", cmd.qualified_name),
84+
None => format!("`/{}`", cmd.qualified_name),
85+
};
86+
by_category.entry(category).or_default().push(line);
87+
}
88+
89+
let rows: Vec<(String, String)> = by_category
90+
.into_iter()
91+
.map(|(cat, lines)| (cat, lines.join("\n")))
92+
.collect();
93+
94+
ctx.send(components_v2::card_reply(
95+
Status::Info,
96+
"RetroBot commands",
97+
rows,
98+
))
99+
.await?;
100+
101+
ctx.send(components_v2::status_reply(
102+
Status::Info,
103+
"Type `/help <command>` for details on a specific command.",
104+
))
105+
.await?;
106+
20107
Ok(())
21108
}
22109

@@ -27,22 +114,27 @@ Type /help command for more info on a command.",
27114
required_bot_permissions = "SEND_MESSAGES"
28115
)]
29116
pub async fn ping(ctx: CommandContext<'_>) -> CommandResult {
30-
let handle = ctx.reply("Calculating....").await?;
117+
let handle = ctx
118+
.send(components_v2::status_reply(Status::Info, "Calculating..."))
119+
.await?;
31120

32121
let handle_message = handle.message().await?;
33122

34123
let sent_timestamp = handle_message.timestamp.timestamp_millis();
35124
let original_timestamp = ctx.created_at().timestamp_millis();
36125

37126
let rest_latency = sent_timestamp - original_timestamp;
38-
let gateway_latency = ctx.ping().await.as_millis();
127+
let gateway_latency = ctx.ping().await.map(|d| d.as_millis()).unwrap_or(0);
39128

40129
handle
41130
.edit(
42131
ctx,
43-
CreateReply::default().content(format!(
44-
"Pong! Rest latency: {rest_latency}ms, Gateway latency: {gateway_latency}ms"
45-
)),
132+
components_v2::status_reply(
133+
Status::Success,
134+
format!(
135+
"Pong! Rest latency: {rest_latency}ms, Gateway latency: {gateway_latency}ms"
136+
),
137+
),
46138
)
47139
.await?;
48140

0 commit comments

Comments
 (0)