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