diff --git a/simplecommands/plugin.ts b/simplecommands/plugin.ts index 2602352..d6c28d1 100644 --- a/simplecommands/plugin.ts +++ b/simplecommands/plugin.ts @@ -215,13 +215,12 @@ class SqueebotCommandsAPIPlugin extends Plugin { // Start executing for (const spec of permitted) { - // Help command needs access to plugin list in the channel - // Very dirty - if (spec.name === 'help') { - spec.tempargv = plugins; + const argv: any[] = separate.slice(1); + if (spec.plugin === this.name) { + argv.unshift(plugins); } - const success = await spec.execute(msg, spec, prefix, ...separate.slice(1)); + const success = await spec.execute(msg, spec, prefix, ...argv); if (success) { break; } @@ -416,8 +415,7 @@ class SqueebotCommandsAPIPlugin extends Plugin { } } - private helpCommand(msg: IMessage, prefix: string, plugins: string[]): void { - // Iteration 1: Resolve commands by name and by aliases + private withAliases(msg: IMessage, plugins: string[]): CommandSpec[] { const withAliases = []; for (const spec of this.commands) { if (plugins.length && plugins.indexOf(spec.plugin) === -1) { @@ -435,6 +433,20 @@ class SqueebotCommandsAPIPlugin extends Plugin { withAliases.push(spec); } + return withAliases; + } + + private aliasesCommand(msg: IMessage, prefix: string, args: any[]): void { + // First argument could be a passed plugin array within this plugin + let plugins: string[] = []; + let cmdarg = 0; + if (args && args.length && Array.isArray(args[0])) { + plugins = args[0]; + cmdarg += 1; + } + + // Iteration 1: Resolve commands (with aliases) + const withAliases = this.withAliases(msg, plugins); // Iteration 2: Match rooms for message let matching = this.roomMatcher(msg, withAliases); @@ -442,23 +454,76 @@ class SqueebotCommandsAPIPlugin extends Plugin { // Iteration 3: Match permissions for user matching = this.permissionMatcher(msg, matching); - const text = msg.text; - const argv = text.toLowerCase().split(' '); const b = (t: string) => { return msg.source.format.format('bold', t); }; - if (argv[1]) { + if (!args[cmdarg]) { + msg.resolve('A command name is required.'); + return; + } + + let found: CommandSpec | null = null; + for (const spec of matching) { + if (spec.name === args[cmdarg]) { + found = spec; + break; + } + } + + if (!found) { + msg.resolve('aliases: No such command "%s"!', args[cmdarg]); + return; + } + + if (!found.aliases || found.aliases.length === 0) { + msg.resolve('%s - No aliases found.', b(prefix + found.name)); + return; + } + + let list = found.aliases; + let aliasText = ''; + if (found.alias) { + aliasText = b(`[alias of ${found.alias}]`); + list = list.filter((x) => x !== found?.name); + } + + msg.resolve(b('Aliases of %s:'), prefix + found.name, list.join(', '), aliasText); + } + + private helpCommand(msg: IMessage, prefix: string, args: any[]): void { + // First argument could be a passed plugin array within this plugin + let plugins: string[] = []; + let cmdarg = 0; + if (args && args.length && Array.isArray(args[0])) { + plugins = args[0]; + cmdarg += 1; + } + + // Iteration 1: Resolve commands (with aliases) + const withAliases = this.withAliases(msg, plugins); + + // Iteration 2: Match rooms for message + let matching = this.roomMatcher(msg, withAliases); + + // Iteration 3: Match permissions for user + matching = this.permissionMatcher(msg, matching); + + const b = (t: string) => { + return msg.source.format.format('bold', t); + }; + + if (args[cmdarg]) { let found: CommandSpec | null = null; for (const spec of matching) { - if (spec.name === argv[1]) { + if (spec.name === args[cmdarg]) { found = spec; break; } } if (!found) { - msg.resolve('help: No such command "%s"!', argv[1]); + msg.resolve('help: No such command "%s"!', args[cmdarg]); return; } @@ -491,9 +556,21 @@ class SqueebotCommandsAPIPlugin extends Plugin { name: 'help', aliases: ['commands'], usage: '[]', - description: 'Show command usage or list all commands', - execute: async (msg: IMessage, spec: CommandSpec, prefix: string): Promise => { - this.helpCommand(msg, prefix, spec.tempargv as string[]); + description: 'Show command usage or list all available commands', + execute: async (msg: IMessage, spec: CommandSpec, prefix: string, ...args: any[]): Promise => { + this.helpCommand(msg, prefix, args); + return true; + } + }); + + this.registerCommand({ + plugin: this.name, + name: 'aliases', + aliases: ['alias'], + usage: '', + description: 'Show the list of aliases for command', + execute: async (msg: IMessage, spec: CommandSpec, prefix: string, ...args: any[]): Promise => { + this.aliasesCommand(msg, prefix, args); return true; } });