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
enableDiscord: false
enableConsole: true
enableSwitchChat: true
enableSwitchChat: false

View File

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

View File

@ -3,3 +3,7 @@ import { ServiceLoader } from "./services";
loadCommands();
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 { 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<readline.ReadLine> {
export class ConsoleAgent extends ServiceAgent<readline.ReadLine> {
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<readline.ReadLine> {
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<readline.ReadLine> {
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();
});
}
}

View File

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