2021-04-05 08:46:50 +00:00
|
|
|
import {Command, NamedCommand, loadableCommands, categories, getPermissionName, CHANNEL_TYPE} from "../../core";
|
|
|
|
import {toTitleCase, requireAllCasesHandledFor} from "../../lib";
|
2020-07-25 08:15:26 +00:00
|
|
|
|
2021-04-05 03:40:31 +00:00
|
|
|
export default new NamedCommand({
|
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"],
|
2021-04-05 04:35:12 +00:00
|
|
|
async run({message, channel, guild, author, member, client, args}) {
|
2021-01-26 09:52:39 +00:00
|
|
|
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) {
|
2021-04-05 08:46:50 +00:00
|
|
|
let tmp = `\n\n===[ ${toTitleCase(category)} ]===`;
|
|
|
|
// Ignore empty categories, including ["test"].
|
|
|
|
let hasActualCommands = false;
|
2020-10-15 09:23:24 +00:00
|
|
|
|
2020-12-15 01:44:28 +00:00
|
|
|
for (const header of headers) {
|
|
|
|
if (header !== "test") {
|
2021-04-05 03:40:31 +00:00
|
|
|
const command = commands.get(header)!;
|
2021-04-05 08:46:50 +00:00
|
|
|
tmp += `\n- \`${header}\`: ${command.description}`;
|
|
|
|
hasActualCommands = true;
|
2020-12-15 01:44:28 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-05 08:46:50 +00:00
|
|
|
|
|
|
|
if (hasActualCommands) output += tmp;
|
2020-10-15 09:23:24 +00:00
|
|
|
}
|
|
|
|
|
2021-04-05 04:35:12 +00:00
|
|
|
channel.send(output, {split: true});
|
2020-12-15 01:44:28 +00:00
|
|
|
},
|
|
|
|
any: new Command({
|
2021-04-05 04:35:12 +00:00
|
|
|
async run({message, channel, guild, author, member, client, args}) {
|
2021-04-05 03:40:31 +00:00
|
|
|
// Setup the root command
|
|
|
|
const commands = await loadableCommands;
|
2021-04-05 04:35:12 +00:00
|
|
|
let header = args.shift() as string;
|
2021-04-05 03:40:31 +00:00
|
|
|
let command = commands.get(header);
|
2021-04-05 04:35:12 +00:00
|
|
|
if (!command || header === "test") return channel.send(`No command found by the name \`${header}\`.`);
|
2021-04-05 03:40:31 +00:00
|
|
|
if (!(command instanceof NamedCommand))
|
2021-04-05 04:35:12 +00:00
|
|
|
return channel.send(`Command is not a proper instance of NamedCommand.`);
|
2021-04-05 03:40:31 +00:00
|
|
|
if (command.name) header = command.name;
|
|
|
|
|
|
|
|
// Search categories
|
|
|
|
let category = "Unknown";
|
|
|
|
for (const [referenceCategory, headers] of categories) {
|
|
|
|
if (headers.includes(header)) {
|
|
|
|
category = toTitleCase(referenceCategory);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Gather info
|
2021-04-05 04:35:12 +00:00
|
|
|
const result = await command.resolveInfo(args);
|
2021-04-05 03:40:31 +00:00
|
|
|
|
2021-04-05 04:35:12 +00:00
|
|
|
if (result.type === "error") return channel.send(result.message);
|
2020-12-15 01:44:28 +00:00
|
|
|
|
|
|
|
let append = "";
|
2021-04-05 03:40:31 +00:00
|
|
|
command = result.command;
|
2020-12-15 01:44:28 +00:00
|
|
|
|
2021-04-05 08:46:50 +00:00
|
|
|
if (result.args.length > 0) header += " " + result.args.join(" ");
|
|
|
|
|
2021-04-05 03:40:31 +00:00
|
|
|
if (command.usage === "") {
|
2020-12-15 01:44:28 +00:00
|
|
|
const list: string[] = [];
|
|
|
|
|
2021-04-05 03:40:31 +00:00
|
|
|
for (const [tag, subcommand] of result.keyedSubcommandInfo) {
|
|
|
|
const customUsage = subcommand.usage ? ` ${subcommand.usage}` : "";
|
|
|
|
list.push(`- \`${header} ${tag}${customUsage}\` - ${subcommand.description}`);
|
|
|
|
}
|
2020-12-15 01:44:28 +00:00
|
|
|
|
2021-04-05 03:40:31 +00:00
|
|
|
for (const [type, subcommand] of result.subcommandInfo) {
|
|
|
|
const customUsage = subcommand.usage ? ` ${subcommand.usage}` : "";
|
|
|
|
list.push(`- \`${header} ${type}${customUsage}\` - ${subcommand.description}`);
|
|
|
|
}
|
2020-12-15 01:44:28 +00:00
|
|
|
|
2020-12-15 07:56:09 +00:00
|
|
|
append = "Usages:" + (list.length > 0 ? `\n${list.join("\n")}` : " None.");
|
2021-04-05 03:40:31 +00:00
|
|
|
} else {
|
|
|
|
append = `Usage: \`${header} ${command.usage}\``;
|
|
|
|
}
|
|
|
|
|
|
|
|
let aliases = "N/A";
|
2020-12-15 01:44:28 +00:00
|
|
|
|
2021-04-05 03:40:31 +00:00
|
|
|
if (command instanceof NamedCommand) {
|
|
|
|
const formattedAliases: string[] = [];
|
|
|
|
for (const alias of command.aliases) formattedAliases.push(`\`${alias}\``);
|
|
|
|
// Short circuit an empty string, in this case, if there are no aliases.
|
|
|
|
aliases = formattedAliases.join(", ") || "None";
|
|
|
|
}
|
2020-12-15 01:44:28 +00:00
|
|
|
|
2021-04-05 04:35:12 +00:00
|
|
|
return channel.send(
|
2021-04-05 03:40:31 +00:00
|
|
|
`Command: \`${header}\`\nAliases: ${aliases}\nCategory: \`${category}\`\nPermission Required: \`${getPermissionName(
|
|
|
|
result.permission
|
2021-04-05 08:46:50 +00:00
|
|
|
)}\` (${result.permission})\nChannel Type: ${getChannelTypeName(result.channelType)}\nNSFW Only: ${
|
|
|
|
result.nsfw ? "Yes" : "No"
|
|
|
|
}\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
|
|
|
});
|
2021-04-05 08:46:50 +00:00
|
|
|
|
|
|
|
function getChannelTypeName(type: CHANNEL_TYPE): string {
|
|
|
|
switch (type) {
|
|
|
|
case CHANNEL_TYPE.ANY:
|
|
|
|
return "Any";
|
|
|
|
case CHANNEL_TYPE.GUILD:
|
|
|
|
return "Guild Only";
|
|
|
|
case CHANNEL_TYPE.DM:
|
|
|
|
return "DM Only";
|
|
|
|
default:
|
|
|
|
requireAllCasesHandledFor(type);
|
|
|
|
}
|
|
|
|
}
|