/[sudobot]/trunk/src/commands/settings/HelpCommand.ts
ViewVC logotype

Contents of /trunk/src/commands/settings/HelpCommand.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 486 - (show annotations)
Mon Jul 29 17:30:28 2024 UTC (8 months, 1 week ago) by rakin
File MIME type: application/typescript
File size: 5279 byte(s)
feat: add pagination to help command
1 /**
2 * This file is part of SudoBot.
3 *
4 * Copyright (C) 2021-2022 OSN Inc.
5 *
6 * SudoBot is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * SudoBot is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
15 *
16 * You should have received a copy of the GNU Affero General Public License
17 * along with SudoBot. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20 import { CommandInteraction, Message } from 'discord.js';
21 import BaseCommand from '../../utils/structures/BaseCommand';
22 import DiscordClient from '../../client/Client';
23 import Help from '../../utils/Help';
24 import CommandOptions from '../../types/CommandOptions';
25 import InteractionOptions from '../../types/InteractionOptions';
26 import MessageEmbed from '../../client/MessageEmbed';
27 import { getAllCommandData, renderCommandMeta } from '../../services/CommandMetaDataManager';
28 import Pagination from '../../utils/Pagination';
29
30 export default class HelpCommand extends BaseCommand {
31 constructor() {
32 super('help', 'settings', ['?']);
33 this.supportsInteractions = true;
34 }
35
36 async render() {
37 let string = '';
38
39 for (let cmd of Help) {
40 string += `\n\n**${cmd.name}**\n${cmd.shortBrief}`;
41 }
42
43 return string;
44 }
45
46 async run(client: DiscordClient, message: Message | CommandInteraction, options: CommandOptions | InteractionOptions) {
47 if ((options.isInteraction && !options.options.getString('command')) || (!options.isInteraction && options.args[0] === undefined)) {
48 const pagination = new Pagination(getAllCommandData(), {
49 channel_id: message.channel!.id,
50 guild_id: message.guild!.id,
51 limit: 10,
52 user_id: message.member!.user.id,
53 timeout: 120_000,
54 embedBuilder({ data, maxPages, currentPage }) {
55 let description = `\`<...>\` means required argument and \`[...]\` means optional argument.\nRun \`${client.config.get('prefix')}help <CommandName>\` for more information about a specific command.\n\n`;
56
57 for (const { name, shortBrief } of data) {
58 description += `**${name}**\n`;
59 description += `${shortBrief}\n\n`;
60 }
61
62 return new MessageEmbed({
63 author: {
64 iconURL: client.user?.displayAvatarURL(),
65 name: "Help"
66 },
67 description,
68 footer: {
69 text: `Page ${currentPage} of ${maxPages}`
70 }
71 });
72 },
73 });
74
75 let reply = await message.reply(pagination.getMessageOptions(1));
76
77 if (message instanceof CommandInteraction) {
78 reply = <Message> await message.fetchReply();
79 }
80
81 await pagination.start(reply!);
82
83 return;
84 }
85
86 const commandName = options.isInteraction ? options.options.getString('command') : options.args[0];
87 const cmd = Help.find(c => c.name === commandName);
88
89 if (!cmd) {
90 await message.reply({
91 embeds: [
92 new MessageEmbed()
93 .setColor('#f14a60')
94 .setDescription(`Invalid command \`${commandName}\`.`)
95 ]
96 });
97
98 return;
99 }
100
101 await message.reply({
102 embeds: [renderCommandMeta(cmd)]
103 });
104
105 // let fields = [
106 // {
107 // name: "Usage",
108 // value: `\`${client.config.get('prefix')}${cmd.name}\`` + (cmd.structure.trim() !== '' ? ` \`${cmd.structure}\`` : '')
109 // },
110 // {
111 // name: 'Examples',
112 // value: cmd.example.replace(/\%\%/g, client.config.get('prefix'))
113 // }
114 // ];
115
116 // if (cmd.options !== undefined) {
117 // let str = '';
118
119 // for (let opt in cmd.options)
120 // str += `\`${opt}\` - ${cmd.options[opt]}\n`;
121
122 // str = str.substring(0, str.length - 1);
123
124 // fields.push({
125 // name: 'Options',
126 // value: str
127 // });
128 // }
129
130 // if (cmd.notes !== null) {
131 // fields.push({
132 // name: "Notes",
133 // value: cmd.notes
134 // });
135 // }
136
137 // await message.reply({
138 // embeds: [
139 // new MessageEmbed()
140 // .setTitle(`${client.config.get('prefix')}${cmd.name}`)
141 // .setDescription("`<...>` means required argument, `[...]` means optional argument.\n\n" + (cmd.description !== null ? cmd.description : cmd.shortBrief))
142 // .addFields(fields)
143 // ]
144 // });
145 }
146 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26