nice commit
diff --git a/src/utils/database.ts b/src/utils/database.ts
index d000340..c1728c3 100644
--- a/src/utils/database.ts
+++ b/src/utils/database.ts
@@ -186,11 +186,20 @@
createdBy: TranscriptAuthor;
}
+interface findDocSchema { channelID:string, messageID: string; transcript: string }
+
export class Transcript {
transcripts: Collection<TranscriptSchema>;
+ messageToTranscript: Collection<findDocSchema>;
constructor() {
this.transcripts = database.collection<TranscriptSchema>("transcripts");
+ this.messageToTranscript = database.collection<findDocSchema>("messageToTranscript");
+ }
+
+ async upload(data: findDocSchema) {
+ // console.log("Transcript upload")
+ await this.messageToTranscript.insertOne(data);
}
async create(transcript: Omit<TranscriptSchema, "code">) {
@@ -209,21 +218,16 @@
}
const doc = await this.transcripts.insertOne(Object.assign(transcript, { code: code }), collectionOptions);
- if(doc.acknowledged) return [code, key, iv];
+ if(doc.acknowledged) {
+ client.database.eventScheduler.schedule("deleteTranscript", (Date.now() + 1000 * 60 * 60 * 24 * 7).toString(), { guild: transcript.guild, code: code, iv: iv, key: key });
+ return [code, key, iv];
+ }
else return [null, null, null];
}
- async read(code: string, key: string, iv: string) {
- // console.log("Transcript read")
- 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 delete(code: string) {
+ // console.log("Transcript delete")
+ await this.transcripts.deleteOne({ code: code });
}
async deleteAll(guild: string) {
@@ -234,6 +238,74 @@
}
}
+ async readEncrypted(code: string) {
+ // console.log("Transcript read")
+ let doc: TranscriptSchema | null = await this.transcripts.findOne({ code: code });
+ let findDoc: findDocSchema | null = null;
+ if(!doc) findDoc = (await this.messageToTranscript.findOne({ transcript: code }));
+ if(findDoc) {
+ const message = await ((client.channels.cache.get(findDoc.channelID)) as Discord.TextBasedChannel | null)?.messages.fetch(findDoc.messageID);
+ if(!message) return null;
+ const attachment = message.attachments.first();
+ if(!attachment) return null;
+ const transcript = (await fetch(attachment.url)).body;
+ if(!transcript) return null;
+ const reader = transcript.getReader();
+ let data: Uint8Array | null = null;
+ let allPacketsReceived = false;
+ while (!allPacketsReceived) {
+ const { value, done } = await reader.read();
+ if (done) {allPacketsReceived = true; continue;}
+ if(!data) {
+ data = value;
+ } else {
+ data = new Uint8Array(Buffer.concat([data, value]));
+ }
+ }
+ if(!data) return null;
+ doc = JSON.parse(Buffer.from(data).toString());
+ }
+ if(!doc) return null;
+ return doc;
+ }
+
+ async read(code: string, key: string, iv: string) {
+ // console.log("Transcript read")
+ let doc: TranscriptSchema | null = await this.transcripts.findOne({ code: code });
+ let findDoc: findDocSchema | null = null;
+ if(!doc) findDoc = (await this.messageToTranscript.findOne({ transcript: code }));
+ if(findDoc) {
+ const message = await ((client.channels.cache.get(findDoc.channelID)) as Discord.TextBasedChannel | null)?.messages.fetch(findDoc.messageID);
+ if(!message) return null;
+ const attachment = message.attachments.first();
+ if(!attachment) return null;
+ const transcript = (await fetch(attachment.url)).body;
+ if(!transcript) return null;
+ const reader = transcript.getReader();
+ let data: Uint8Array | null = null;
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition, no-constant-condition
+ while(true) {
+ const { value, done } = await reader.read();
+ if (done) break;
+ if(!data) {
+ data = value;
+ } else {
+ data = new Uint8Array(Buffer.concat([data, value]));
+ }
+ }
+ if(!data) return null;
+ doc = JSON.parse(Buffer.from(data).toString());
+ }
+ 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 createTranscript(messages: Message[], interaction: MessageComponentInteraction | CommandInteraction, member: GuildMember) {
const interactionMember = await interaction.guild?.members.fetch(interaction.user.id)
const newOut: Omit<TranscriptSchema, "code"> = {