mirror of
https://github.com/qwerinope/qweribot.git
synced 2025-12-19 09:01:38 +01:00
moving to postgres, part 2
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import { redis } from "bun";
|
||||
import { Command, sendMessage } from "commands";
|
||||
import { getUserRecord, updateUserRecord } from "db/dbUser";
|
||||
import items, { type inventory, type items } from "items";
|
||||
import itemMap, { type inventory, type items } from "items";
|
||||
import { buildTimeString } from "lib/dateManager";
|
||||
import { timeout } from "lib/timeout";
|
||||
import { isInvuln, removeInvuln } from "lib/invuln";
|
||||
@@ -59,7 +59,7 @@ export default new Command({
|
||||
if (Math.floor(Math.random() * 250) === 0) itemDiff.silverbullet! += 1;
|
||||
};
|
||||
|
||||
for (const [item, amount] of Object.entries(itemDiff) as [items, number]) {
|
||||
for (const [item, amount] of Object.entries(itemDiff) as [items, number][]) {
|
||||
if (userData.inventory[item]) userData.inventory[item] += amount;
|
||||
else userData.inventory[item] = amount;
|
||||
};
|
||||
@@ -68,7 +68,7 @@ export default new Command({
|
||||
|
||||
for (const [item, amount] of Object.entries(itemDiff)) {
|
||||
if (amount === 0) continue;
|
||||
const selection = items.get(item);
|
||||
const selection = itemMap.get(item);
|
||||
if (!selection) continue;
|
||||
itemstrings.push(`${amount} ${selection.prettyName + (amount === 1 ? '' : selection.plural)}`);
|
||||
};
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
import { RedisClient } from "bun";
|
||||
import db from "db/connection";
|
||||
import { users } from "db/schema";
|
||||
import logger from "lib/logger";
|
||||
|
||||
export async function connectionCheck() {
|
||||
let pbstatus = false;
|
||||
let pgstatus = false;
|
||||
try {
|
||||
pbstatus = true;
|
||||
await db.select().from(users); // The query doesn't matter, only that it fails. This also fails if the migration hasn't taken place
|
||||
pgstatus = true;
|
||||
} catch { };
|
||||
const tempclient = new RedisClient(undefined, {
|
||||
connectionTimeout: 100,
|
||||
@@ -16,7 +19,7 @@ export async function connectionCheck() {
|
||||
redisstatus = true;
|
||||
} catch { };
|
||||
logger.info(`Currently using the "${process.env.NODE_ENV ?? "production"}" database`);
|
||||
pbstatus ? logger.ok(`Pocketbase status: good`) : logger.err(`Pocketbase status: bad`);
|
||||
pgstatus ? logger.ok(`Postgresql status: good`) : logger.err(`Postgresql status: bad`);
|
||||
redisstatus ? logger.ok(`Redis/Valkey status: good`) : logger.err(`Redis/Valkey status: bad`);
|
||||
if (!pbstatus || !redisstatus) process.exit(1);
|
||||
if (!pgstatus || !redisstatus) process.exit(1);
|
||||
};
|
||||
|
||||
@@ -1,18 +1,15 @@
|
||||
import * as schema from "db/schema";
|
||||
import logger from "lib/logger";
|
||||
|
||||
// const host = process.env.POSTGRES_HOST ?? "";
|
||||
// if (!host) { logger.enverr("POSTGRES_HOST"); process.exit(1); };
|
||||
//
|
||||
// const user = process.env.POSTGRES_USER ?? "";
|
||||
// if (!user) { logger.enverr("POSTGRES_USER"); process.exit(1); };
|
||||
// const password = process.env.POSTGRES_PASSWORD ?? "";
|
||||
// if (!password) { logger.enverr("POSTGRES_USER"); process.exit(1); };
|
||||
// const database = process.env.POSTGRES_DB ?? "twitchbot";
|
||||
//
|
||||
// const connection = { host, user, password, database };
|
||||
const url = `postgresql://admin:abcdefgh@localhost:5432/twitchbot`;
|
||||
const host = process.env.POSTGRES_HOST ?? "";
|
||||
if (!host) { logger.enverr("POSTGRES_HOST"); process.exit(1); };
|
||||
const user = process.env.POSTGRES_USER ?? "";
|
||||
if (!user) { logger.enverr("POSTGRES_USER"); process.exit(1); };
|
||||
const password = process.env.POSTGRES_PASSWORD ?? "";
|
||||
if (!password) { logger.enverr("POSTGRES_USER"); process.exit(1); };
|
||||
const database = process.env.POSTGRES_DB ?? "";
|
||||
if (!database) { logger.enverr("POSTGRES_DB"); process.exit(1); };
|
||||
const url = `postgresql://${user}:${password}@${host}/${database}`;
|
||||
|
||||
import { drizzle } from 'drizzle-orm/bun-sql';
|
||||
export default drizzle(url, {
|
||||
schema
|
||||
});
|
||||
export default drizzle(url, { schema });
|
||||
|
||||
@@ -40,7 +40,12 @@ export async function getItemStats(target: User, thismonth: boolean) {
|
||||
]);
|
||||
if (!items || !cheers) return;
|
||||
|
||||
const returnObj: inventory = {};
|
||||
const returnObj: inventory = {
|
||||
blaster: 0,
|
||||
silverbullet: 0,
|
||||
grenade: 0,
|
||||
tnt: 0,
|
||||
};
|
||||
|
||||
for (const item of items) {
|
||||
if (!returnObj[item.item]) returnObj[item.item] = 0;
|
||||
@@ -48,8 +53,8 @@ export async function getItemStats(target: User, thismonth: boolean) {
|
||||
};
|
||||
|
||||
for (const cheer of cheers) {
|
||||
if (!returnObj[cheer.cheer]) returnObj[cheer.cheer] = 0;
|
||||
returnObj[cheer.cheer]! += 1
|
||||
if (!returnObj[cheer.event]) returnObj[cheer.event] = 0;
|
||||
returnObj[cheer.event]! += 1
|
||||
};
|
||||
|
||||
return returnObj;
|
||||
|
||||
Reference in New Issue
Block a user