From 9603862910434db6021831ebf02fb8c41d9e8874 Mon Sep 17 00:00:00 2001 From: derock Date: Fri, 3 Jun 2022 16:10:27 -0400 Subject: [PATCH] feat: add support for djs v14, closes #34 --- src/exporthtml.ts | 27 ++++++++++++++++++++------- src/index.ts | 5 ++++- tests/generate-v14.ts | 30 ++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 8 deletions(-) create mode 100644 tests/generate-v14.ts diff --git a/src/exporthtml.ts b/src/exporthtml.ts index f4b3910..34f613d 100644 --- a/src/exporthtml.ts +++ b/src/exporthtml.ts @@ -9,21 +9,34 @@ import { minify } from 'html-minifier'; import { parse as emoji } from 'twemoji-parser'; import { internalGenerateOptions, ObjectType, ReturnTypes } from './types'; -import { charCodeUTF32, downloadImageToDataURL } from './utils'; +import { downloadImageToDataURL } from './utils'; const template = fs.readFileSync(path.join(__dirname, 'template.html'), 'utf8'); -const version = require('../package.json').version; +const version = require('../package.json').version; +const isDJSv14 = discord.version.startsWith('14'); +// @ts-ignore +const Attachment = (isDJSv14 ? discord.Attachment : discord.MessageAttachment) as (typeof discord.MessageAttachment); + +if(!process.env.HIDE_TRANSCRIPT_WARNINGS && isDJSv14) + console.log('[WARN] discord-html-transcripts was designed to work with v13, but you are using v14. Please note that some bugs may occur.'); // copilot helped so much here // copilot smart 🧠 async function generateTranscript( messages: discord.Message[], - channel: discord.TextBasedChannel, + inputChannel: discord.TextBasedChannel, opts: internalGenerateOptions = { returnType: 'buffer' as T, fileName: 'transcript.html' } ): Promise> { - if(channel.type === "DM" || channel.isThread()) - throw new Error("Cannot operate on DM channels or thread channels"); + if( + (isDJSv14 + // @ts-ignore + ? inputChannel.type === 1 // djs v14 uses 1 for dm + : inputChannel.type === "DM") + || inputChannel.isThread() + ) throw new Error("Cannot operate on DM channels or thread channels"); + + const channel = inputChannel as (discord.NewsChannel | discord.TextChannel); const dom = new JSDOM(template.replace('{{TITLE}}', channel.name)); const document = dom.window.document; @@ -115,7 +128,7 @@ async function generateTranscript( if(author.bot) { const botTag = document.createElement('span'); botTag.classList.add('chatlog__bot-tag'); - botTag.textContent = (author.flags?.has("VERIFIED_BOT") ? '✔ ' : '') + 'BOT'; + botTag.textContent = (author.flags?.has(65536 /* verified bot flag */) ? '✔ ' : '') + 'BOT'; content.appendChild(botTag); } @@ -529,7 +542,7 @@ async function generateTranscript( return Buffer.from(serialized) as ObjectType; if(opts.returnType === "attachment") - return new discord.MessageAttachment(Buffer.from(serialized), opts.fileName ?? 'transcript.html') as ObjectType; + return new Attachment(Buffer.from(serialized), opts.fileName ?? 'transcript.html') as ObjectType; // should never get here. return serialized as ObjectType; diff --git a/src/index.ts b/src/index.ts index 379f4f2..fb570dd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -83,7 +83,10 @@ export const createTranscript = async (channel: ValidTextChannels, opts?: Create var last_id: string | undefined; while (true) { - const messages = await channel.messages.fetch({ limit: 100, before: last_id }); + const options = { limit: 100, before: last_id }; + if(!last_id) delete options['before']; + + const messages = await channel.messages.fetch(); sum_messages.push(...Array.from(messages.values())); last_id = messages.last()?.id; diff --git a/tests/generate-v14.ts b/tests/generate-v14.ts new file mode 100644 index 0000000..c0e09b0 --- /dev/null +++ b/tests/generate-v14.ts @@ -0,0 +1,30 @@ +// @ts-nocheck +// ^ UNCOMMENT IF v14 INSTALLED + +import * as discord from 'discord.js'; +import { createTranscript } from '../dist'; + +const client = new discord.Client({ + intents: [discord.IntentsBitField.Flags.GuildMessages, discord.IntentsBitField.Flags.Guilds] +}); + +client.on('ready', async () => { + /** @type {discord.TextChannel} */ + const channel = await client.channels.fetch(process.env.CHANNEL!); + + if(!channel || !channel.isText()) { + console.error('Invalid channel provided.'); + process.exit(1); + } + + const attachment = await createTranscript(channel, { minify: true }); + + await channel.send({ + files: [attachment] + }); + + client.destroy(); + process.exit(0); +}); + +client.login(process.env.TOKEN!); \ No newline at end of file