Add command line client

This commit is contained in:
Hri7566 2024-02-26 07:20:22 -05:00
parent 0808584aba
commit 677a1a958a
5 changed files with 92 additions and 24 deletions

BIN
bun.lockb

Binary file not shown.

View File

@ -1,25 +1,26 @@
{
"name": "fishing-api",
"module": "src/api/index.ts",
"type": "module",
"scripts": {
"start": "bun .",
"start-bot": "bun src/mpp/index.ts"
},
"devDependencies": {
"@types/bun": "latest"
},
"peerDependencies": {
"typescript": "^5.0.0"
},
"dependencies": {
"@prisma/client": "^5.9.1",
"@trpc/client": "next",
"@trpc/server": "next",
"mpp-client-net": "^1.1.3",
"prisma": "^5.9.1",
"trpc-bun-adapter": "^1.1.0",
"yaml": "^2.3.4",
"zod": "^3.22.4"
}
"name": "fishing-api",
"module": "src/api/index.ts",
"type": "module",
"scripts": {
"start": "bun .",
"start-bot": "bun src/mpp/index.ts"
},
"devDependencies": {
"@types/bun": "latest"
},
"peerDependencies": {
"typescript": "^5.0.0"
},
"dependencies": {
"@prisma/client": "^5.9.1",
"@trpc/client": "next",
"@trpc/server": "next",
"cli-markdown": "^3.2.2",
"mpp-client-net": "^1.1.3",
"prisma": "^5.9.1",
"trpc-bun-adapter": "^1.1.0",
"yaml": "^2.3.4",
"zod": "^3.22.4"
}
}

67
src/cli/index.ts Normal file
View File

@ -0,0 +1,67 @@
import { Logger } from "@util/Logger";
import { createInterface, type ReadLine } from "readline";
import { EventEmitter } from "events";
import trpc from "@util/api/trpc";
// no typedefs :/
const cliMd = require("cli-markdown").default;
const logger = new Logger("CLI");
const b = new EventEmitter();
const rl = createInterface({
input: process.stdin,
output: process.stdout
});
(globalThis as unknown as any).rl = rl;
rl.setPrompt("> ");
rl.prompt();
rl.on("line", async line => {
if (line == "stop" || line == "exit") process.exit();
rl.prompt();
const msg = {
a: line,
p: {
_id: "stdin",
name: "CLI",
color: "#abe3d6"
}
};
let prefixes: string[];
try {
prefixes = await trpc.prefixes.query();
} catch (err) {
logger.error(err);
logger.warn("Unable to contact server");
return;
}
let usedPrefix: string | undefined = prefixes.find(pr =>
msg.a.startsWith(pr)
);
if (!usedPrefix) return;
const args = msg.a.split(" ");
const command = await trpc.command.query({
args: args.slice(1, args.length),
command: args[0].substring(usedPrefix.length),
prefix: usedPrefix,
user: {
id: msg.p._id,
name: msg.p.name,
color: msg.p.color
}
});
if (!command) return;
if (command.response) {
logger.info(cliMd(command.response));
}
});

View File

@ -1,6 +1,6 @@
import Client from "mpp-client-net";
import { Logger } from "@util/Logger";
import trpc from "@client/api/trpc";
import trpc from "@util/api/trpc";
import { EventEmitter } from "events";
export interface MPPNetBotConfig {