50 lines
2.0 KiB
JavaScript
Raw Normal View History

2025-03-16 15:02:23 +01:00
const _ = require('underscore');
const helptext = `The \`!help\` command can be used to get a list of commands, or to get specific help text using \`!help <command>\`
Also see the \`!alias\` command for extra simplifications.`
const ex = /^[\w\d]+$/
function help(context) {
return new Promise((resolve, reject) => {
if(context.isHelpRequest) {
context.message.reply(helptext)
.then(() => {
console.info(`[${context.id}] ? help`);
resolve();
}).catch(err => reject(err));
} else if(!context.args) {
let text = 'Listing commands, use \`!help <command>\` for detailed information. Use \`!alias\` to find easy shortcut commands\n```';
_.each(_.keys(context.commands), key => text += `\n${key}`);
text += '\n```'
context.message.reply(text)
.then(() => {
console.info(`[${context.id}] List commands -> [${context.pid}]`)
resolve();
}).catch(err => reject(err));
} else {
let match = ex.exec(context.args);
if(match && _.has(context.commands, context.args)) {
console.info(`[${context.id}] Passing help request to <${context.args}>`);
context.isHelpRequest = true;
context.commands[context.args](context);
} else if (match && _.has(context.alias, context.args)) {
console.info(`[${context.id}] Alias help request to <${context.command}>`);
context.message.reply(`\`!${context.args}\`: ${context.alias[context.args].desc}`)
.then(() => {
console.info(`[${context.id}] Alias -> [${context.pid}]`);
resolve();
}).catch(err => reject(err));
} else {
context.message.reply(`No such command <${context.args}>`)
.then(() => {
console.info(`[${context.id}] No command -> [${context.pid}]`)
resolve();
}).catch(err => reject(err));
}
}
});
}
module.exports = commands => {
commands.help = help;
};