Skip to content

Commit e67c31b

Browse files
author
xyzjesper
committed
fix(help): re-created help button to manage pagination (#16)
1 parent 93dafdd commit e67c31b

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import {
2+
ButtonBuilder,
3+
ButtonInteraction,
4+
ButtonStyle,
5+
ContainerBuilder,
6+
MessageFlags,
7+
SeparatorSpacingSize,
8+
TextDisplayBuilder
9+
} from "discord.js";
10+
import { convertToEmojiPng } from "../../../helper/emojis.js";
11+
import { ExtendedClient } from "../../../types/client.js";
12+
13+
14+
export default {
15+
id: "bot-help-page",
16+
17+
async execute(interaction: ButtonInteraction, client: ExtendedClient) {
18+
try {
19+
if (!client.user) throw new Error("Client user not found");
20+
await interaction.deferReply({ flags: MessageFlags.Ephemeral })
21+
22+
const [, pageRaw] = interaction.customId.split(":");
23+
const page = Math.max(0, parseInt(pageRaw ?? "0"));
24+
25+
const allCommands = [
26+
...(client.commands?.values() ?? []),
27+
...(client.subCommands?.values() ?? [])
28+
].filter(cmd => !!cmd.help);
29+
30+
const perPage = 5;
31+
const totalPages = Math.ceil(allCommands.length / perPage);
32+
33+
const container = new ContainerBuilder();
34+
35+
const start = page * perPage;
36+
const pageCommands = allCommands.slice(start, start + perPage);
37+
38+
const title = new TextDisplayBuilder().setContent(
39+
`# ${await convertToEmojiPng("disbot", client.user!.id)} **DisBot Help**`
40+
);
41+
container.addTextDisplayComponents(title);
42+
container.addSeparatorComponents(s => s.setSpacing(SeparatorSpacingSize.Large));
43+
44+
for (const command of pageCommands) {
45+
const help = command.help;
46+
const content = `### ${await convertToEmojiPng("bookmarked", client.user!.id)} [${help.name}](${help.docsLink ?? "https://docs.disbot.xyz"})\n` +
47+
`> ${await convertToEmojiPng("paperclip", client.user!.id)} **Description:** ${help.description}\n` +
48+
`> ${await convertToEmojiPng("use", client.user!.id)} **Usage:** \`${help.usage}\`\n` +
49+
(help.examples.length > 0 ? `> ${await convertToEmojiPng("bookdashed", client.user!.id)} **Example:**\n${help.examples.map(e => `> - ${e}`).join("\n")}` : "") +
50+
(help.aliases.length > 0 ? `\n> ${await convertToEmojiPng("filestack", client.user!.id)} **Aliases:**\n ${help.aliases.map(e => `> - ${e}`).join("\n")}` : "");
51+
52+
container.addTextDisplayComponents(new TextDisplayBuilder().setContent(content));
53+
container.addSeparatorComponents(s => s.setSpacing(SeparatorSpacingSize.Small));
54+
}
55+
56+
container.addSeparatorComponents(s => s.setSpacing(SeparatorSpacingSize.Large));
57+
58+
const prevButton = new ButtonBuilder()
59+
.setCustomId(`bot-help-page:${page - 1}`)
60+
.setStyle(ButtonStyle.Secondary)
61+
.setEmoji("<:arrowbackregular24:1301119279088799815>")
62+
.setDisabled(page <= 0);
63+
64+
const nextButton = new ButtonBuilder()
65+
.setCustomId(`bot-help-page:${page + 1}`)
66+
.setStyle(ButtonStyle.Secondary)
67+
.setEmoji("<:next:1287457822526935090>")
68+
.setDisabled(page + 1 >= totalPages);
69+
70+
container.addActionRowComponents(row => row.addComponents(prevButton, nextButton));
71+
72+
await interaction.editReply({ components: [container], flags: MessageFlags.IsComponentsV2 });
73+
} catch (error) {
74+
console.error("Error in bot-help-page interaction:", error);
75+
76+
}
77+
}
78+
};

0 commit comments

Comments
 (0)