From 30fd1555b96ce0ec00fccf7c2c36110dc40894cd Mon Sep 17 00:00:00 2001 From: gnuxie Date: Fri, 9 Dec 2022 13:50:44 +0000 Subject: [PATCH] MatrixInterfaceCommand more abstract. --- src/commands/MatrixInterfaceCommand.ts | 28 ++++++++++++++------------ 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/src/commands/MatrixInterfaceCommand.ts b/src/commands/MatrixInterfaceCommand.ts index 6414b98c..4562b9ae 100644 --- a/src/commands/MatrixInterfaceCommand.ts +++ b/src/commands/MatrixInterfaceCommand.ts @@ -25,11 +25,10 @@ limitations under the License. * are NOT distributed, contributed, committed, or licensed under the Apache License. */ -import { Mjolnir } from "../Mjolnir"; -import { ApplicationCommand, ApplicationFeature, getApplicationFeature } from "./ApplicationCommand"; -import { ValidationError, ValidationResult } from "./Validation"; -import { RichReply, LogService } from "matrix-bot-sdk"; -import { ReadItem } from "./CommandReader"; +import { ApplicationCommand, ApplicationFeature, getApplicationFeature } from "./interface-manager/ApplicationCommand"; +import { ValidationError, ValidationResult } from "./interface-manager/Validation"; +import { RichReply, LogService, MatrixClient } from "matrix-bot-sdk"; +import { ReadItem } from "./interface-manager/CommandReader"; type CommandLookupEntry = Map>; @@ -39,13 +38,16 @@ const THIS_COMMAND_SYMBOL = Symbol("thisCommand"); type ParserSignature Promise> = ( this: MatrixInterfaceCommand, - mjolnir: Mjolnir, - roomId: string, - event: any, - parts: ReadItem[]) => Promise>>; + // The idea then is that this can be extended to include a Mjolnir or whatever. + options: { + client: MatrixClient, + roomId: string, + event: any, + }, + ...parts: ReadItem[]) => Promise>>; type RendererSignature> = ( - mjolnir: Mjolnir, + client: MatrixClient, commandRoomId: string, event: any, result: Awaited) => Promise; @@ -71,7 +73,7 @@ class MatrixInterfaceCommand Promise private readonly parser: ParserSignature, public readonly applicationCommand: ApplicationCommand, private readonly renderer: RendererSignature>, - private readonly validationErrorHandler?: (mjolnir: Mjolnir, roomId: string, event: any, validationError: ValidationError) => Promise + private readonly validationErrorHandler?: (client: MatrixClient, roomId: string, event: any, validationError: ValidationError) => Promise ) { } @@ -94,7 +96,7 @@ class MatrixInterfaceCommand Promise await this.renderer.apply(this, [...args.slice(0, -1), executorResult]); } - private async reportValidationError(mjolnir: Mjolnir, roomId: string, event: any, validationError: ValidationError): Promise { + private async reportValidationError(client: MatrixClient, roomId: string, event: any, validationError: ValidationError): Promise { LogService.info("MatrixInterfaceCommand", `User input validation error when parsing command ${this.commandParts}: ${validationError.message}`); if (this.validationErrorHandler) { await this.validationErrorHandler.apply(this, arguments); @@ -103,7 +105,7 @@ class MatrixInterfaceCommand Promise const replyMessage = validationError.message; const reply = RichReply.createFor(roomId, event, replyMessage, replyMessage); reply["msgtype"] = "m.notice"; - await mjolnir.client.sendMessage(roomId, reply); + await client.sendMessage(roomId, reply); } }