first commit, basic command handling and auth managing

This commit is contained in:
2025-05-30 22:56:46 +02:00
commit cc796765ed
17 changed files with 610 additions and 0 deletions

31
bot/db/connection.ts Normal file
View File

@@ -0,0 +1,31 @@
import Surreal from "surrealdb";
const surrealurl = process.env.SURREAL_URL ?? "";
if (surrealurl === "") { console.error("Please provide a SURREAL_URL in .env."); process.exit(1); };
const namespace = process.env.SURREAL_NAMESPACE ?? "";
if (namespace === "") { console.error("Please provide a SURREAL_NAMESPACE in .env."); process.exit(1); };
const database = process.env.SURREAL_DATABASE ?? "";
if (database === "") { console.error("Please provide a SURREAL_DATABASE in .env."); process.exit(1); };
const username = process.env.SURREAL_USERNAME ?? "";
if (username === "") { console.error("Please provide a SURREAL_USERNAME in .env."); process.exit(1); };
const password = process.env.SURREAL_PASSWORD ?? "";
if (password === "") { console.error("Please provide a SURREAL_PASSWORD in .env."); process.exit(1); };
export default async function DB(): Promise<Surreal> {
const db = new Surreal();
try {
await db.connect(surrealurl, {
auth: {
username,
password
}
});
await db.use({ namespace, database });
return db;
}
catch (err) {
console.error("Failed to connect to SurrealDB:", err instanceof Error ? err.message : String(err));
await db.close();
throw err;
};
};

78
bot/db/dbAuth.ts Normal file
View File

@@ -0,0 +1,78 @@
import type { AccessToken } from "@twurple/auth";
import DB from "./connection";
import type { RecordId } from "surrealdb";
type authRecord = {
accesstoken: AccessToken,
user: string,
};
export async function createAuthRecord(token: AccessToken, userId: string): Promise<void> {
const db = await DB();
if (!db) return;
const data: authRecord = {
accesstoken: token,
user: userId
};
try {
await db.create("auth", data);
} catch (err) {
console.error(err);
} finally {
await db.close();
};
};
type getAuthRecordQuery = authRecord & { id: RecordId };
type getAuthRecordResult = { accesstoken: AccessToken, id: RecordId };
export async function getAuthRecord(userId: string, requiredIntents: string[]): Promise<getAuthRecordResult | undefined> {
const db = await DB();
if (!db) return undefined;
try {
const data = await db.query<getAuthRecordQuery[][]>("SELECT * from auth WHERE user=$userId AND accesstoken.scope CONTAINSALL $intents;", { userId, intents: requiredIntents });
if (!data[0] || !data[0][0]) return undefined;
return { accesstoken: data[0][0].accesstoken, id: data[0][0].id };
} catch (err) {
console.error(err);
return undefined;
} finally {
await db.close();
};
};
export async function updateAuthRecord(userId: string, intents: string[], newtoken: AccessToken): Promise<boolean> {
const db = await DB();
if (!db) return false;
try {
const data = await getAuthRecord(userId, intents);
const newrecord: authRecord = {
accesstoken: newtoken,
user: userId
};
await db.update(data?.id!, newrecord);
return true;
} catch (err) {
console.error(err);
return false;
} finally {
await db.close();
};
};
export async function deleteAuthRecord(userId: string): Promise<void> {
const db = await DB();
if (!db) return;
try {
const data = await db.query<getAuthRecordQuery[][]>("SELECT * FROM auth WHERE user=$userId;", { userId });
if (!data[0] || !data[0][0]) return undefined;
console.log(data)
for (const obj of data[0]) {
db.delete(obj.id);
};
} catch (err) {
console.error(err);
};
};