Skip to content

Commit

Permalink
Merge branch 'master' into feat/extended-image-download-opts
Browse files Browse the repository at this point in the history
  • Loading branch information
ItzDerock authored Jun 14, 2024
2 parents cc95e47 + aa5ca76 commit 9c416c1
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 12 deletions.
8 changes: 8 additions & 0 deletions docs/api-reference/createtranscript.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,11 @@ Defined in [discord.js](https://discord.js.org/#/docs/discord.js/main/typedef/Gu
### `options: CreateTranscriptOptions`

The same options as [generatefrommessages.md](generatefrommessages.md 'mention') but adds the `limit` option which lets you limit set the number of messages to fetch.

### `options.limit: number`

The number of messages to fetch.

### `options.filter: (message: Message<boolean>) => boolean`

A function that will be called for each message to determine if it should be included in the transcript. If false, the message will not be included.
2 changes: 1 addition & 1 deletion src/generator/renderers/reply.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default async function MessageReply({ message, context }: { message: Mess
roleColor={referencedMessage.member?.displayHexColor ?? undefined}
bot={!isCrosspost && referencedMessage.author.bot}
verified={referencedMessage.author.flags?.has(UserFlags.VerifiedBot)}
op={message.channel.isThread() && referencedMessage.author.id === message.channel.ownerId}
op={message?.channel?.isThread?.() && referencedMessage.author.id === message?.channel?.ownerId}
server={isCrosspost ?? undefined}
command={isCommand}
>
Expand Down
6 changes: 4 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export async function createTranscript<T extends ExportReturnType = ExportReturn
// fetch messages
let allMessages: Message[] = [];
let lastMessageId: string | undefined;
const { limit } = options;
const { limit, filter } = options;
const resolvedLimit = typeof limit === 'undefined' || limit === -1 ? Infinity : limit;

// until there are no more messages, keep fetching
Expand All @@ -123,9 +123,11 @@ export async function createTranscript<T extends ExportReturnType = ExportReturn

// fetch messages
const messages = await channel.messages.fetch(fetchLimitOptions);
const filteredMessages = typeof filter === 'function' ? messages.filter(filter) : messages;

// add the messages to the array
allMessages.push(...messages.values());
allMessages.push(...filteredMessages.values());
// Get the last key of 'messages', not 'filteredMessages' because you will be refetching the same messages
lastMessageId = messages.lastKey();

// if there are no more messages, break
Expand Down
8 changes: 7 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { AttachmentBuilder } from 'discord.js';
import type { AttachmentBuilder, Message } from 'discord.js';
import type { RenderMessageContext } from './generator';

export type AttachmentTypes = 'audio' | 'video' | 'image' | 'file';
Expand Down Expand Up @@ -72,5 +72,11 @@ export type CreateTranscriptOptions<T extends ExportReturnType> = Partial<
* The max amount of messages to fetch. Use `-1` to recursively fetch.
*/
limit: number;

/**
* Filter messages of the channel
* @default (() => true)
*/
filter: (message: Message<boolean>) => boolean;
}
>;
14 changes: 6 additions & 8 deletions tests/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import { TranscriptImageDownloader, createTranscript } from '../src';
import { config } from 'dotenv';
config();

const { GuildMessages, Guilds, MessageContent } = discord.GatewayIntentBits;

const client = new discord.Client({
intents: [discord.IntentsBitField.Flags.GuildMessages, discord.IntentsBitField.Flags.Guilds],
intents: [GuildMessages, Guilds, MessageContent],
});

client.on('ready', async () => {
Expand All @@ -18,15 +20,11 @@ client.on('ready', async () => {
}

console.time('transcript');

const attachment = await createTranscript(channel, {
// saveImages: true,
callbacks: {
resolveImageSrc: new TranscriptImageDownloader()
.withMaxSize(5120) // 5MB in KB
.withCompression(40, true) // 40% quality, convert to webp
.build(),
},
// options go here
});

console.timeEnd('transcript');

await channel.send({
Expand Down

0 comments on commit 9c416c1

Please sign in to comment.