schat2-clive/src/games/help.js

51 lines
1.7 KiB
JavaScript
Executable File

'use strict';
const config = require('config');
const { version } = require('../../package.json');
const style = require('../utils/style');
function capitalize(string) {
if (typeof string !== 'string') {
return string;
}
return `${string.slice(0, 1).toUpperCase()}${string.slice(1)}`;
}
function onCommand(args, context) {
if (context.subcommand) {
const game = context.games[context.subcommand];
if (game?.help) {
context.sendMessage(`${style.bold(`${capitalize(game.name || game.key)}:`)} ${game.help}`, context.room.id, { styleCommands: true });
return;
}
if (game) {
context.sendMessage(`No help available for '${capitalize(game.name || game.key)}'.`, context.room.id, { styleCommands: true });
return;
}
context.sendMessage(`No game '${capitalize(context.subcommand)}' known.`, context.room.id, { styleCommands: true });
return;
}
const commandsByGame = Object.entries(context.games)
.filter(([command, game]) => !command.includes('-') && !game.restricted)
.reduce((acc, [command, game]) => ({ ...acc, [game.name]: [...(acc[game.name] || []), command] }), {});
const commands = Object.entries(commandsByGame)
.map(([gameName, gameCommands]) => `${capitalize(gameName)}: ${gameCommands.map((command) => `${config.prefix}${command}`).join(', ')}`)
.join(' | ');
context.sendMessage(`${commands}`, context.room.id, { styleCommands: true });
context.sendMessage(`Try ${config.prefix}game:help for more info, ${config.prefix}game:score [username] and ${config.prefix}game:lead for scores. | ${config.user.username} v${version} | operated by ${config.operators[0]}`, context.room.id, { styleCommands: true });
}
module.exports = {
onCommand,
commands: ['commands'],
};