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",
"description": "Official Simplistic Commands API for Squeebot 3",
"tags": ["handler", "commands", "api"],
"version": "1.0.0",
"version": "1.1.0",
"dependencies": ["control?", "permissions?"],
"npmDependencies": []
}

View File

@ -6,7 +6,7 @@ import {
DependencyUnload
} 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';
@ -28,7 +28,12 @@ interface CommandSpec {
hidden?: boolean;
permissions?: string[];
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 {
@ -156,7 +161,11 @@ class SqueebotCommandsAPIPlugin extends Plugin {
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 separate = text.split(' ');
if (separate[0].indexOf(prefix) === 0) {
@ -220,7 +229,7 @@ class SqueebotCommandsAPIPlugin extends Plugin {
argv.unshift(plugins);
}
const success = await spec.execute(msg, spec, prefix, ...argv);
const success = await spec.execute(msg, msr, spec, prefix, ...argv);
if (success) {
break;
}
@ -229,7 +238,11 @@ class SqueebotCommandsAPIPlugin extends Plugin {
// 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();
// Only pass command specs which have `match` and match rooms
@ -292,7 +305,7 @@ class SqueebotCommandsAPIPlugin extends Plugin {
// Start executing
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 : []));
if (success) {
break;
@ -303,7 +316,7 @@ class SqueebotCommandsAPIPlugin extends Plugin {
}
@EventListener('message')
public digest(msg: IMessage, chan: IChannel): void {
public digest(msg: IMessage, chan: IChannel, msr: MessageResolver): void {
if (msg.type !== EMessageType.message) {
return;
}
@ -321,7 +334,7 @@ class SqueebotCommandsAPIPlugin extends Plugin {
if (keywords && keywords.length) {
for (const kw of keywords) {
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));
return;
}
@ -355,7 +368,7 @@ class SqueebotCommandsAPIPlugin extends Plugin {
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));
}
@ -557,7 +570,12 @@ class SqueebotCommandsAPIPlugin extends Plugin {
aliases: ['commands'],
usage: '[<command>]',
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);
return true;
}
@ -569,7 +587,12 @@ class SqueebotCommandsAPIPlugin extends Plugin {
aliases: ['alias'],
usage: '<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);
return true;
}

View File

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