forked from engineering/cloudservices
New subcommand support
parent
29c7b0a5f9
commit
ab7008b429
|
@ -35,15 +35,45 @@ export default class Util {
|
|||
return result;
|
||||
}
|
||||
|
||||
public resolveCommand(command: string): Command {
|
||||
if (this.client.commands.has(command)) return this.client.commands.get(command);
|
||||
for (const cmd of this.client.commands.values()) {
|
||||
if (!cmd.aliases) continue;// eslint-disable-line no-continue
|
||||
for (const alias of cmd.aliases) {
|
||||
if (command === alias.toLowerCase()) return cmd;
|
||||
/**
|
||||
* Resolves a command
|
||||
* @param command Parent command label
|
||||
* @param args Use to resolve subcommands
|
||||
* @param message Only used to check for errors
|
||||
*/
|
||||
public resolveCommand(command: string, args?: string[], message?: Message): Promise<{cmd: Command, args: string[] }> {
|
||||
try {
|
||||
let resolvedCommand: Command;
|
||||
|
||||
if (this.client.commands.has(command)) resolvedCommand = this.client.commands.get(command);
|
||||
else {
|
||||
for (const cmd of this.client.commands.toArray()) {
|
||||
if (cmd.aliases.includes(command)) { resolvedCommand = cmd; break; }
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
if (!resolvedCommand) return Promise.resolve({ cmd: null, args });
|
||||
|
||||
let hasSubCommands = true;
|
||||
while (hasSubCommands) {
|
||||
if (!resolvedCommand.subcommands.size) {
|
||||
hasSubCommands = false; break;
|
||||
} else if (resolvedCommand.subcommands.has(args[0])) {
|
||||
resolvedCommand = resolvedCommand.subcommands.get(args[0]);
|
||||
args.shift();
|
||||
} else {
|
||||
for (const subCmd of resolvedCommand.subcommands.toArray()) {
|
||||
if (subCmd.aliases.includes(args[0])) {
|
||||
resolvedCommand = subCmd; args.shift(); break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Promise.resolve({ cmd: resolvedCommand, args });
|
||||
} catch (error) {
|
||||
this.handleError(error, message);
|
||||
return Promise.reject(error);
|
||||
}
|
||||
}
|
||||
|
||||
public async handleError(error: Error, message?: Message, command?: Command): Promise<void> {
|
||||
|
|
|
@ -16,11 +16,10 @@ export default class Help extends Command {
|
|||
// eslint-disable-next-line consistent-return
|
||||
public async run(message: Message, args?: string[]) {
|
||||
try {
|
||||
const cmd = args.filter((c) => c)[0];
|
||||
if (!cmd) {
|
||||
if (!args[0]) {
|
||||
const cmdList: Command[] = [];
|
||||
this.client.commands.forEach((c) => cmdList.push(c));
|
||||
const commands = cmdList.map((c) => {
|
||||
const commands = this.client.commands.map((c: Command) => {
|
||||
const aliases = c.aliases.map((alias) => `${this.client.config.prefix}${alias}`).join(', ');
|
||||
const perms: string[] = [];
|
||||
let allowedRoles = c.permissions && c.permissions.roles && c.permissions.roles.map((r) => `<@&${r}>`).join(', ');
|
||||
|
@ -45,19 +44,19 @@ export default class Help extends Command {
|
|||
if (cmdPages.length === 1) return message.channel.createMessage({ embed: cmdPages[0] });
|
||||
return createPaginationEmbed(message, this.client, cmdPages);
|
||||
}
|
||||
const foundCommand = this.client.util.resolveCommand(cmd);
|
||||
if (!foundCommand) return message.channel.createMessage(`${this.client.stores.emojis.error} **Command not found!**`);
|
||||
const { cmd } = await this.client.util.resolveCommand(args[0], args, message);
|
||||
if (!cmd) return message.channel.createMessage(`${this.client.stores.emojis.error} **Command not found!**`);
|
||||
const perms: string[] = [];
|
||||
let allowedRoles = foundCommand.permissions && foundCommand.permissions.roles && foundCommand.permissions.roles.map((r) => `<@&${r}>`).join(', ');
|
||||
let allowedRoles = cmd.permissions && cmd.permissions.roles && cmd.permissions.roles.map((r) => `<@&${r}>`).join(', ');
|
||||
if (allowedRoles) { allowedRoles = `**Roles:** ${allowedRoles}`; perms.push(allowedRoles); }
|
||||
let allowedUsers = foundCommand.permissions && foundCommand.permissions.users && foundCommand.permissions.users.map((u) => `<@${u}>`).join(', ');
|
||||
let allowedUsers = cmd.permissions && cmd.permissions.users && cmd.permissions.users.map((u) => `<@${u}>`).join(', ');
|
||||
if (allowedUsers) { allowedUsers = `**Users:** ${allowedUsers}`; perms.push(allowedUsers); }
|
||||
const displayedPerms = perms.length ? `**Permissions:**\n${perms.join('\n')}` : '';
|
||||
const aliases = foundCommand.aliases.map((alias) => `${this.client.config.prefix}${alias}`).join(', ');
|
||||
const aliases = cmd.aliases.map((alias) => `${this.client.config.prefix}${alias}`).join(', ');
|
||||
const embed = new RichEmbed();
|
||||
embed.setTimestamp(); embed.setFooter(`Requested by ${message.author.username}#${message.author.discriminator}`, message.author.avatarURL);
|
||||
embed.setTitle(`${this.client.config.prefix}${foundCommand.name}`); embed.setAuthor(`${this.client.user.username}#${this.client.user.discriminator}`, this.client.user.avatarURL);
|
||||
const description = `**Description**: ${foundCommand.description}\n**Usage:** ${foundCommand.usage}\n**Aliases:** ${aliases}\n${displayedPerms}`;
|
||||
embed.setTitle(`${this.client.config.prefix}${cmd.name}`); embed.setAuthor(`${this.client.user.username}#${this.client.user.discriminator}`, this.client.user.avatarURL);
|
||||
const description = `**Description**: ${cmd.description}\n**Usage:** ${cmd.usage}\n**Aliases:** ${aliases}\n${displayedPerms}`;
|
||||
embed.setDescription(description);
|
||||
// @ts-ignore
|
||||
message.channel.createMessage({ embed });
|
||||
|
|
|
@ -7,8 +7,11 @@ export { default as Exec } from './exec';
|
|||
export { default as Help } from './help';
|
||||
export { default as Lock } from './lock';
|
||||
export { default as Modlogs } from './modlogs';
|
||||
export { default as Notify } from './notify';
|
||||
export { default as Parse } from './parse';
|
||||
export { default as Ping } from './ping';
|
||||
export { default as Pull } from './pull';
|
||||
export { default as Sysinfo } from './sysinfo';
|
||||
export { default as Unlock } from './unlock';
|
||||
export { default as Warn } from './warn';
|
||||
export { default as Whois } from './whois';
|
||||
|
|
|
@ -15,30 +15,30 @@ export default class {
|
|||
if (message.content.indexOf(this.client.config.prefix) !== 0) return;
|
||||
const noPrefix: string[] = message.content.slice(this.client.config.prefix.length).trim().split(/ +/g);
|
||||
const command: string = noPrefix[0].toLowerCase();
|
||||
const resolved: Command = this.client.util.resolveCommand(command);
|
||||
if (!resolved) return;
|
||||
if (resolved.guildOnly && !(message.channel instanceof TextChannel)) return;
|
||||
const args: string[] = noPrefix.slice(1);
|
||||
const resolved = await this.client.util.resolveCommand(command, args, message);
|
||||
if (!resolved.cmd) return;
|
||||
if (resolved.cmd.guildOnly && !(message.channel instanceof TextChannel)) return;
|
||||
let hasUserPerms: boolean;
|
||||
if (resolved.permissions.users) {
|
||||
hasUserPerms = resolved.permissions.users.includes(message.author.id);
|
||||
if (resolved.cmd.permissions.users) {
|
||||
hasUserPerms = resolved.cmd.permissions.users.includes(message.author.id);
|
||||
}
|
||||
let hasRolePerms: boolean = false;
|
||||
if (resolved.permissions.roles) {
|
||||
for (const role of resolved.permissions.roles) {
|
||||
if (resolved.cmd.permissions.roles) {
|
||||
for (const role of resolved.cmd.permissions.roles) {
|
||||
if (message.member && message.member.roles.includes(role)) {
|
||||
// this.client.signale.debug(message.member.roles.includes(role));
|
||||
hasRolePerms = true; break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!resolved.permissions.users && !resolved.permissions.roles) {
|
||||
if (!resolved.cmd.permissions.users && !resolved.cmd.permissions.roles) {
|
||||
hasUserPerms = true;
|
||||
hasRolePerms = true;
|
||||
}
|
||||
if (!hasRolePerms && !hasUserPerms) return;
|
||||
if (!resolved.enabled) { message.channel.createMessage(`***${this.client.stores.emojis.error} This command has been disabled***`); return; }
|
||||
const args: string[] = noPrefix.slice(1);
|
||||
resolved.run(message, args);
|
||||
if (!resolved.cmd.enabled) { message.channel.createMessage(`***${this.client.stores.emojis.error} This command has been disabled***`); return; }
|
||||
resolved.cmd.run(message, resolved.args);
|
||||
} catch (error) {
|
||||
this.client.util.handleError(error, message);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue