TravBot-v3/src/commands/help.ts

136 lines
5.3 KiB
TypeScript
Raw Normal View History

2020-12-15 01:44:28 +00:00
import Command from "../core/command";
import {CommonLibrary} from "../core/lib";
import {loadCommands, categories} from "../core/command";
import {PermissionNames} 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"],
2020-10-15 09:23:24 +00:00
async run($: CommonLibrary): Promise<any> {
2020-12-15 01:44:28 +00:00
const commands = await loadCommands();
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===[ ${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)
2020-12-15 07:56:09 +00:00
return $.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($: CommonLibrary): Promise<any> {
const commands = await loadCommands();
let header = $.args.shift() as string;
let command = commands.get(header);
2020-12-15 07:56:09 +00:00
if (!command || header === "test") return $.channel.send(`No command found by the name \`${header}\`!`);
2020-12-15 01:44:28 +00:00
2020-12-15 07:56:09 +00:00
if (command.originalCommandName) header = command.originalCommandName;
2020-12-15 01:44:28 +00:00
else $.warn(`originalCommandName isn't defined for ${header}?!`);
let permLevel = command.permission ?? Command.PERMISSIONS.NONE;
let usage = command.usage;
let invalid = false;
let selectedCategory = "Unknown";
for (const [category, headers] of categories) {
if (headers.includes(header)) {
if (selectedCategory !== "Unknown")
$.warn(
`Command "${header}" is somehow in multiple categories. This means that the command loading stage probably failed in properly adding categories.`
);
else selectedCategory = category;
}
}
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;
}
}
2020-12-15 07:56:09 +00:00
if (invalid) return $.channel.send(`No command found by the name \`${header}\`!`);
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(
`Command: \`${header}\`\nAliases: ${aliases}\nCategory: \`${selectedCategory}\`\nPermission Required: \`${PermissionNames[permLevel]}\` (${permLevel})\nDescription: ${command.description}\n${append}`,
{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
});