From 2e54a775c2ce67d106be35d5bae739e6ce60268d Mon Sep 17 00:00:00 2001 From: TheCodedProf Date: Tue, 14 Feb 2023 16:26:47 -0500 Subject: [PATCH] changing transcript saving --- TODO | 1 - src/commands/mod/ban.ts | 2 +- src/premium/createTranscript.ts | 130 +++++++++++++++++++++++++++++++- 3 files changed, 130 insertions(+), 3 deletions(-) diff --git a/TODO b/TODO index 38073c6..2a0d7be 100644 --- a/TODO +++ b/TODO @@ -1,3 +1,2 @@ -Role all (?) Server rules verificationRequired on welcome \ No newline at end of file diff --git a/src/commands/mod/ban.ts b/src/commands/mod/ban.ts index 7e67222..b500642 100644 --- a/src/commands/mod/ban.ts +++ b/src/commands/mod/ban.ts @@ -25,7 +25,7 @@ const command = (builder: SlashCommandSubcommandBuilder) => const callback = async (interaction: CommandInteraction): Promise => { if (!interaction.guild) return; const { renderUser } = client.logger; - // TODO:[Modals] Replace this with a modal + // TODO:[Modals] Replace the command arguments with a modal let reason = null; let notify = true; let confirmation; diff --git a/src/premium/createTranscript.ts b/src/premium/createTranscript.ts index 754a06b..43de767 100644 --- a/src/premium/createTranscript.ts +++ b/src/premium/createTranscript.ts @@ -7,7 +7,8 @@ import { MessageComponentInteraction, TextChannel, ButtonStyle, - User + User, + ComponentType } from "discord.js"; import EmojiEmbed from "../utils/generateEmojiEmbed.js"; import getEmojiByName from "../utils/getEmojiByName.js"; @@ -16,6 +17,64 @@ import client from "../utils/client.js"; const pbClient = new PasteClient(client.config.pastebinApiKey); +interface TranscriptEmbed { + title?: string; + description?: string; + fields?: { + name: string; + value: string; + inline: boolean; + }[]; + footer?: { + text: string; + iconURL?: string; + }; +} + +interface TranscriptComponent { + type: number; + style?: ButtonStyle; + label?: string; + description?: string; + placeholder?: string; + emojiURL?: string; +} + +interface TranscriptAuthor { + username: string; + discriminator: number; + nickname?: string; + id: string; + iconURL?: string; + topRole: { + color: number; + badgeURL?: string; + } +} + +interface TranscriptMessage { + id: string; + author: TranscriptAuthor; + content?: string; + embeds?: TranscriptEmbed[]; + components?: TranscriptComponent[][]; + editedTimestamp?: number; + createdTimestamp: number; + flags?: string[]; + attachments?: unknown; //FIXME + stickerURLs?: string[]; + referencedMessage?: string | [string, string, string]; +} + +interface Transcript { + type: "ticket" | "purge" + guild: string; + channel: string; + messages: TranscriptMessage[]; + createdTimestamp: number; + createdBy: TranscriptAuthor; +} + export default async function (interaction: CommandInteraction | MessageComponentInteraction) { if (interaction.channel === null) return; if (!(interaction.channel instanceof TextChannel)) return; @@ -45,6 +104,75 @@ export default async function (interaction: CommandInteraction | MessageComponen out += "\n\n"; } }); + + let interactionMember = await interaction.guild?.members.fetch(interaction.user.id) + + let newOut: Transcript = { + type: "ticket", + guild: interaction.guild!.id, + channel: interaction.channel!.id, + messages: [], + createdTimestamp: Date.now(), + createdBy: { + username: interaction.user.username, + discriminator: parseInt(interaction.user.discriminator), + id: interaction.user.id, + topRole: { + color: interactionMember?.roles.highest.color ?? 0x000000 + } + } + } + if(interactionMember?.roles.icon) newOut.createdBy.topRole.badgeURL = interactionMember.roles.icon.iconURL()!; + messages.reverse().forEach((message) => { + let msg: TranscriptMessage = { + id: message.id, + author: { + username: message.author.username, + discriminator: parseInt(message.author.discriminator), + id: message.author.id, + topRole: { + color: message.member!.roles.highest.color + } + }, + createdTimestamp: message.createdTimestamp + }; + if (message.member!.roles.icon) msg.author.topRole.badgeURL = message.member!.roles.icon.iconURL()!; + if (message.content) msg.content = message.content; + if (message.embeds.length > 0) msg.embeds = message.embeds.map(embed => { + let obj: TranscriptEmbed = {}; + if (embed.title) obj.title = embed.title; + if (embed.description) obj.description = embed.description; + if (embed.fields.length > 0) obj.fields = embed.fields.map(field => { + return { + name: field.name, + value: field.value, + inline: field.inline ?? false + } + }); + if (embed.footer) obj.footer = { + text: embed.footer.text, + }; + if (embed.footer?.iconURL) obj.footer!.iconURL = embed.footer.iconURL; + return obj; + }); + if (message.components.length > 0) msg.components = message.components.map(component => component.components.map(child => { + let obj: TranscriptComponent = { + type: child.type + } + if (child.type === ComponentType.Button) { + obj.style = child.style; + obj.label = child.label ?? ""; + } + return obj + })); + if (message.editedTimestamp) msg.editedTimestamp = message.editedTimestamp; + if (message.flags) msg.flags = message.flags.toArray(); + + if (message.stickers.size > 0) msg.stickerURLs = message.stickers.map(sticker => sticker.url); + if (message.reference) msg.referencedMessage = [message.reference.guildId ?? "", message.reference.channelId, message.reference.messageId ?? ""]; + + }); + const topic = interaction.channel.topic; let member: GuildMember | null = null; if (topic !== null) {