rewrite command resolver
parent
a283542048
commit
a718bf23b3
|
@ -38,50 +38,30 @@ export default class Util {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Resolves a command
|
* Resolves a command
|
||||||
* @param command Parent command label
|
* @param query Command input
|
||||||
* @param args Use to resolve subcommands
|
|
||||||
* @param message Only used to check for errors
|
* @param message Only used to check for errors
|
||||||
*/
|
*/
|
||||||
public resolveCommand(command: string, args?: string[], message?: Message): Promise<{cmd: Command, args: string[] }> {
|
public resolveCommand(query: string | string[], message?: Message): Promise<{cmd: Command, args: string[] }> {
|
||||||
try {
|
try {
|
||||||
let resolvedCommand: Command;
|
let resolvedCommand: Command;
|
||||||
|
if (typeof query === 'string') query = query.split(' ');
|
||||||
|
query = query.map((q) => q.toLowerCase());
|
||||||
|
const commands = this.client.commands.toArray();
|
||||||
|
resolvedCommand = commands.find((c) => c.name === query[0] || c.aliases.includes(query[0]));
|
||||||
|
|
||||||
if (this.client.commands.has(command)) resolvedCommand = this.client.commands.get(command);
|
if (!resolvedCommand) return Promise.resolve(null);
|
||||||
else {
|
query.shift();
|
||||||
for (const cmd of this.client.commands.toArray()) {
|
while (resolvedCommand.subcommands.size) {
|
||||||
if (cmd.aliases.includes(command)) { resolvedCommand = cmd; break; }
|
const subCommands = resolvedCommand.subcommands.toArray();
|
||||||
}
|
const found = subCommands.find((c) => c.name === query[0] || c.aliases.includes(query[0]));
|
||||||
|
if (!found) break;
|
||||||
|
resolvedCommand = found;
|
||||||
|
query.shift();
|
||||||
}
|
}
|
||||||
if (!resolvedCommand) return Promise.resolve({ cmd: null, args });
|
return Promise.resolve({ cmd: resolvedCommand, args: query });
|
||||||
|
|
||||||
let parentLabel = '';
|
|
||||||
let hasSubCommands = true;
|
|
||||||
while (hasSubCommands) {
|
|
||||||
if (!resolvedCommand.subcommands.size) {
|
|
||||||
hasSubCommands = false; break;
|
|
||||||
} else if (!args[0]) {
|
|
||||||
hasSubCommands = false; break;
|
|
||||||
} else if (resolvedCommand.subcommands.has(args[0])) {
|
|
||||||
parentLabel += `${resolvedCommand.name} `;
|
|
||||||
resolvedCommand = resolvedCommand.subcommands.get(args[0]); args.shift();
|
|
||||||
} else {
|
|
||||||
const subcommandArray = resolvedCommand.subcommands.toArray();
|
|
||||||
for (const subCmd of subcommandArray) {
|
|
||||||
if (subCmd.aliases.includes(args[0])) {
|
|
||||||
parentLabel += `${resolvedCommand.name} `; resolvedCommand = subCmd; args.shift(); break;
|
|
||||||
}
|
|
||||||
if (subcommandArray.findIndex((v) => v === subCmd) === subcommandArray.length - 1) {
|
|
||||||
hasSubCommands = false; break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
const finalCommand = resolvedCommand;
|
|
||||||
finalCommand.parentName = parentLabel;
|
|
||||||
|
|
||||||
return Promise.resolve({ cmd: finalCommand, args });
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.handleError(error, message);
|
if (message) this.handleError(error, message);
|
||||||
|
else this.handleError(error);
|
||||||
return Promise.reject(error);
|
return Promise.reject(error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -107,9 +87,7 @@ export default class Util {
|
||||||
}
|
}
|
||||||
await this.client.createMessage('595788220764127272', info);
|
await this.client.createMessage('595788220764127272', info);
|
||||||
const msg = message.content.slice(this.client.config.prefix.length).trim().split(/ +/g);
|
const msg = message.content.slice(this.client.config.prefix.length).trim().split(/ +/g);
|
||||||
const label = msg[0];
|
if (command) this.resolveCommand(msg).then((c) => { c.cmd.enabled = false; });
|
||||||
const args = msg.slice(1);
|
|
||||||
if (command) this.resolveCommand(label, args).then((c) => { c.cmd.enabled = false; });
|
|
||||||
if (message) message.channel.createMessage(`***${this.client.stores.emojis.error} An unexpected error has occured - please contact a member of the Engineering Team.${command ? ' This command has been disabled.' : ''}***`);
|
if (message) message.channel.createMessage(`***${this.client.stores.emojis.error} An unexpected error has occured - please contact a member of the Engineering Team.${command ? ' This command has been disabled.' : ''}***`);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
this.client.signale.error(err);
|
this.client.signale.error(err);
|
||||||
|
|
|
@ -44,7 +44,7 @@ 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 { cmd } = await this.client.util.resolveCommand(args[0], args.slice(1), message);
|
const { cmd } = await this.client.util.resolveCommand(args, message);
|
||||||
if (!cmd) 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 = cmd.permissions && cmd.permissions.roles && cmd.permissions.roles.map((r) => `<@&${r}>`).join(', ');
|
let allowedRoles = cmd.permissions && cmd.permissions.roles && cmd.permissions.roles.map((r) => `<@&${r}>`).join(', ');
|
||||||
|
|
|
@ -14,9 +14,7 @@ export default class {
|
||||||
if (message.author.bot) return;
|
if (message.author.bot) return;
|
||||||
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 resolved = await this.client.util.resolveCommand(noPrefix, message);
|
||||||
const args: string[] = noPrefix.slice(1);
|
|
||||||
const resolved = await this.client.util.resolveCommand(command, args, message);
|
|
||||||
if (!resolved.cmd) return;
|
if (!resolved.cmd) return;
|
||||||
if (resolved.cmd.guildOnly && !(message.channel instanceof TextChannel)) return;
|
if (resolved.cmd.guildOnly && !(message.channel instanceof TextChannel)) return;
|
||||||
let hasUserPerms: boolean;
|
let hasUserPerms: boolean;
|
||||||
|
|
Loading…
Reference in New Issue