tokens but no worky

This commit is contained in:
Hri7566 2024-02-15 22:02:39 -05:00
parent f8d1af1b0a
commit 63472ca3f6
18 changed files with 203 additions and 24 deletions

View File

@ -14,6 +14,7 @@ model User {
id String @id
name String
color String
inventory Inventory @relation(fields: [inventoryId], references: [id])
inventoryId Int @unique
}
@ -21,7 +22,11 @@ model User {
model Inventory {
id Int @id @default(autoincrement())
balance Int
items Json @default("[]")
fishSack Json @default("[]")
pokemon Json @default("[]")
User User?
}

View File

@ -1,22 +1,31 @@
import { handleCommand } from "@server/commands/handler";
import { prefixes } from "@server/commands/prefixes";
import { checkToken } from "@server/data/token";
import { TRPCError, initTRPC } from "@trpc/server";
import { Logger } from "@util/Logger";
import type { CreateBunContextOptions } from "trpc-bun-adapter";
import { z } from "zod";
const logger = new Logger("tRPC");
export interface Context {
isAuthed: boolean;
}
const t = initTRPC.context<Context>().create();
export const createContext = async (opts: CreateBunContextOptions) => {
return {
isAuthed: false
} as Context;
};
const t = initTRPC.context<typeof createContext>().create();
export const router = t.router;
export const publicProcedure = t.procedure;
export const privateProcedure = publicProcedure.use(async opts => {
const { ctx } = opts;
if (!ctx.isAuthed) throw new TRPCError({ code: "UNAUTHORIZED" });
return opts.next({
ctx: {
isAuthed: true
@ -24,14 +33,18 @@ export const privateProcedure = publicProcedure.use(async opts => {
});
});
const logger = new Logger("tRPC");
export const appRouter = router({
prefixes: publicProcedure.query(async opts => {
return prefixes;
}),
command: publicProcedure
auth: publicProcedure.input(z.string()).query(async opts => {
logger.debug(opts);
const token = opts.input;
opts.ctx.isAuthed = await checkToken(token);
}),
command: privateProcedure
.input(
z.object({
command: z.string(),
@ -49,10 +62,6 @@ export const appRouter = router({
const out = await handleCommand(command, args, prefix, user);
return out;
}),
auth: publicProcedure.input(z.string()).query(async opts => {
const token = opts.input;
})
});

View File

@ -0,0 +1,11 @@
export type ReadlineCommandCallback = (line: string) => Promise<string | void>;
export class ReadlineCommand {
constructor(
public id: string,
public aliases: string[],
public description: string,
public usage: string,
public callback: ReadlineCommandCallback
) {}
}

14
src/api/cli/commands.ts Normal file
View File

@ -0,0 +1,14 @@
import type { ReadlineCommand } from "./ReadlineCommand";
import { deltoken } from "./commands/deltoken";
import { gentoken } from "./commands/gentoken";
import { help } from "./commands/help";
import { lstoken } from "./commands/lstoken";
import { stop } from "./commands/stop";
export const readlineCommands: ReadlineCommand[] = [];
readlineCommands.push(help);
readlineCommands.push(gentoken);
readlineCommands.push(deltoken);
readlineCommands.push(lstoken);
readlineCommands.push(stop);

View File

@ -0,0 +1,18 @@
import { deleteToken } from "@server/data/token";
import { ReadlineCommand } from "../ReadlineCommand";
export const deltoken = new ReadlineCommand(
"deltoken",
["deltoken"],
"Delete an API token",
"deltoken <token>",
async line => {
const args = line.split(" ");
if (!args[1]) return "Please provide a token";
const argcat = args.slice(1).join(" ");
await deleteToken(argcat);
return "Token deleted";
}
);

View File

@ -0,0 +1,13 @@
import { createToken } from "@server/data/token";
import { ReadlineCommand } from "../ReadlineCommand";
export const gentoken = new ReadlineCommand(
"gentoken",
["gentoken"],
"Generate a new API token",
"gentoken",
async line => {
const token = await createToken();
return `New token: ${token}`;
}
);

View File

@ -0,0 +1,23 @@
import { ReadlineCommand } from "../ReadlineCommand";
import { readlineCommands } from "../commands";
export const help = new ReadlineCommand(
"help",
["help", "cmds"],
"List commands",
"help [command]",
async line => {
const args = line.split(" ");
if (!args[1]) {
return `Commands: ${readlineCommands
.map(cmd => cmd.aliases[0])
.join(" | ")}`;
} else {
const foundCommand: ReadlineCommand | undefined =
readlineCommands.find(cmd => cmd.aliases.includes(args[1]));
if (!foundCommand) return `No such command "${args[1]}"`;
return `Description: ${foundCommand.description} | Usage: ${foundCommand.usage}`;
}
}
);

View File

@ -0,0 +1,17 @@
import { getAllTokens } from "@server/data/token";
import { ReadlineCommand } from "../ReadlineCommand";
export const lstoken = new ReadlineCommand(
"lstoken",
["lstoken"],
"List all API tokens",
"lstoken",
async line => {
const tokens = await getAllTokens();
if (tokens.length > 0) {
return `Tokens:\n\t- ${tokens.map(t => t.token).join("\n\t- ")}`;
} else {
return "No tokens";
}
}
);

View File

@ -0,0 +1,11 @@
import { ReadlineCommand } from "../ReadlineCommand";
export const stop = new ReadlineCommand(
"stop",
["stop", "exit"],
"Stop server",
"stop",
async line => {
process.exit();
}
);

18
src/api/cli/handler.ts Normal file
View File

@ -0,0 +1,18 @@
import type { ReadlineCommand } from "./ReadlineCommand";
import { readlineCommands } from "./commands";
import { logger } from "./readline";
export async function handleReadlineCommand(line: string) {
let args = line.split(" ");
let usedCommand: ReadlineCommand | undefined = readlineCommands.find(cmd =>
cmd.aliases.includes(args[0])
);
if (!usedCommand) return "Unknown command";
try {
return await usedCommand.callback(line);
} catch (err) {
logger.error(err);
}
}

19
src/api/cli/readline.ts Normal file
View File

@ -0,0 +1,19 @@
import { createInterface } from "readline";
import { handleReadlineCommand } from "./handler";
import { Logger } from "@util/Logger";
export const rl = createInterface({
input: process.stdin,
output: process.stdout
});
export const logger = new Logger("Readline");
rl.on("line", async data => {
const out = await handleReadlineCommand(data);
if (typeof out !== "undefined") logger.info(out);
});
rl.prompt();
(globalThis as unknown as any).rl = rl;

View File

@ -5,7 +5,8 @@ export class Command {
public description: string,
public usage: string,
public permissionNode: string,
public callback: TCommandCallback
public callback: TCommandCallback,
public visible: boolean = true
) {}
}

View File

@ -23,11 +23,12 @@ export const help = new Command(
"command.general.help",
async (command, args, prefix, user) => {
return `${commandGroups
.map(group => {
return `${group.displayName}: ${group.commands
.map(cmd => cmd.aliases[0])
.join(", ")}`;
})
.map(
group =>
`${group.displayName}: ${group.commands
.map(cmd => (cmd.visible ? cmd.aliases[0] : "<hidden>"))
.join(", ")}`
)
.join("\n")}`;
}
);

View File

@ -6,5 +6,7 @@ export const data = new Command(
"Data command",
"data",
"command.util.data",
async () => {}
async (command, args, prefix, user) => {
return JSON.stringify({ command, args, prefix, user });
}
);

View File

@ -25,3 +25,7 @@ export async function checkToken(token: string) {
return !!existing;
}
export async function getAllTokens() {
return await prisma.authToken.findMany();
}

View File

@ -1 +1,2 @@
import "./api/server";
import "./cli/readline";

View File

@ -39,6 +39,7 @@ export class MPPNetBot {
public bindEventListeners() {
this.client.on("hi", msg => {
this.logger.info(`Connected to ${this.client.uri}`);
trpc.auth.query(process.env.FISHING_TOKEN as string);
});
this.client.on("ch", msg => {

View File

@ -1,10 +1,21 @@
import { getHHMMSS } from "./time";
import type { ReadLine } from "readline";
export class Logger {
private static log(...args: any[]) {
const time = getHHMMSS();
if (typeof (globalThis as unknown as any).rl !== "undefined") {
process.stdout.write("\x1b[2K\r");
}
console.log(`\x1b[30m${time}\x1b[0m`, ...args);
if (typeof (globalThis as unknown as any).rl !== "undefined") {
try {
((globalThis as unknown as any).rl as ReadLine).prompt();
} catch (err) {}
}
}
constructor(public id: string) {}