This enhanced help command provides users with a comprehensive guide to all available bot commands, allowing them to view commands by category or specific command details.
The /help
command is designed to assist users in exploring bot capabilities by organizing commands into categories and providing detailed descriptions and usage guides for each command.
/help commandName
), users receive an in-depth description, usage guide, and category information for that command./help
without arguments, the bot presents a keyboard with all command categories./help <commandName>
, and the bot provides detailed information on that command./help
/help ping
(provides details on the ping
command)If a command name is invalid, the bot notifies the user and suggests using /help
to view all categories.
const { MessageEntity } = require('node-telegram-bot-api');
module.exports = {
name: 'help',
adminOnly: false,
ownerOnly: false,
category: 'Utility',
description: 'Show available commands or info about a specific command',
guide: 'Use /help for all commands, or /help <command> for specific info',
execute: async (bot, msg, args) => {
const chatId = msg.chat.id;
const commands = bot.commands;
if (!commands) {
return bot.sendMessage(chatId, 'Error: Commands not available. Please try again later.');
}
const createCommandList = (cmds) => {
return Object.entries(cmds)
.map(([name, cmd]) => `/${name} - ${cmd.description || 'No description'}`)
.join('\n');
};
const createCategoryKeyboard = () => {
const categories = [...new Set(Object.values(commands).map(cmd => cmd.category || 'Uncategorized'))];
const keyboard = categories.map(category => [{
text: category,
callback_data: `help_category_${category}`
}]);
return { reply_markup: JSON.stringify({ inline_keyboard: keyboard }) };
};
const handleCallback = async (callbackQuery) => {
const action = callbackQuery.data;
const chatId = callbackQuery.message.chat.id;
const messageId = callbackQuery.message.message_id;
if (action.startsWith('help_category_')) {
const category = action.split('_')[2];
const categoryCommands = Object.fromEntries(
Object.entries(commands).filter(([_, cmd]) => cmd.category === category)
);
const commandList = createCommandList(categoryCommands);
await bot.editMessageText(`Commands in ${category}:\n\n${commandList}`, {
chat_id: chatId,
message_id: messageId,
parse_mode: 'Markdown'
});
}
await bot.answerCallbackQuery(callbackQuery.id);
};
bot.on('callback_query', handleCallback);
if (args.length === 0) {
const message = 'Choose a category to see commands:';
await bot.sendMessage(chatId, message, createCategoryKeyboard());
} else {
const commandName = args[0].toLowerCase();
const command = commands[commandName];
if (command) {
const helpMessage = `*/${commandName}*\n` +
`Description: ${command.description || 'No description'}\n` +
`Usage: ${command.guide || 'No usage guide'}\n` +
`Category: ${command.category || 'Uncategorized'}`;
await bot.sendMessage(chatId, helpMessage, { parse_mode: 'Markdown' });
} else {
await bot.sendMessage(chatId, `Command /${commandName} not found. Use /help to see all categories.`);
}
}
}
};