Development (#12)

Co-authored-by: PineaFan <ash@pinea.dev>
Co-authored-by: pineafan <pineapplefanyt@gmail.com>
Co-authored-by: PineappleFan <PineaFan@users.noreply.github.com>
Co-authored-by: Skyler <skyler3665@gmail.com>
diff --git a/src/utils/database.ts b/src/utils/database.ts
index 2e64320..06c41f0 100644
--- a/src/utils/database.ts
+++ b/src/utils/database.ts
@@ -22,6 +22,9 @@
 
 const collectionOptions = { authdb: "admin" };
 
+const getIV = () => crypto.randomBytes(16);
+
+
 export class Guilds {
     guilds: Collection<GuildConfig>;
     defaultData: GuildConfig;
@@ -203,15 +206,39 @@
         do {
             code = crypto.randomBytes(64).toString("base64").replace(/=/g, "").replace(/\//g, "_").replace(/\+/g, "-");
         } while (await this.transcripts.findOne({ code: code }));
+        const key = crypto.randomBytes(32**2).toString("base64").replace(/=/g, "").replace(/\//g, "_").replace(/\+/g, "-").substring(0, 32);
+        const iv = getIV().toString("base64").replace(/=/g, "").replace(/\//g, "_").replace(/\+/g, "-");
+        for(const message of transcript.messages) {
+            if(message.content) {
+                const encCipher = crypto.createCipheriv("AES-256-CBC", key, iv);
+                message.content = encCipher.update(message.content, "utf8", "base64") + encCipher.final("base64");
+            }
+        }
 
         const doc = await this.transcripts.insertOne(Object.assign(transcript, { code: code }), collectionOptions);
-        if(doc.acknowledged) return code;
-        else return null;
+        if(doc.acknowledged) return [code, key, iv];
+        else return [null, null, null];
     }
 
-    async read(code: string) {
+    async read(code: string, key: string, iv: string) {
         // console.log("Transcript read")
-        return await this.transcripts.findOne({ code: code });
+        const doc = await this.transcripts.findOne({ code: code });
+        if(!doc) return null;
+        for(const message of doc.messages) {
+            if(message.content) {
+                const decCipher = crypto.createDecipheriv("AES-256-CBC", key, iv);
+                message.content = decCipher.update(message.content, "base64", "utf8") + decCipher.final("utf8");
+            }
+        }
+        return doc;
+    }
+
+    async deleteAll(guild: string) {
+        // console.log("Transcript delete")
+        const filteredDocs = await this.transcripts.find({ guild: guild }).toArray();
+        for (const doc of filteredDocs) {
+            await this.transcripts.deleteOne({ code: doc.code });
+        }
     }
 
     async createTranscript(messages: Message[], interaction: MessageComponentInteraction | CommandInteraction, member: GuildMember) {