Add command line client
This commit is contained in:
parent
0808584aba
commit
677a1a958a
47
package.json
47
package.json
|
@ -1,25 +1,26 @@
|
||||||
{
|
{
|
||||||
"name": "fishing-api",
|
"name": "fishing-api",
|
||||||
"module": "src/api/index.ts",
|
"module": "src/api/index.ts",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "bun .",
|
"start": "bun .",
|
||||||
"start-bot": "bun src/mpp/index.ts"
|
"start-bot": "bun src/mpp/index.ts"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/bun": "latest"
|
"@types/bun": "latest"
|
||||||
},
|
},
|
||||||
"peerDependencies": {
|
"peerDependencies": {
|
||||||
"typescript": "^5.0.0"
|
"typescript": "^5.0.0"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@prisma/client": "^5.9.1",
|
"@prisma/client": "^5.9.1",
|
||||||
"@trpc/client": "next",
|
"@trpc/client": "next",
|
||||||
"@trpc/server": "next",
|
"@trpc/server": "next",
|
||||||
"mpp-client-net": "^1.1.3",
|
"cli-markdown": "^3.2.2",
|
||||||
"prisma": "^5.9.1",
|
"mpp-client-net": "^1.1.3",
|
||||||
"trpc-bun-adapter": "^1.1.0",
|
"prisma": "^5.9.1",
|
||||||
"yaml": "^2.3.4",
|
"trpc-bun-adapter": "^1.1.0",
|
||||||
"zod": "^3.22.4"
|
"yaml": "^2.3.4",
|
||||||
}
|
"zod": "^3.22.4"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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));
|
||||||
|
}
|
||||||
|
});
|
|
@ -1,6 +1,6 @@
|
||||||
import Client from "mpp-client-net";
|
import Client from "mpp-client-net";
|
||||||
import { Logger } from "@util/Logger";
|
import { Logger } from "@util/Logger";
|
||||||
import trpc from "@client/api/trpc";
|
import trpc from "@util/api/trpc";
|
||||||
import { EventEmitter } from "events";
|
import { EventEmitter } from "events";
|
||||||
|
|
||||||
export interface MPPNetBotConfig {
|
export interface MPPNetBotConfig {
|
||||||
|
|
Loading…
Reference in New Issue