tokens but no worky
This commit is contained in:
parent
f8d1af1b0a
commit
63472ca3f6
|
@ -11,18 +11,23 @@ datasource db {
|
||||||
}
|
}
|
||||||
|
|
||||||
model User {
|
model User {
|
||||||
id String @id
|
id String @id
|
||||||
name String
|
name String
|
||||||
color String
|
color String
|
||||||
|
|
||||||
inventory Inventory @relation(fields: [inventoryId], references: [id])
|
inventory Inventory @relation(fields: [inventoryId], references: [id])
|
||||||
inventoryId Int @unique
|
inventoryId Int @unique
|
||||||
}
|
}
|
||||||
|
|
||||||
model Inventory {
|
model Inventory {
|
||||||
id Int @id @default(autoincrement())
|
id Int @id @default(autoincrement())
|
||||||
balance Int
|
balance Int
|
||||||
items Json @default("[]")
|
|
||||||
User User?
|
items Json @default("[]")
|
||||||
|
fishSack Json @default("[]")
|
||||||
|
pokemon Json @default("[]")
|
||||||
|
|
||||||
|
User User?
|
||||||
}
|
}
|
||||||
|
|
||||||
model AuthToken {
|
model AuthToken {
|
||||||
|
|
|
@ -1,22 +1,31 @@
|
||||||
import { handleCommand } from "@server/commands/handler";
|
import { handleCommand } from "@server/commands/handler";
|
||||||
import { prefixes } from "@server/commands/prefixes";
|
import { prefixes } from "@server/commands/prefixes";
|
||||||
|
import { checkToken } from "@server/data/token";
|
||||||
import { TRPCError, initTRPC } from "@trpc/server";
|
import { TRPCError, initTRPC } from "@trpc/server";
|
||||||
import { Logger } from "@util/Logger";
|
import { Logger } from "@util/Logger";
|
||||||
|
import type { CreateBunContextOptions } from "trpc-bun-adapter";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
|
|
||||||
|
const logger = new Logger("tRPC");
|
||||||
|
|
||||||
export interface Context {
|
export interface Context {
|
||||||
isAuthed: boolean;
|
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 router = t.router;
|
||||||
export const publicProcedure = t.procedure;
|
export const publicProcedure = t.procedure;
|
||||||
|
|
||||||
export const privateProcedure = publicProcedure.use(async opts => {
|
export const privateProcedure = publicProcedure.use(async opts => {
|
||||||
const { ctx } = opts;
|
const { ctx } = opts;
|
||||||
|
|
||||||
if (!ctx.isAuthed) throw new TRPCError({ code: "UNAUTHORIZED" });
|
if (!ctx.isAuthed) throw new TRPCError({ code: "UNAUTHORIZED" });
|
||||||
|
|
||||||
return opts.next({
|
return opts.next({
|
||||||
ctx: {
|
ctx: {
|
||||||
isAuthed: true
|
isAuthed: true
|
||||||
|
@ -24,14 +33,18 @@ export const privateProcedure = publicProcedure.use(async opts => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
const logger = new Logger("tRPC");
|
|
||||||
|
|
||||||
export const appRouter = router({
|
export const appRouter = router({
|
||||||
prefixes: publicProcedure.query(async opts => {
|
prefixes: publicProcedure.query(async opts => {
|
||||||
return prefixes;
|
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(
|
.input(
|
||||||
z.object({
|
z.object({
|
||||||
command: z.string(),
|
command: z.string(),
|
||||||
|
@ -49,11 +62,7 @@ export const appRouter = router({
|
||||||
const out = await handleCommand(command, args, prefix, user);
|
const out = await handleCommand(command, args, prefix, user);
|
||||||
|
|
||||||
return out;
|
return out;
|
||||||
}),
|
})
|
||||||
|
|
||||||
auth: publicProcedure.input(z.string()).query(async opts => {
|
|
||||||
const token = opts.input;
|
|
||||||
})
|
|
||||||
});
|
});
|
||||||
|
|
||||||
export type AppRouter = typeof appRouter;
|
export type AppRouter = typeof appRouter;
|
||||||
|
|
|
@ -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
|
||||||
|
) {}
|
||||||
|
}
|
|
@ -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);
|
|
@ -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";
|
||||||
|
}
|
||||||
|
);
|
|
@ -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}`;
|
||||||
|
}
|
||||||
|
);
|
|
@ -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}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
|
@ -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";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
|
@ -0,0 +1,11 @@
|
||||||
|
import { ReadlineCommand } from "../ReadlineCommand";
|
||||||
|
|
||||||
|
export const stop = new ReadlineCommand(
|
||||||
|
"stop",
|
||||||
|
["stop", "exit"],
|
||||||
|
"Stop server",
|
||||||
|
"stop",
|
||||||
|
async line => {
|
||||||
|
process.exit();
|
||||||
|
}
|
||||||
|
);
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -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;
|
|
@ -5,7 +5,8 @@ export class Command {
|
||||||
public description: string,
|
public description: string,
|
||||||
public usage: string,
|
public usage: string,
|
||||||
public permissionNode: string,
|
public permissionNode: string,
|
||||||
public callback: TCommandCallback
|
public callback: TCommandCallback,
|
||||||
|
public visible: boolean = true
|
||||||
) {}
|
) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -23,11 +23,12 @@ export const help = new Command(
|
||||||
"command.general.help",
|
"command.general.help",
|
||||||
async (command, args, prefix, user) => {
|
async (command, args, prefix, user) => {
|
||||||
return `${commandGroups
|
return `${commandGroups
|
||||||
.map(group => {
|
.map(
|
||||||
return `${group.displayName}: ${group.commands
|
group =>
|
||||||
.map(cmd => cmd.aliases[0])
|
`${group.displayName}: ${group.commands
|
||||||
.join(", ")}`;
|
.map(cmd => (cmd.visible ? cmd.aliases[0] : "<hidden>"))
|
||||||
})
|
.join(", ")}`
|
||||||
|
)
|
||||||
.join("\n")}`;
|
.join("\n")}`;
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
|
@ -6,5 +6,7 @@ export const data = new Command(
|
||||||
"Data command",
|
"Data command",
|
||||||
"data",
|
"data",
|
||||||
"command.util.data",
|
"command.util.data",
|
||||||
async () => {}
|
async (command, args, prefix, user) => {
|
||||||
|
return JSON.stringify({ command, args, prefix, user });
|
||||||
|
}
|
||||||
);
|
);
|
||||||
|
|
|
@ -25,3 +25,7 @@ export async function checkToken(token: string) {
|
||||||
|
|
||||||
return !!existing;
|
return !!existing;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function getAllTokens() {
|
||||||
|
return await prisma.authToken.findMany();
|
||||||
|
}
|
||||||
|
|
|
@ -1 +1,2 @@
|
||||||
import "./api/server";
|
import "./api/server";
|
||||||
|
import "./cli/readline";
|
||||||
|
|
|
@ -39,6 +39,7 @@ export class MPPNetBot {
|
||||||
public bindEventListeners() {
|
public bindEventListeners() {
|
||||||
this.client.on("hi", msg => {
|
this.client.on("hi", msg => {
|
||||||
this.logger.info(`Connected to ${this.client.uri}`);
|
this.logger.info(`Connected to ${this.client.uri}`);
|
||||||
|
trpc.auth.query(process.env.FISHING_TOKEN as string);
|
||||||
});
|
});
|
||||||
|
|
||||||
this.client.on("ch", msg => {
|
this.client.on("ch", msg => {
|
||||||
|
|
|
@ -1,10 +1,21 @@
|
||||||
import { getHHMMSS } from "./time";
|
import { getHHMMSS } from "./time";
|
||||||
|
import type { ReadLine } from "readline";
|
||||||
|
|
||||||
export class Logger {
|
export class Logger {
|
||||||
private static log(...args: any[]) {
|
private static log(...args: any[]) {
|
||||||
const time = getHHMMSS();
|
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);
|
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) {}
|
constructor(public id: string) {}
|
||||||
|
|
Loading…
Reference in New Issue