mirror of
https://github.com/qwerinope/qweribot.git
synced 2025-12-20 13:01:37 +01:00
add inventory, give and admingive commands. Handle user records in database and minor bugfixes
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import type { AccessToken } from "@twurple/auth";
|
||||
import PocketBase, { RecordService } from "pocketbase";
|
||||
import type { inventory } from "../items";
|
||||
|
||||
const pocketbaseurl = process.env.POCKETBASE_URL ?? "localhost:8090";
|
||||
if (pocketbaseurl === "") { console.error("Please provide a POCKETBASE_URL in .env."); process.exit(1); };
|
||||
@@ -13,15 +14,15 @@ export type userRecord = {
|
||||
id: string;
|
||||
username: string;
|
||||
balance: number;
|
||||
inventory: object;
|
||||
inventory: inventory;
|
||||
lastlootbox: string;
|
||||
};
|
||||
|
||||
type TypedPocketBase = {
|
||||
interface TypedPocketBase extends PocketBase {
|
||||
collection(idOrName: 'auth'): RecordService<authRecord>;
|
||||
collection(idOrName: 'users'): RecordService<userRecord>;
|
||||
};
|
||||
|
||||
const pb = new PocketBase(pocketbaseurl) as TypedPocketBase;
|
||||
export default pb.autoCancellation(false);
|
||||
|
||||
export default pb;
|
||||
|
||||
44
bot/db/dbUser.ts
Normal file
44
bot/db/dbUser.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import pocketbase, { type userRecord } from "./connection";
|
||||
import { emptyInventory, itemarray } from "../items";
|
||||
import type { User } from "../user";
|
||||
const pb = pocketbase.collection('users');
|
||||
|
||||
/** Use this function to both ensure existance and to retreive data */
|
||||
export async function getUserRecord(user: User): Promise<userRecord> {
|
||||
try {
|
||||
const data = await pb.getOne<userRecord>(user.id);
|
||||
|
||||
if (Object.keys(data.inventory).sort().toString() !== itemarray.sort().toString()) { // If the items in the user inventory are missing an item.
|
||||
itemarray.forEach(key => {
|
||||
if (!(key in data.inventory)) Object.defineProperty(data.inventory, key, { value: 0 });
|
||||
});
|
||||
};
|
||||
|
||||
return data;
|
||||
} catch (err) {
|
||||
// This gets triggered if the user doesn't exist in the database
|
||||
return await createUserRecord(user);
|
||||
};
|
||||
};
|
||||
|
||||
async function createUserRecord(user: User): Promise<userRecord> {
|
||||
const data = await pb.create<userRecord>({
|
||||
id: user.id,
|
||||
username: user.username,
|
||||
balance: 0,
|
||||
inventory: emptyInventory,
|
||||
lastlootbox: new Date(0).toISOString()
|
||||
});
|
||||
|
||||
return data;
|
||||
};
|
||||
|
||||
export async function updateUserRecord(user: User, newData: userRecord): Promise<boolean> {
|
||||
try {
|
||||
await pb.update(user.id, newData);
|
||||
return true;
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
return false;
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user