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

91 lines
2.8 KiB
JavaScript
Raw Normal View History

2018-05-13 18:33:22 +08:00
/*
Description: Outputs the current command module list or command categories
*/
// module support functions
2018-05-13 18:33:22 +08:00
const stripIndents = require('common-tags').stripIndents;
// module main
exports.run = async (core, server, socket, payload) => {
// check for spam
if (server._police.frisk(socket.remoteAddress, 2)) {
return server.reply({
cmd: 'warn',
text: 'You are sending too much text. Wait a moment and try again.\nPress the up arrow key to restore your last message.'
}, socket);
}
2018-06-04 15:07:24 +08:00
// verify user input
if (typeof payload.command !== 'undefined' && typeof payload.command !== 'string') {
2018-05-13 18:33:22 +08:00
return;
}
let reply = '';
if (typeof payload.command === 'undefined') {
reply = stripIndents`Listing all current commands. For specific help on certain commands, use either:
Text: /help <command name>
API: {cmd: 'help', command: '<command name>'}`;
reply += '\n\n-------------------------------------\n\n';
2018-05-13 18:33:22 +08:00
let categories = core.commands.categories().sort();
for (let i = 0, j = categories.length; i < j; i++) {
reply += `${categories[i].replace('../src/commands/', '').replace(/^\w/, c => c.toUpperCase())} Commands:\n`;
let catCommands = core.commands.all(categories[i]).sort((a, b) => a.info.name.localeCompare(b.info.name));
reply += ` ${catCommands.map(c => `${c.info.name}`).join(', ')}\n\n`;
}
} else {
let command = core.commands.get(payload.command);
if (typeof command === 'undefined') {
reply = 'Unknown command';
} else {
reply = stripIndents`Name: ${command.info.name}
Aliases: ${typeof command.info.aliases !== 'undefined' ? command.info.aliases.join(', ') : 'None'}
Category: ${command.info.category.replace('../src/commands/', '').replace(/^\w/, c => c.toUpperCase())}
Required Parameters: ${command.requiredData || 'None'}\n
Description: ${command.info.description || '¯\_(ツ)_/¯'}\n
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',
text: reply
}, socket);
};
// module hook functions
exports.initHooks = (server) => {
server.registerHook('in', 'chat', this.helpCheck);
};
// hooks chat commands checking for /whisper
exports.helpCheck = (core, server, socket, payload) => {
if (typeof payload.text !== 'string') {
return false;
}
if (payload.text.startsWith('/help')) {
let input = payload.text.substr(1, payload.text.length).split(' ', 2);
this.run(core, server, socket, {
cmd: input[0],
command: input[1]
});
return false;
}
return payload;
};
// module meta
2018-05-13 18:33:22 +08:00
exports.info = {
name: 'help',
description: 'Outputs information about the servers current protocol',
usage: `
API: { cmd: 'help', command: '<optional command name>' }
Text: /help <optional command name>`
2018-06-04 15:07:24 +08:00
};