Implement console service & microcommand handler for utility

This commit is contained in:
Hri7566 2023-11-08 03:17:42 -05:00
parent 58b57e6413
commit cb4c84f932
6 changed files with 86 additions and 22 deletions

View File

@ -1,4 +1,4 @@
enableMPP: false enableMPP: false
enableDiscord: false enableDiscord: false
enableConsole: true enableConsole: true
enableSwitchChat: true enableSwitchChat: false

View File

@ -16,6 +16,7 @@ model User {
platformId String platformId String
platform String platform String
Inventory Inventory[] Inventory Inventory[]
role Role
} }
model Inventory { model Inventory {
@ -24,3 +25,10 @@ model Inventory {
user User @relation(fields: [userId], references: [id]) user User @relation(fields: [userId], references: [id])
items Json items Json
} }
enum Role {
NONE
MODERATOR
ADMINISTRATOR
OWNER
}

View File

@ -3,3 +3,7 @@ import { ServiceLoader } from "./services";
loadCommands(); loadCommands();
ServiceLoader.loadServices(); ServiceLoader.loadServices();
export function scopedEval(code: string) {
return eval(code);
}

View File

@ -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;
}
}
}

View File

@ -1,25 +1,32 @@
import { CommandHandler, CommandMessage } from "../../commands/CommandHandler"; import {
BaseCommandMessage,
CommandHandler
} from "../../commands/CommandHandler";
import { loadConfig } from "../../util/config"; import { loadConfig } from "../../util/config";
import { ServiceAgent } from "../ServiceAgent"; import { ServiceAgent } from "../ServiceAgent";
import readline from "readline"; import readline from "readline";
import { MicroHandler } from "./MicroHandler";
import { Logger } from "../../util/Logger";
const config = loadConfig("config/switchchat.yml", { const config = loadConfig("config/switchchat.yml", {
ownerOnly: false ownerOnly: false
}); });
export class SwitchChatAgent extends ServiceAgent<readline.ReadLine> { export class ConsoleAgent extends ServiceAgent<readline.ReadLine> {
public desiredUser = { public desiredUser = {
name: "🟇 𝙎𝙪𝙥𝙚𝙧 Cosmic", name: "🟇 𝙎𝙪𝙥𝙚𝙧 Cosmic",
color: "#1d0054" color: "#1d0054"
}; };
constructor(token: string) { public logger = new Logger("Console");
constructor() {
const cl = readline.createInterface({ const cl = readline.createInterface({
input: process.stdin, input: process.stdin,
output: process.stdout output: process.stdout
}); });
super(cl); super("console", cl);
} }
public started = false; public started = false;
@ -40,26 +47,33 @@ export class SwitchChatAgent extends ServiceAgent<readline.ReadLine> {
protected bindEventListeners(): void { protected bindEventListeners(): void {
super.bindEventListeners(); super.bindEventListeners();
this.client.on("command", async cmd => { this.client.on("line", async text => {
if (config.ownerOnly && !cmd.ownerOnly) return; const args = text.split(" ");
console.log( const message: BaseCommandMessage = {
`${cmd.user.displayName}: ${cmd.ownerOnly ? "^" : "\\"}${
cmd.command
} ${cmd.args.join(" ")}`
);
const message: CommandMessage = {
m: "command", m: "command",
a: `${cmd.command} ${cmd.args.join(" ")}`, a: text,
argc: cmd.args.length + 1, argc: args.length,
argv: [cmd.command, ...cmd.args], argv: args,
originalMessage: cmd originalMessage: text,
p: {
platformId: "console",
_id: "console",
name: "Console",
color: "#ffffff"
}
}; };
const out = await CommandHandler.handleCommand(message, this); let out;
console.log(out);
if (out) this.client.tell(cmd.user.name, 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();
}); });
} }
} }

View File

@ -5,6 +5,7 @@ import { ServiceAgent } from "./ServiceAgent";
import { loadConfig } from "../util/config"; import { loadConfig } from "../util/config";
import { z } from "zod"; import { z } from "zod";
import { SwitchChatAgent } from "./switchchat"; import { SwitchChatAgent } from "./switchchat";
import { ConsoleAgent } from "./console";
/** /**
* Services are anything (any platforms or environments) that the bot will directly communicate to users with * 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(); switchChatAgent.start();
this.addAgent(switchChatAgent); this.addAgent(switchChatAgent);
} }
if (config.enableConsole) {
const consoleAgent = new ConsoleAgent();
consoleAgent.start();
this.addAgent(consoleAgent);
}
} }
} }