From 9eca427ce96f2e0129992247855b46cf6b281fc0 Mon Sep 17 00:00:00 2001 From: Bsian Date: Tue, 29 Oct 2019 20:12:01 +0000 Subject: [PATCH 1/4] Prepare for subcommand --- src/class/Command.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/class/Command.ts b/src/class/Command.ts index ba3ee3f..a6cec05 100644 --- a/src/class/Command.ts +++ b/src/class/Command.ts @@ -18,6 +18,8 @@ export default class Command { guildOnly?: boolean + subcommands: Command[] + public run(message: Message, args: string[]) {} // eslint-disable-line constructor(client: Client) { @@ -28,6 +30,7 @@ export default class Command { this.aliases = []; this.guildOnly = true; this.client = client; + this.subcommands = []; this.permissions = {}; } } From f0385ce17723bead3beaaa7392df5234163042b6 Mon Sep 17 00:00:00 2001 From: Bsian Date: Tue, 29 Oct 2019 20:14:02 +0000 Subject: [PATCH 2/4] Stop creating another message --- src/commands/modlogs.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/commands/modlogs.ts b/src/commands/modlogs.ts index f326669..ee5ea30 100644 --- a/src/commands/modlogs.ts +++ b/src/commands/modlogs.ts @@ -54,7 +54,7 @@ export default class Modlogs extends Command { if (embeds.length === 1) { // @ts-ignore - message.channel.createMessage({ embed: embeds[0] }); + msg.edit({ content: '', embed: embeds[0] }); } else { createPaginationEmbed(message, this.client, embeds, {}, msg); } From e7437d4b59ddba3067a5df040e32f54f957d16db Mon Sep 17 00:00:00 2001 From: Bsian Date: Tue, 29 Oct 2019 20:16:08 +0000 Subject: [PATCH 3/4] Fixed exec failure --- src/commands/exec.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/commands/exec.ts b/src/commands/exec.ts index 6728171..17ae8ca 100644 --- a/src/commands/exec.ts +++ b/src/commands/exec.ts @@ -18,7 +18,12 @@ export default class Exec extends Command { if (!args.length) return this.client.commands.get('help').run(message, [this.name]); const response = await message.channel.createMessage(`${this.client.stores.emojis.loading} ***Executing \`${args.join(' ')}\`***`); - const result = await this.client.util.exec(args.join(' ')); + let result: string; + try { + result = await this.client.util.exec(args.join(' ')); + } catch (error) { + result = error.message; + } if (result.length <= 1975) return response.edit(`\`\`\`bash\n${result}\n\`\`\``); const splitResult = this.client.util.splitString(result, 1975); From 3f001eebd5d341b7d31cda8094f48637ec1cf259 Mon Sep 17 00:00:00 2001 From: Bsian Date: Tue, 29 Oct 2019 21:44:12 +0000 Subject: [PATCH 4/4] Added support for subcommands --- src/class/Command.ts | 4 ++-- src/class/Util.ts | 46 +++++++++++++++++++++++++++++++------ src/events/messageCreate.ts | 20 ++++++++-------- 3 files changed, 51 insertions(+), 19 deletions(-) diff --git a/src/class/Command.ts b/src/class/Command.ts index a6cec05..e17a29e 100644 --- a/src/class/Command.ts +++ b/src/class/Command.ts @@ -18,7 +18,7 @@ export default class Command { guildOnly?: boolean - subcommands: Command[] + subcommands: Map public run(message: Message, args: string[]) {} // eslint-disable-line @@ -30,7 +30,7 @@ export default class Command { this.aliases = []; this.guildOnly = true; this.client = client; - this.subcommands = []; + this.subcommands = new Map(); this.permissions = {}; } } diff --git a/src/class/Util.ts b/src/class/Util.ts index b02284a..f21a081 100644 --- a/src/class/Util.ts +++ b/src/class/Util.ts @@ -36,15 +36,47 @@ 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; + public resolveCommand(command: string, args: string[]): { command: Command, args: string[]} { + let Cmd: Command; + if (this.client.commands.has(command)) Cmd = this.client.commands.get(command); + else { + 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.toLowerCase() === alias.toLowerCase()) { + Cmd = cmd; + } else { + return undefined; + } + } } } - return undefined; + if (!Cmd) return undefined; + let hasSubcmd = true; + while (hasSubcmd) { + if (!Cmd.subcommands.size) { + hasSubcmd = false; break; + } + if (Cmd.subcommands.has(args[0])) { + Cmd = Cmd.subcommands.get(args[0]); + args.shift(); + } else { + for (const cmd of Cmd.subcommands.values()) { + if (!cmd.aliases) continue; // eslint-disable-line + for (const alias of cmd.aliases) { + if (args[0].toLowerCase() === alias.toLowerCase()) { + Cmd = cmd; args.shift(); break; + } + } + break; + } + } + } + return { command: Cmd, args }; + } + + public resolveSubcommand(parentCmd: Command, args?: string[]) { + } public async handleError(error: Error, message?: Message, command?: Command): Promise { diff --git a/src/events/messageCreate.ts b/src/events/messageCreate.ts index a263db7..fb81835 100644 --- a/src/events/messageCreate.ts +++ b/src/events/messageCreate.ts @@ -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); + const Args = noPrefix.slice(1); + const resolved: { command: Command, args: string[] } = this.client.util.resolveCommand(command, Args); if (!resolved) return; - if (resolved.guildOnly && !(message.channel instanceof TextChannel)) return; + if (resolved.command.guildOnly && !(message.channel instanceof TextChannel)) return; let hasUserPerms: boolean; - if (resolved.permissions.users) { - hasUserPerms = resolved.permissions.users.includes(message.author.id); + if (resolved.command.permissions.users) { + hasUserPerms = resolved.command.permissions.users.includes(message.author.id); } let hasRolePerms: boolean = false; - if (resolved.permissions.roles) { - for (const role of resolved.permissions.roles) { + if (resolved.command.permissions.roles) { + for (const role of resolved.command.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.command.permissions.users && !resolved.command.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.command.enabled) { message.channel.createMessage(`***${this.client.stores.emojis.error} This command has been disabled***`); return; } + resolved.command.run(message, resolved.args); } catch (error) { this.client.util.handleError(error, message); }