add resolver

This commit is contained in:
Evert Prants 2020-12-13 12:17:04 +02:00
parent d83b7a5b02
commit 0e5e15170c
Signed by: evert
GPG Key ID: 1688DA83D222D0B5
3 changed files with 36 additions and 13 deletions

View File

@ -3,7 +3,7 @@
"name": "simplecommands", "name": "simplecommands",
"description": "Official Simplistic Commands API for Squeebot 3", "description": "Official Simplistic Commands API for Squeebot 3",
"tags": ["handler", "commands", "api"], "tags": ["handler", "commands", "api"],
"version": "1.0.0", "version": "1.1.0",
"dependencies": ["control?", "permissions?"], "dependencies": ["control?", "permissions?"],
"npmDependencies": [] "npmDependencies": []
} }

View File

@ -6,7 +6,7 @@ import {
DependencyUnload DependencyUnload
} from '@squeebot/core/lib/plugin'; } from '@squeebot/core/lib/plugin';
import { EMessageType, IMessage } from '@squeebot/core/lib/types'; import { EMessageType, IMessage, MessageResolver } from '@squeebot/core/lib/types';
import { fullIDMatcher } from '@squeebot/core/lib/common'; import { fullIDMatcher } from '@squeebot/core/lib/common';
@ -28,7 +28,12 @@ interface CommandSpec {
hidden?: boolean; hidden?: boolean;
permissions?: string[]; permissions?: string[];
tempargv?: any[]; tempargv?: any[];
execute(msg: IMessage, command: CommandSpec, prefix: string, ...args: any[]): Promise<boolean>; execute(
msg: IMessage,
msr: MessageResolver,
command: CommandSpec,
prefix: string,
...args: any[]): Promise<boolean>;
} }
interface RateLimit { interface RateLimit {
@ -156,7 +161,11 @@ class SqueebotCommandsAPIPlugin extends Plugin {
return permitted; return permitted;
} }
public async handlePrefix(msg: IMessage, prefix: string, plugins: string[]): Promise<void> { public async handlePrefix(
msg: IMessage,
prefix: string,
plugins: string[],
msr: MessageResolver): Promise<void> {
const text = msg.text; const text = msg.text;
const separate = text.split(' '); const separate = text.split(' ');
if (separate[0].indexOf(prefix) === 0) { if (separate[0].indexOf(prefix) === 0) {
@ -220,7 +229,7 @@ class SqueebotCommandsAPIPlugin extends Plugin {
argv.unshift(plugins); argv.unshift(plugins);
} }
const success = await spec.execute(msg, spec, prefix, ...argv); const success = await spec.execute(msg, msr, spec, prefix, ...argv);
if (success) { if (success) {
break; break;
} }
@ -229,7 +238,11 @@ class SqueebotCommandsAPIPlugin extends Plugin {
// Done // Done
} }
public async handleKeywords(msg: IMessage, keyword: string, plugins: string[]): Promise<void> { public async handleKeywords(
msg: IMessage,
keyword: string,
plugins: string[],
msr: MessageResolver): Promise<void> {
const text = msg.text.toLowerCase(); const text = msg.text.toLowerCase();
// Only pass command specs which have `match` and match rooms // Only pass command specs which have `match` and match rooms
@ -292,7 +305,7 @@ class SqueebotCommandsAPIPlugin extends Plugin {
// Start executing // Start executing
for (const spec of permitted) { for (const spec of permitted) {
const success = await spec.execute(msg, spec, keyword, const success = await spec.execute(msg, msr, spec, keyword,
...(spec.tempargv ? spec.tempargv : [])); ...(spec.tempargv ? spec.tempargv : []));
if (success) { if (success) {
break; break;
@ -303,7 +316,7 @@ class SqueebotCommandsAPIPlugin extends Plugin {
} }
@EventListener('message') @EventListener('message')
public digest(msg: IMessage, chan: IChannel): void { public digest(msg: IMessage, chan: IChannel, msr: MessageResolver): void {
if (msg.type !== EMessageType.message) { if (msg.type !== EMessageType.message) {
return; return;
} }
@ -321,7 +334,7 @@ class SqueebotCommandsAPIPlugin extends Plugin {
if (keywords && keywords.length) { if (keywords && keywords.length) {
for (const kw of keywords) { for (const kw of keywords) {
if (text.toLowerCase().match(kw) != null) { if (text.toLowerCase().match(kw) != null) {
this.handleKeywords(msg, kw, allowedPlugins).catch(e => this.handleKeywords(msg, kw, allowedPlugins, msr).catch(e =>
logger.error('[%s] Command handler threw an error:', this.name, e.stack)); logger.error('[%s] Command handler threw an error:', this.name, e.stack));
return; return;
} }
@ -355,7 +368,7 @@ class SqueebotCommandsAPIPlugin extends Plugin {
return; return;
} }
this.handlePrefix(msg, prefix, allowedPlugins).catch(e => this.handlePrefix(msg, prefix, allowedPlugins, msr).catch(e =>
logger.error('[%s] Command handler threw an error:', this.name, e.stack)); logger.error('[%s] Command handler threw an error:', this.name, e.stack));
} }
@ -557,7 +570,12 @@ class SqueebotCommandsAPIPlugin extends Plugin {
aliases: ['commands'], aliases: ['commands'],
usage: '[<command>]', usage: '[<command>]',
description: 'Show command usage or list all available commands', description: 'Show command usage or list all available commands',
execute: async (msg: IMessage, spec: CommandSpec, prefix: string, ...args: any[]): Promise<boolean> => { execute: async (
msg: IMessage,
msr: MessageResolver,
spec: CommandSpec,
prefix: string,
...args: any[]): Promise<boolean> => {
this.helpCommand(msg, prefix, args); this.helpCommand(msg, prefix, args);
return true; return true;
} }
@ -569,7 +587,12 @@ class SqueebotCommandsAPIPlugin extends Plugin {
aliases: ['alias'], aliases: ['alias'],
usage: '<command>', usage: '<command>',
description: 'Show the list of aliases for command', description: 'Show the list of aliases for command',
execute: async (msg: IMessage, spec: CommandSpec, prefix: string, ...args: any[]): Promise<boolean> => { execute: async (
msg: IMessage,
msr: MessageResolver,
spec: CommandSpec,
prefix: string,
...args: any[]): Promise<boolean> => {
this.aliasesCommand(msg, prefix, args); this.aliasesCommand(msg, prefix, args);
return true; return true;
} }

View File

@ -11,7 +11,7 @@
}, },
{ {
"name": "simplecommands", "name": "simplecommands",
"version": "1.0.0" "version": "1.1.0"
} }
], ],
"typescript": true "typescript": true