discord-bot-cli
Version:
An easy way to build a command-based discord bot with discord.js.
173 lines (172 loc) • 8.08 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.commandRawHelp = exports.commandFullName = exports.defaultHelp = void 0;
const discord_js_1 = require("discord.js");
const array_1 = require("../utils/array");
const template_1 = require("../utils/template");
const reply_1 = require("../utils/reply");
/** @internal */
async function defaultHelp(command, { message, options }) {
const embed = embedHelp(command, options.prefix, options.localization, message);
await reply_1.reply(message, { embed });
}
exports.defaultHelp = defaultHelp;
/**
* Get the command full name.
* @category Utils
* @param command
* @returns The command's full name.
*/
function commandFullName(command) {
return command
.getParents()
.map(cmd => cmd.name)
.join(" ");
}
exports.commandFullName = commandFullName;
/**
* Extracts raw data for command help.
* @category Utils
* @param command
* @param localization
* @returns Raw help datas.
*/
function commandRawHelp(command, localization) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
const commandLocalization = (_a = localization.commands[command.name]) !== null && _a !== void 0 ? _a : {};
const fullName = commandFullName(command);
const description = (_b = commandLocalization.description) !== null && _b !== void 0 ? _b : command.description;
const collection = (_d = (_c = command.parent) === null || _c === void 0 ? void 0 : _c.subs) !== null && _d !== void 0 ? _d : command.commandSet.commands;
const aliases = command.aliases.filter(a => collection.hasAlias(a));
const args = Array.from(command.args.entries()).map(([name, arg]) => argRawHelp(arg, name, commandLocalization, localization.typeNames));
const flags = Array.from(command.flags.entries()).map(([name, flag]) => flagRawHelp(flag, name, commandLocalization, localization.typeNames));
const subs = Array.from(command.subs.values()).map(c => commandRawHelp(c, localization));
let rest = undefined;
if (command.rest) {
const name = (_f = (_e = commandLocalization === null || commandLocalization === void 0 ? void 0 : commandLocalization.rest) === null || _e === void 0 ? void 0 : _e.name) !== null && _f !== void 0 ? _f : command.rest.name;
const description = (_j = (_h = (_g = commandLocalization === null || commandLocalization === void 0 ? void 0 : commandLocalization.rest) === null || _g === void 0 ? void 0 : _g.description) !== null && _h !== void 0 ? _h : command.rest.description) !== null && _j !== void 0 ? _j : "";
const usageString = `[...${name}]`;
const typeNames = array_1.isArray(command.rest.type)
? command.rest.type.map(t => localization.typeNames[t])
: [localization.typeNames[command.rest.type]];
rest = { name, description, usageString, typeNames };
}
const tags = [];
if (command.devOnly)
tags.push(localization.help.tags.devOnly);
if (command.guildOnly)
tags.push(localization.help.tags.guildOnly);
return {
command,
fullName,
aliases,
description,
args,
flags,
subs,
rest,
tags,
};
}
exports.commandRawHelp = commandRawHelp;
/** @internal */
function argRawHelp(arg, name, localization, typeNamesLocalization) {
var _a, _b, _c, _d, _e;
const argLocalization = (_b = ((_a = localization.args) !== null && _a !== void 0 ? _a : {})[name]) !== null && _b !== void 0 ? _b : {};
const typeNames = array_1.isArray(arg.type)
? arg.type.map(t => typeNamesLocalization[t])
: [typeNamesLocalization[arg.type]];
const localizedName = (_c = argLocalization.name) !== null && _c !== void 0 ? _c : name;
const description = (_e = (_d = argLocalization.description) !== null && _d !== void 0 ? _d : arg.description) !== null && _e !== void 0 ? _e : "";
let usageString;
if (arg.optional) {
const defaultValue = arg.defaultValue ? ` = ${arg.defaultValue}` : "";
usageString = `[${localizedName}${defaultValue}]`;
}
else {
usageString = `<${localizedName}>`;
}
return {
arg,
typeNames: typeNames,
name,
localizedName,
description,
usageString,
};
}
/** @internal */
function flagRawHelp(flag, name, localization, typeNamesLocalization) {
var _a, _b, _c, _d, _e;
const flagLocalization = (_b = ((_a = localization.flags) !== null && _a !== void 0 ? _a : {})[name]) !== null && _b !== void 0 ? _b : {};
const typeNames = array_1.isArray(flag.type)
? flag.type.map(t => typeNamesLocalization[t])
: [typeNamesLocalization[flag.type]];
const localizedName = (_c = flagLocalization.name) !== null && _c !== void 0 ? _c : name;
const description = (_e = (_d = flagLocalization.description) !== null && _d !== void 0 ? _d : flag.description) !== null && _e !== void 0 ? _e : "";
const longUsageString = `--${name}`;
const shortUsageString = flag.shortcut ? `-${flag.shortcut}` : undefined;
return {
flag,
typeNames,
name,
localizedName,
description,
longUsageString,
shortUsageString,
};
}
/** @internal */
function embedHelp(command, prefix, localization, message) {
const rawHelp = commandRawHelp(command, localization);
const embed = new discord_js_1.MessageEmbed()
.setTitle(rawHelp.fullName)
.setDescription(rawHelp.description +
(rawHelp.tags.length === 0 ? "" : "\n\n" + rawHelp.tags.map(t => `\`${t}\``).join(" ")));
if (command.hasExecutor) {
const usageString = prefix +
rawHelp.fullName +
(rawHelp.args.length === 0 ? "" : " " + rawHelp.args.map(a => a.usageString).join(" ")) +
(rawHelp.rest ? " " + rawHelp.rest.usageString : "");
embed.addField(localization.help.usage, `**\`${usageString}\`**` + (rawHelp.args.length !== 0 ? `\n\n${localization.help.argUsageHint}` : ""), false);
}
const args = rawHelp.args
.map(a => `\`${a.name}\` *${a.typeNames.join(" | ")}*` +
(a.description !== "" ? `\n⮩ ${a.description}` : ""))
.join("\n") +
(rawHelp.rest
? `\n\`${rawHelp.rest.name}\` *${template_1.template(localization.help.restTypeName, {
type: rawHelp.rest.typeNames.join(" | "),
})}*` + (rawHelp.rest.description !== "" ? `\n⮩ ${rawHelp.rest.description}` : "")
: "");
if (args !== "")
embed.addField(localization.help.arguments, args, true);
const flags = rawHelp.flags
.map(f => `\`--${f.name}\`` +
(f.flag.shortcut ? ` \`-${f.flag.shortcut}\`` : "") +
` *${f.typeNames.join(" | ")}*` +
(f.description !== "" ? `\n⮩ ${f.description}` : ""))
.join("\n");
if (flags !== "")
embed.addField(localization.help.flags, flags, true);
const subs = (message ? rawHelp.subs.filter(s => s.command.checkPermissions(message)) : rawHelp.subs)
.map(s => `\`${s.command.name}\`` + (s.description !== "" ? ` ${s.description}` : ""))
.join("\n");
if (subs !== "")
embed.addField(localization.help.subCommands, subs, false);
const aliases = rawHelp.aliases.map(a => `\`${a}\``).join(" ");
if (aliases !== "")
embed.addField(localization.help.aliases, aliases, false);
const clientPermissions = rawHelp.command.clientPermissions.map(p => `\`${p}\``).join(" ");
if (clientPermissions !== "")
embed.addField(localization.help.bot_permissions, clientPermissions, false);
if (rawHelp.command.userPermissions) {
const userPermissions = rawHelp.command.userPermissions.map(p => `\`${p}\``).join(" ");
if (userPermissions !== "")
embed.addField(localization.help.user_permissions, userPermissions, false);
}
const exemples = rawHelp.command.examples.map(e => `\`${e}\``).join("\n");
if (exemples !== "")
embed.addField(localization.help.examples, exemples, false);
return embed;
}