-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
db.ts
112 lines (98 loc) · 2.78 KB
/
db.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
* Copyright 2024 @xditya <https://xditya.me/github>
* Redistribution and use in source and binary forms, with or without modification, are permitted
* provided the copyright header is included and attributes are preserved.
*/
import { MongoClient, ObjectId } from "mongo";
import config from "$env";
console.log("Connecting to MongoDB...");
const client = new MongoClient();
const MONGO_URL = new URL(config.MONGO_URL);
if (!MONGO_URL.searchParams.has("authMechanism")) {
MONGO_URL.searchParams.set("authMechanism", "SCRAM-SHA-1");
}
try {
await client.connect(MONGO_URL.href);
} catch (err) {
console.error("Error connecting to MongoDB", err);
throw err;
}
const db = client.database("GeminiBot");
interface ConversationPart {
text: string;
}
interface UserDataSchema {
_id: ObjectId;
user_id: number;
conversation_history: Array<Map<string, ConversationPart[]>>;
reaction_settings: boolean;
}
const userDb = db.collection<UserDataSchema>("UserData");
async function addUser(id: number) {
const data = await userDb.findOne({ user_id: id });
if (data) return;
await userDb.insertOne({
user_id: id,
conversation_history: new Array<Map<string, ConversationPart[]>>(),
reaction_settings: true,
});
}
async function addConversation(
id: number,
conv: Array<Map<string, ConversationPart[]>>,
) {
const oldData = await userDb.findOne({ user_id: id });
if (!oldData) {
await userDb.insertOne({
user_id: id,
conversation_history: conv,
reaction_settings: true,
});
} else {
const new_conv = oldData.conversation_history.concat(conv);
await userDb.updateOne(
{ user_id: id },
{ $set: { conversation_history: new_conv } },
);
}
}
async function resetConversation(
id: number,
conv: Array<Map<string, ConversationPart[]>>,
) {
await userDb.updateOne(
{ user_id: id },
{ $set: { conversation_history: conv } },
);
}
async function getConversations(id: number) {
const data = await userDb.findOne({ user_id: id });
if (!data) return Array<Map<string, ConversationPart[]>>();
return data.conversation_history;
}
async function getStats() {
return await userDb.countDocuments();
}
async function toggelReactionSettings(id: number) {
const currentReactionSettings = await getUserReactionSettings(id);
await userDb.updateOne(
{ user_id: id },
{ $set: { reaction_settings: !currentReactionSettings } },
);
}
async function getUserReactionSettings(id: number) {
const data = await userDb.findOne({ user_id: id });
if (!data) return true;
if (data.reaction_settings === undefined) return true;
return data.reaction_settings;
}
export {
addConversation,
addUser,
type ConversationPart,
getConversations,
getStats,
getUserReactionSettings,
resetConversation,
toggelReactionSettings,
};