1
0
mirror of https://github.com/hack-chat/main.git synced 2024-03-22 13:20:33 +08:00
hack-chat-main/commands/core/help.js

135 lines
4.2 KiB
JavaScript
Raw Normal View History

2022-06-23 00:32:51 +08:00
/**
* @author Marzavec ( https://github.com/marzavec )
* @summary Get help
* @version 1.0.0
* @description Outputs information about the servers current protocol
* @module help
*/
2018-05-13 18:33:22 +08:00
2022-06-23 00:32:51 +08:00
/**
* Executes when invoked by a remote client
* @param {Object} env - Enviroment object with references to core, server, socket & payload
* @public
* @return {void}
*/
2020-09-17 13:44:32 +08:00
export async function run({
core, server, socket, payload,
}) {
// check for spam
2019-11-07 15:35:23 +08:00
if (server.police.frisk(socket.address, 2)) {
return server.reply({
cmd: 'warn', // @todo Add numeric error code as `id`
2019-11-07 15:35:23 +08:00
text: 'You are sending too much text. Wait a moment and try again.\nPress the up arrow key to restore your last message.',
2020-10-10 13:34:59 +08:00
channel: socket.channel, // @todo Multichannel
}, socket);
}
2018-06-04 15:07:24 +08:00
// verify user input
if (typeof payload.command !== 'undefined' && typeof payload.command !== 'string') {
2019-11-07 15:35:23 +08:00
return true;
2018-05-13 18:33:22 +08:00
}
let reply = '';
if (typeof payload.command === 'undefined') {
2019-11-08 06:34:38 +08:00
reply += '# All commands:\n|Category:|Name:|\n|---:|---|\n';
2018-05-13 18:33:22 +08:00
2019-11-07 15:35:23 +08:00
const categories = core.commands.categoriesList.sort();
for (let i = 0, j = categories.length; i < j; i += 1) {
2019-11-08 06:34:38 +08:00
reply += `|${categories[i].replace('../src/commands/', '').replace(/^\w/, (c) => c.toUpperCase())}:|`;
2020-09-17 13:44:32 +08:00
const catCommands = core.commands.all(categories[i]).sort(
(a, b) => a.info.name.localeCompare(b.info.name),
);
2019-11-08 06:34:38 +08:00
reply += `${catCommands.map((c) => `${c.info.name}`).join(', ')}|\n`;
}
2019-11-08 06:34:38 +08:00
reply += '---\nFor specific help on certain commands, use either:\nText: `/help <command name>`\nAPI: `{cmd: \'help\', command: \'<command name>\'}`';
} else {
2019-11-07 15:35:23 +08:00
const command = core.commands.get(payload.command);
if (typeof command === 'undefined') {
2019-11-08 06:34:38 +08:00
reply += 'Unknown command';
} else {
2019-11-08 06:34:38 +08:00
reply += `# ${command.info.name} command:\n| | |\n|---:|---|\n`;
reply += `|**Name:**|${command.info.name}|\n`;
reply += `|**Aliases:**|${typeof command.info.aliases !== 'undefined' ? command.info.aliases.join(', ') : 'None'}|\n`;
reply += `|**Category:**|${command.info.category.replace('../src/commands/', '').replace(/^\w/, (c) => c.toUpperCase())}|\n`;
reply += `|**Required Parameters:**|${command.requiredData || 'None'}|\n`;
2020-09-17 13:44:32 +08:00
// eslint-disable-next-line no-useless-escape
2019-11-08 06:34:38 +08:00
reply += `|**Description:**|${command.info.description || '¯\_(ツ)_/¯'}|\n\n`;
reply += `**Usage:** ${command.info.usage || command.info.name}`;
}
2018-05-13 18:33:22 +08:00
}
2018-06-04 15:07:24 +08:00
// output reply
2018-05-13 18:33:22 +08:00
server.reply({
cmd: 'info',
2019-11-07 15:35:23 +08:00
text: reply,
2020-10-10 13:34:59 +08:00
channel: socket.channel, // @todo Multichannel
2018-05-13 18:33:22 +08:00
}, socket);
2019-11-07 15:35:23 +08:00
return true;
}
2018-05-13 18:33:22 +08:00
2022-06-23 00:32:51 +08:00
/**
* Automatically executes once after server is ready to register this modules hooks
* @param {Object} server - Reference to server enviroment object
* @public
* @return {void}
*/
2019-11-07 15:35:23 +08:00
export function initHooks(server) {
2019-11-08 01:46:55 +08:00
server.registerHook('in', 'chat', this.helpCheck.bind(this), 28);
2019-11-07 15:35:23 +08:00
}
2022-06-23 00:32:51 +08:00
/**
* Executes every time an incoming chat command is invoked;
* hooks chat commands checking for /help
* @param {Object} env - Enviroment object with references to core, server, socket & payload
* @public
* @return {{Object|boolean|string}} Object = same/altered payload,
* false = suppress action,
* string = error
*/
2020-09-17 13:44:32 +08:00
export function helpCheck({
core, server, socket, payload,
}) {
if (typeof payload.text !== 'string') {
return false;
}
if (payload.text.startsWith('/help')) {
2019-11-07 15:35:23 +08:00
const input = payload.text.substr(1).split(' ', 2);
2020-09-17 13:44:32 +08:00
this.run({
core,
server,
socket,
payload: {
cmd: input[0],
command: input[1],
},
});
return false;
}
return payload;
2019-11-07 15:35:23 +08:00
}
2022-06-23 00:32:51 +08:00
/**
* Module meta information
* @public
* @typedef {Object} help/info
* @property {string} name - Module command name
* @property {string} category - Module category name
* @property {string} description - Information about module
* @property {string} usage - Information about module usage
*/
2019-11-07 15:35:23 +08:00
export const info = {
2018-05-13 18:33:22 +08:00
name: 'help',
2022-06-23 00:32:51 +08:00
category: 'core',
description: 'Outputs information about the servers current protocol',
usage: `
API: { cmd: 'help', command: '<optional command name>' }
2019-11-07 15:35:23 +08:00
Text: /help <optional command name>`,
2018-06-04 15:07:24 +08:00
};