Skip to content

Commit

Permalink
Merge pull request #174 from fronkdev/master
Browse files Browse the repository at this point in the history
Added support for internal filtering with createTranscript method
  • Loading branch information
ItzDerock authored Jun 14, 2024
2 parents 0c42383 + da0f3df commit aa5ca76
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 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.
6 changes: 4 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,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 @@ -109,9 +109,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;
}
>;
11 changes: 9 additions & 2 deletions tests/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import { 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,7 +20,12 @@ client.on('ready', async () => {
}

console.time('transcript');
const attachment = await createTranscript(channel);

const attachment = await createTranscript(channel, {
// Filter bot messages
filter: (message) => !message.author.bot,
});

console.timeEnd('transcript');

await channel.send({
Expand Down

0 comments on commit aa5ca76

Please sign in to comment.