Skip to content

Commit aa5ca76

Browse files
authored
Merge pull request #174 from fronkdev/master
Added support for internal filtering with createTranscript method
2 parents 0c42383 + da0f3df commit aa5ca76

File tree

4 files changed

+28
-5
lines changed

4 files changed

+28
-5
lines changed

docs/api-reference/createtranscript.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,11 @@ Defined in [discord.js](https://discord.js.org/#/docs/discord.js/main/typedef/Gu
9090
### `options: CreateTranscriptOptions`
9191

9292
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.
93+
94+
### `options.limit: number`
95+
96+
The number of messages to fetch.
97+
98+
### `options.filter: (message: Message<boolean>) => boolean`
99+
100+
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.

src/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ export async function createTranscript<T extends ExportReturnType = ExportReturn
9797
// fetch messages
9898
let allMessages: Message[] = [];
9999
let lastMessageId: string | undefined;
100-
const { limit } = options;
100+
const { limit, filter } = options;
101101
const resolvedLimit = typeof limit === 'undefined' || limit === -1 ? Infinity : limit;
102102

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

110110
// fetch messages
111111
const messages = await channel.messages.fetch(fetchLimitOptions);
112+
const filteredMessages = typeof filter === 'function' ? messages.filter(filter) : messages;
112113

113114
// add the messages to the array
114-
allMessages.push(...messages.values());
115+
allMessages.push(...filteredMessages.values());
116+
// Get the last key of 'messages', not 'filteredMessages' because you will be refetching the same messages
115117
lastMessageId = messages.lastKey();
116118

117119
// if there are no more messages, break

src/types.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { AttachmentBuilder } from 'discord.js';
1+
import type { AttachmentBuilder, Message } from 'discord.js';
22
import type { RenderMessageContext } from './generator';
33

44
export type AttachmentTypes = 'audio' | 'video' | 'image' | 'file';
@@ -72,5 +72,11 @@ export type CreateTranscriptOptions<T extends ExportReturnType> = Partial<
7272
* The max amount of messages to fetch. Use `-1` to recursively fetch.
7373
*/
7474
limit: number;
75+
76+
/**
77+
* Filter messages of the channel
78+
* @default (() => true)
79+
*/
80+
filter: (message: Message<boolean>) => boolean;
7581
}
7682
>;

tests/generate.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import { createTranscript } from '../src';
44
import { config } from 'dotenv';
55
config();
66

7+
const { GuildMessages, Guilds, MessageContent } = discord.GatewayIntentBits;
8+
79
const client = new discord.Client({
8-
intents: [discord.IntentsBitField.Flags.GuildMessages, discord.IntentsBitField.Flags.Guilds],
10+
intents: [GuildMessages, Guilds, MessageContent],
911
});
1012

1113
client.on('ready', async () => {
@@ -18,7 +20,12 @@ client.on('ready', async () => {
1820
}
1921

2022
console.time('transcript');
21-
const attachment = await createTranscript(channel);
23+
24+
const attachment = await createTranscript(channel, {
25+
// Filter bot messages
26+
filter: (message) => !message.author.bot,
27+
});
28+
2229
console.timeEnd('transcript');
2330

2431
await channel.send({

0 commit comments

Comments
 (0)