nice commit

pull/18/head
TheCodedProf 3 years ago
parent 9f4cf9f444
commit 003160ffab

@ -1,9 +1,10 @@
import { ActionRowBuilder, ButtonBuilder, ButtonStyle, ChannelSelectMenuBuilder, CommandInteraction, SlashCommandSubcommandBuilder } from "discord.js"; import { ActionRowBuilder, APIMessageComponentEmoji, ButtonBuilder, ButtonStyle, ChannelSelectMenuBuilder, ChannelType, CommandInteraction, SlashCommandSubcommandBuilder } from "discord.js";
import type Discord from "discord.js"; import type Discord from "discord.js";
import client from "../../utils/client.js"; import client from "../../utils/client.js";
import { LoadingEmbed } from "../../utils/defaults.js"; import { LoadingEmbed } from "../../utils/defaults.js";
import compare from "lodash" import compare from "lodash"
import EmojiEmbed from "../../utils/generateEmojiEmbed.js"; import EmojiEmbed from "../../utils/generateEmojiEmbed.js";
import getEmojiByName from "../../utils/getEmojiByName.js";
export const command = new SlashCommandSubcommandBuilder() export const command = new SlashCommandSubcommandBuilder()
.setName("autopublish") .setName("autopublish")
@ -24,14 +25,14 @@ export const callback = async (interaction: CommandInteraction): Promise<void> =
.addComponents( .addComponents(
new ButtonBuilder() new ButtonBuilder()
.setCustomId("switch") .setCustomId("switch")
.setLabel(data.enabled ? "Disabled" : "Enabled") .setLabel(data.enabled ? "Enabled" : "Disabled")
.setStyle(data.enabled ? ButtonStyle.Danger : ButtonStyle.Success) .setStyle(data.enabled ? ButtonStyle.Success : ButtonStyle.Danger)
.setEmoji(data.enabled ? "✅" : "❌"), .setEmoji(getEmojiByName("CONTROL." + (data.enabled ? "TICK" : "CROSS"), "id") as APIMessageComponentEmoji),
new ButtonBuilder() new ButtonBuilder()
.setCustomId("save") .setCustomId("save")
.setLabel("Save") .setLabel("Save")
.setStyle(ButtonStyle.Success) .setStyle(ButtonStyle.Success)
.setEmoji("💾") .setEmoji(getEmojiByName("ICONS.SAVE", "id") as APIMessageComponentEmoji)
.setDisabled(compare.isEqual(data, config.autoPublish)) .setDisabled(compare.isEqual(data, config.autoPublish))
); );
@ -41,11 +42,18 @@ export const callback = async (interaction: CommandInteraction): Promise<void> =
.setCustomId("channel") .setCustomId("channel")
.setPlaceholder("Select a channel") .setPlaceholder("Select a channel")
.setMinValues(1) .setMinValues(1)
.setChannelTypes(ChannelType.GuildAnnouncement, ChannelType.AnnouncementThread)
); );
const current = data.channels.map((c) => `> <#${c}>`).join("\n") || "*None set*";
const embed = new EmojiEmbed() const embed = new EmojiEmbed()
.setTitle("Auto Publish")
.setDescription("Currently enabled in:\n" + current)
.setStatus('Success')
.setEmoji("ICONS.PUBLISH")
await interaction.editReply({ await interaction.editReply({
embeds: [embed], embeds: [embed],
components: [channelSelect, buttons] components: [channelSelect, buttons]
}); });

@ -26,6 +26,7 @@
"LOGGING": "999613304446144562", "LOGGING": "999613304446144562",
"SAVE": "1065722246322200586", "SAVE": "1065722246322200586",
"REORDER": "1069323453909454890", "REORDER": "1069323453909454890",
"PUBLISH": "1081691380004421743",
"NOTIFY": { "NOTIFY": {
"ON": "1000726394579464232", "ON": "1000726394579464232",
"OFF": "1078058136092541008" "OFF": "1078058136092541008"

@ -79,7 +79,7 @@ export default async function (interaction: CommandInteraction | MessageComponen
"You can view the transcript using the link below. You can save the link for later" + "You can view the transcript using the link below. You can save the link for later" +
(guildConfig.logging.logs.channel (guildConfig.logging.logs.channel
? ` or find it in <#${guildConfig.logging.logs.channel}> once you press delete below. After this the channel will be deleted.` ? ` or find it in <#${guildConfig.logging.logs.channel}> once you press delete below. After this the channel will be deleted.`
: ".") : ". It will be automatically deleted after 30 days if a logging channel is not set.")
) )
.setStatus("Success") .setStatus("Success")
.setEmoji("CONTROL.DOWNLOAD") .setEmoji("CONTROL.DOWNLOAD")

@ -186,11 +186,20 @@ interface TranscriptSchema {
createdBy: TranscriptAuthor; createdBy: TranscriptAuthor;
} }
interface findDocSchema { channelID:string, messageID: string; transcript: string }
export class Transcript { export class Transcript {
transcripts: Collection<TranscriptSchema>; transcripts: Collection<TranscriptSchema>;
messageToTranscript: Collection<findDocSchema>;
constructor() { constructor() {
this.transcripts = database.collection<TranscriptSchema>("transcripts"); 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">) { async create(transcript: Omit<TranscriptSchema, "code">) {
@ -209,13 +218,84 @@ export class Transcript {
} }
const doc = await this.transcripts.insertOne(Object.assign(transcript, { code: code }), collectionOptions); 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]; else return [null, null, null];
} }
async delete(code: string) {
// console.log("Transcript delete")
await this.transcripts.deleteOne({ code: code });
}
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 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) { async read(code: string, key: string, iv: string) {
// console.log("Transcript read") // console.log("Transcript read")
const doc = await this.transcripts.findOne({ code: code }); 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; if(!doc) return null;
for(const message of doc.messages) { for(const message of doc.messages) {
if(message.content) { if(message.content) {
@ -226,14 +306,6 @@ export class Transcript {
return doc; 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) { async createTranscript(messages: Message[], interaction: MessageComponentInteraction | CommandInteraction, member: GuildMember) {
const interactionMember = await interaction.guild?.members.fetch(interaction.user.id) const interactionMember = await interaction.guild?.members.fetch(interaction.user.id)
const newOut: Omit<TranscriptSchema, "code"> = { const newOut: Omit<TranscriptSchema, "code"> = {

@ -3,6 +3,7 @@ import client from "./client.js";
import * as fs from "fs"; import * as fs from "fs";
import * as path from "path"; import * as path from "path";
import config from "../config/main.js"; import config from "../config/main.js";
import { TextChannel } from "discord.js";
class EventScheduler { class EventScheduler {
private agenda: Agenda; private agenda: Agenda;
@ -21,6 +22,19 @@ class EventScheduler {
if (role) await user.roles.remove(role); if (role) await user.roles.remove(role);
await job.remove(); await job.remove();
}); });
this.agenda.define("uploadTranscript", async (job) => {
const channelID: string | null = (await client.database.guilds.read(job.attrs.data.guild)).logging.logs.channel;
if (!channelID) return;
const channel = await client.channels.fetch(channelID);
if(!channel || !(channel instanceof TextChannel)) return;
const transcript = await client.database.transcripts.read(job.attrs.data.transcript, job.attrs.data.key, job.attrs.data.iv);
await client.database.transcripts.delete(job.attrs.data.transcript);
const file = Buffer.from(JSON.stringify(transcript), "base64");
const fileName = `${job.attrs.data.transcript}.json`;
const m = await channel.send({ files: [{ attachment: file, name: fileName }] });
await client.database.transcripts.upload({ channelID: channel.id, messageID: m.id, transcript: job.attrs.data.transcript})
await job.remove();
});
this.agenda.define("deleteFile", async (job) => { this.agenda.define("deleteFile", async (job) => {
fs.rm(path.resolve("dist/utils/temp", job.attrs.data.fileName), (e) => { client.emit("error", e as Error); }); fs.rm(path.resolve("dist/utils/temp", job.attrs.data.fileName), (e) => { client.emit("error", e as Error); });
await job.remove(); await job.remove();

Loading…
Cancel
Save