mirror of
https://gitlab.com/qwerinope/qweribot.git
synced 2026-02-04 16:06:59 +01:00
rename bot directory to src, add chatwidget
This commit is contained in:
44
src/db/connection.ts
Normal file
44
src/db/connection.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import type { AccessToken } from "@twurple/auth";
|
||||
import PocketBase, { RecordService } from "pocketbase";
|
||||
import type { inventory } from "../items";
|
||||
import logger from "../lib/logger";
|
||||
|
||||
const pocketbaseurl = process.env.POCKETBASE_URL ?? "localhost:8090";
|
||||
if (pocketbaseurl === "") { logger.enverr("POCKETBASE_URL"); process.exit(1); };
|
||||
|
||||
export type authRecord = {
|
||||
id: string;
|
||||
accesstoken: AccessToken;
|
||||
};
|
||||
|
||||
export type userRecord = {
|
||||
id: string;
|
||||
username: string; // Don't use this, Use User.username or User.displayName. This is just to make the pocketbase data easier to read.
|
||||
balance: number;
|
||||
inventory: inventory;
|
||||
lastlootbox: string;
|
||||
};
|
||||
|
||||
export type usedItemRecord = {
|
||||
id?: string;
|
||||
user: string;
|
||||
item: string;
|
||||
created: string;
|
||||
};
|
||||
|
||||
export type timeoutRecord = {
|
||||
id?: string;
|
||||
user: string;
|
||||
target: string;
|
||||
item: string;
|
||||
created: string;
|
||||
};
|
||||
|
||||
interface TypedPocketBase extends PocketBase {
|
||||
collection(idOrName: 'auth'): RecordService<authRecord>;
|
||||
collection(idOrName: 'users'): RecordService<userRecord>;
|
||||
collection(idOrName: 'usedItems'): RecordService<usedItemRecord>;
|
||||
collection(idOrName: 'timeouts'): RecordService<timeoutRecord>;
|
||||
};
|
||||
|
||||
export default new PocketBase(pocketbaseurl).autoCancellation(false) as TypedPocketBase;
|
||||
38
src/db/dbAuth.ts
Normal file
38
src/db/dbAuth.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import type { AccessToken } from "@twurple/auth";
|
||||
import pocketbase, { type authRecord } from "./connection";
|
||||
const pb = pocketbase.collection('auth');
|
||||
|
||||
export async function createAuthRecord(token: AccessToken, userId: string) {
|
||||
try {
|
||||
const data: authRecord = {
|
||||
accesstoken: token,
|
||||
id: userId
|
||||
};
|
||||
await pb.create(data);
|
||||
} catch (err) { };
|
||||
};
|
||||
|
||||
export async function getAuthRecord(userId: string, requiredIntents: string[]) {
|
||||
try {
|
||||
const data = await pb.getOne(userId);
|
||||
if (!requiredIntents.every(intent => data.accesstoken.scope.includes(intent))) return undefined;
|
||||
return { accesstoken: data.accesstoken };
|
||||
} catch (err) {
|
||||
return undefined;
|
||||
};
|
||||
};
|
||||
|
||||
export async function updateAuthRecord(userId: string, newtoken: AccessToken) {
|
||||
try {
|
||||
const newrecord = {
|
||||
accesstoken: newtoken,
|
||||
};
|
||||
await pb.update(userId, newrecord);
|
||||
} catch (err) { };
|
||||
};
|
||||
|
||||
export async function deleteAuthRecord(userId: string): Promise<void> {
|
||||
try {
|
||||
await pb.delete(userId);
|
||||
} catch (err) { };
|
||||
};
|
||||
13
src/db/dbTimeouts.ts
Normal file
13
src/db/dbTimeouts.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import pocketbase from "./connection";
|
||||
import { User } from "../user";
|
||||
import logger from "../lib/logger";
|
||||
const pb = pocketbase.collection('timeouts');
|
||||
|
||||
export async function createTimeoutRecord(user: User, target: User, item: string): Promise<void> {
|
||||
try {
|
||||
await pb.create({ user: user.id, target: target.id, item });
|
||||
} catch (err) {
|
||||
logger.err(`Failed to create timeout record in database: user: ${user.id}, target: ${target.id}, item: ${item}`);
|
||||
logger.err(err as string);
|
||||
};
|
||||
};
|
||||
13
src/db/dbUsedItems.ts
Normal file
13
src/db/dbUsedItems.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import pocketbase from "./connection";
|
||||
import { User } from "../user";
|
||||
import logger from "../lib/logger";
|
||||
const pb = pocketbase.collection('usedItems');
|
||||
|
||||
export async function createUsedItemRecord(user: User, item: string): Promise<void> {
|
||||
try {
|
||||
await pb.create({ user: user.id, item });
|
||||
} catch (err) {
|
||||
logger.err(`Failed to create usedItem record in database: user: ${user.id}, item: ${item}`);
|
||||
logger.err(err as string);
|
||||
};
|
||||
};
|
||||
46
src/db/dbUser.ts
Normal file
46
src/db/dbUser.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import pocketbase, { type userRecord } from "./connection";
|
||||
import { emptyInventory, itemarray } from "../items";
|
||||
import type { User } from "../user";
|
||||
import logger from "../lib/logger";
|
||||
|
||||
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(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)) data.inventory[key] = 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({
|
||||
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) {
|
||||
logger.err(err as string);
|
||||
return false;
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user