Skip to content

Commit

Permalink
feat: add support for djs v14, closes #34
Browse files Browse the repository at this point in the history
  • Loading branch information
ItzDerock committed Jun 3, 2022
1 parent b759df7 commit 9603862
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 8 deletions.
27 changes: 20 additions & 7 deletions src/exporthtml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T extends ReturnTypes>(
messages: discord.Message[],
channel: discord.TextBasedChannel,
inputChannel: discord.TextBasedChannel,
opts: internalGenerateOptions = { returnType: 'buffer' as T, fileName: 'transcript.html' }
): Promise<ObjectType<T>> {
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;
Expand Down Expand Up @@ -115,7 +128,7 @@ async function generateTranscript<T extends ReturnTypes>(
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);
}

Expand Down Expand Up @@ -529,7 +542,7 @@ async function generateTranscript<T extends ReturnTypes>(
return Buffer.from(serialized) as ObjectType<T>;

if(opts.returnType === "attachment")
return new discord.MessageAttachment(Buffer.from(serialized), opts.fileName ?? 'transcript.html') as ObjectType<T>;
return new Attachment(Buffer.from(serialized), opts.fileName ?? 'transcript.html') as ObjectType<T>;

// should never get here.
return serialized as ObjectType<T>;
Expand Down
5 changes: 4 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
30 changes: 30 additions & 0 deletions tests/generate-v14.ts
Original file line number Diff line number Diff line change
@@ -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!);

0 comments on commit 9603862

Please sign in to comment.