TravBot-v3/src/commands/system/help.ts

146 lines
5.4 KiB
TypeScript
Raw Normal View History

2021-03-31 03:22:25 +00:00
import Command from "../../core/command";
import {toTitleCase} from "../../core/lib";
import {loadableCommands, categories} from "../../core/command";
import {getPermissionName} from "../../core/permissions";
export default new Command({
2020-12-15 07:56:09 +00:00
description: "Lists all commands. If a command is specified, their arguments are listed as well.",
2020-12-15 01:44:28 +00:00
usage: "([command, [subcommand/type], ...])",
aliases: ["h"],
async run($) {
const commands = await loadableCommands;
2020-12-15 01:44:28 +00:00
let output = `Legend: \`<type>\`, \`[list/of/stuff]\`, \`(optional)\`, \`(<optional type>)\`, \`([optional/list/...])\``;
2020-10-15 09:23:24 +00:00
2020-12-15 01:44:28 +00:00
for (const [category, headers] of categories) {
output += `\n\n===[ ${toTitleCase(category)} ]===`;
2020-10-15 09:23:24 +00:00
2020-12-15 01:44:28 +00:00
for (const header of headers) {
if (header !== "test") {
const command = commands.get(header);
2020-10-15 09:23:24 +00:00
2020-12-15 01:44:28 +00:00
if (!command)
return console.warn(
`Command "${header}" of category "${category}" unexpectedly doesn't exist!`
);
2020-10-15 09:23:24 +00:00
2020-12-15 01:44:28 +00:00
output += `\n- \`${header}\`: ${command.description}`;
}
}
2020-10-15 09:23:24 +00:00
}
2020-12-15 01:44:28 +00:00
$.channel.send(output, {split: true});
},
any: new Command({
async run($) {
const commands = await loadableCommands;
2020-12-15 01:44:28 +00:00
let header = $.args.shift() as string;
let command = commands.get(header);
if (!command || header === "test") {
$.channel.send(`No command found by the name \`${header}\`!`);
return;
}
2020-12-15 01:44:28 +00:00
2020-12-15 07:56:09 +00:00
if (command.originalCommandName) header = command.originalCommandName;
else console.warn(`originalCommandName isn't defined for ${header}?!`);
2020-12-15 01:44:28 +00:00
2021-03-30 10:54:52 +00:00
let permLevel = command.permission ?? 0;
2020-12-15 01:44:28 +00:00
let usage = command.usage;
let invalid = false;
let selectedCategory = "Unknown";
for (const [category, headers] of categories) {
if (headers.includes(header)) {
if (selectedCategory !== "Unknown")
console.warn(
2020-12-15 01:44:28 +00:00
`Command "${header}" is somehow in multiple categories. This means that the command loading stage probably failed in properly adding categories.`
);
else selectedCategory = toTitleCase(category);
2020-12-15 01:44:28 +00:00
}
}
for (const param of $.args) {
const type = command.resolve(param);
command = command.get(param);
permLevel = command.permission ?? permLevel;
switch (type) {
case Command.TYPES.SUBCOMMAND:
header += ` ${command.originalCommandName}`;
break;
case Command.TYPES.USER:
header += " <user>";
break;
case Command.TYPES.NUMBER:
header += " <number>";
break;
case Command.TYPES.ANY:
header += " <any>";
break;
default:
header += ` ${param}`;
break;
}
if (type === Command.TYPES.NONE) {
invalid = true;
break;
}
}
if (invalid) {
$.channel.send(`No command found by the name \`${header}\`!`);
return;
}
2020-12-15 01:44:28 +00:00
let append = "";
if (usage === "") {
const list: string[] = [];
command.subcommands.forEach((subcmd, subtag) => {
// Don't capture duplicates generated from aliases.
if (subcmd.originalCommandName === subtag) {
2020-12-15 07:56:09 +00:00
const customUsage = subcmd.usage ? ` ${subcmd.usage}` : "";
list.push(`- \`${header} ${subtag}${customUsage}\` - ${subcmd.description}`);
2020-12-15 01:44:28 +00:00
}
});
const addDynamicType = (cmd: Command | null, type: string) => {
if (cmd) {
const customUsage = cmd.usage ? ` ${cmd.usage}` : "";
2020-12-15 07:56:09 +00:00
list.push(`- \`${header} <${type}>${customUsage}\` - ${cmd.description}`);
2020-12-15 01:44:28 +00:00
}
};
addDynamicType(command.user, "user");
addDynamicType(command.number, "number");
addDynamicType(command.any, "any");
2020-12-15 07:56:09 +00:00
append = "Usages:" + (list.length > 0 ? `\n${list.join("\n")}` : " None.");
2020-12-15 01:44:28 +00:00
} else append = `Usage: \`${header} ${usage}\``;
let aliases = "None";
if (command.aliases.length > 0) {
aliases = "";
for (let i = 0; i < command.aliases.length; i++) {
const alias = command.aliases[i];
aliases += `\`${alias}\``;
if (i !== command.aliases.length - 1) aliases += ", ";
}
}
$.channel.send(
2021-03-30 10:54:52 +00:00
`Command: \`${header}\`\nAliases: ${aliases}\nCategory: \`${selectedCategory}\`\nPermission Required: \`${getPermissionName(
permLevel
)}\` (${permLevel})\nDescription: ${command.description}\n${append}`,
2020-12-15 01:44:28 +00:00
{split: true}
2020-10-15 09:23:24 +00:00
);
}
2020-12-15 01:44:28 +00:00
})
2020-10-15 09:23:24 +00:00
});