From cb4c84f93209050d66749b7131ca72030e36a5e0 Mon Sep 17 00:00:00 2001 From: Hri7566 Date: Wed, 8 Nov 2023 03:17:42 -0500 Subject: [PATCH] Implement console service & microcommand handler for utility --- config/services.yml | 2 +- prisma/schema.prisma | 8 ++++ src/index.ts | 4 ++ src/services/console/MicroHandler.ts | 30 +++++++++++++++ src/services/console/index.ts | 56 +++++++++++++++++----------- src/services/index.ts | 8 ++++ 6 files changed, 86 insertions(+), 22 deletions(-) create mode 100644 src/services/console/MicroHandler.ts diff --git a/config/services.yml b/config/services.yml index 42ce890..9bdc4c2 100644 --- a/config/services.yml +++ b/config/services.yml @@ -1,4 +1,4 @@ enableMPP: false enableDiscord: false enableConsole: true -enableSwitchChat: true +enableSwitchChat: false diff --git a/prisma/schema.prisma b/prisma/schema.prisma index da69ed6..664278c 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -16,6 +16,7 @@ model User { platformId String platform String Inventory Inventory[] + role Role } model Inventory { @@ -24,3 +25,10 @@ model Inventory { user User @relation(fields: [userId], references: [id]) items Json } + +enum Role { + NONE + MODERATOR + ADMINISTRATOR + OWNER +} diff --git a/src/index.ts b/src/index.ts index 0a8dcc3..aeb6ae7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,3 +3,7 @@ import { ServiceLoader } from "./services"; loadCommands(); ServiceLoader.loadServices(); + +export function scopedEval(code: string) { + return eval(code); +} diff --git a/src/services/console/MicroHandler.ts b/src/services/console/MicroHandler.ts new file mode 100644 index 0000000..e9f393a --- /dev/null +++ b/src/services/console/MicroHandler.ts @@ -0,0 +1,30 @@ +import { scopedEval } from "../.."; +import { BaseCommandMessage } from "../../commands/CommandHandler"; + +export class MicroHandler { + public static async handleMicroCommand(command: BaseCommandMessage) { + let microcommand = command.argv[0].substring(1); + + switch (microcommand) { + case "help": + case "commands": + case "cmds": + default: + return "Microcommands: /help | /js | /exit"; + break; + case "js": + case "eval": + try { + const out = scopedEval(command.argv.slice(1).join(" ")); + return `(${typeof out}) ${out}`; + } catch (err) { + return err; + } + break; + case "exit": + case "stop": + process.exit(); + break; + } + } +} diff --git a/src/services/console/index.ts b/src/services/console/index.ts index 16b405c..ca777f7 100644 --- a/src/services/console/index.ts +++ b/src/services/console/index.ts @@ -1,25 +1,32 @@ -import { CommandHandler, CommandMessage } from "../../commands/CommandHandler"; +import { + BaseCommandMessage, + CommandHandler +} from "../../commands/CommandHandler"; import { loadConfig } from "../../util/config"; import { ServiceAgent } from "../ServiceAgent"; import readline from "readline"; +import { MicroHandler } from "./MicroHandler"; +import { Logger } from "../../util/Logger"; const config = loadConfig("config/switchchat.yml", { ownerOnly: false }); -export class SwitchChatAgent extends ServiceAgent { +export class ConsoleAgent extends ServiceAgent { public desiredUser = { name: "🟇 𝙎𝙪𝙥𝙚𝙧 Cosmic", color: "#1d0054" }; - constructor(token: string) { + public logger = new Logger("Console"); + + constructor() { const cl = readline.createInterface({ input: process.stdin, output: process.stdout }); - super(cl); + super("console", cl); } public started = false; @@ -27,7 +34,7 @@ export class SwitchChatAgent extends ServiceAgent { public start() { if (this.started) return; this.started = true; - this.client.setPrompt(">"); + this.client.setPrompt("> "); this.client.prompt(true); } @@ -40,26 +47,33 @@ export class SwitchChatAgent extends ServiceAgent { protected bindEventListeners(): void { super.bindEventListeners(); - this.client.on("command", async cmd => { - if (config.ownerOnly && !cmd.ownerOnly) return; + this.client.on("line", async text => { + const args = text.split(" "); - console.log( - `${cmd.user.displayName}: ${cmd.ownerOnly ? "^" : "\\"}${ - cmd.command - } ${cmd.args.join(" ")}` - ); - - const message: CommandMessage = { + const message: BaseCommandMessage = { m: "command", - a: `${cmd.command} ${cmd.args.join(" ")}`, - argc: cmd.args.length + 1, - argv: [cmd.command, ...cmd.args], - originalMessage: cmd + a: text, + argc: args.length, + argv: args, + originalMessage: text, + p: { + platformId: "console", + _id: "console", + name: "Console", + color: "#ffffff" + } }; - const out = await CommandHandler.handleCommand(message, this); - console.log(out); - if (out) this.client.tell(cmd.user.name, out); + let out; + + if (text.startsWith("/")) { + out = await MicroHandler.handleMicroCommand(message); + } else { + out = await CommandHandler.handleCommand(message, this); + } + + if (out) this.logger.info(out); + this.client.prompt(); }); } } diff --git a/src/services/index.ts b/src/services/index.ts index 9efcc49..34ed145 100644 --- a/src/services/index.ts +++ b/src/services/index.ts @@ -5,6 +5,7 @@ import { ServiceAgent } from "./ServiceAgent"; import { loadConfig } from "../util/config"; import { z } from "zod"; import { SwitchChatAgent } from "./switchchat"; +import { ConsoleAgent } from "./console"; /** * Services are anything (any platforms or environments) that the bot will directly communicate to users with @@ -79,5 +80,12 @@ export class ServiceLoader { switchChatAgent.start(); this.addAgent(switchChatAgent); } + + if (config.enableConsole) { + const consoleAgent = new ConsoleAgent(); + + consoleAgent.start(); + this.addAgent(consoleAgent); + } } }