Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Features: Max number of messages per file #162

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ Both methods of generating a transcript allow for an option object as the last p
```js
const attachment = await discordTranscripts.createTranscript(channel, {
limit: -1, // Max amount of messages to fetch. `-1` recursively fetches.
limitPerFile: 2000, // Max amount of messages per file. null | undefined = no limit and will only create one file
returnType: 'attachment', // Valid options: 'buffer' | 'string' | 'attachment' Default: 'attachment' OR use the enum ExportReturnType
filename: 'transcript.html', // Only valid with returnType is 'attachment'. Name of attachment.
saveImages: false, // Download all images and include the image data in the HTML (allows viewing the image even after it has been deleted) (! WILL INCREASE FILE SIZE !)
Expand Down
22 changes: 11 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,19 @@
"dist/**/*.js.map"
],
"devDependencies": {
"@types/node": "^18.11.18",
"@types/react": "^18.0.27",
"@types/react-dom": "^18.0.10",
"@typescript-eslint/eslint-plugin": "^5.49.0",
"@typescript-eslint/parser": "^5.49.0",
"@types/node": "^20.8.5",
"@types/react": "^18.2.28",
"@types/react-dom": "^18.2.13",
"@typescript-eslint/eslint-plugin": "^6.7.5",
"@typescript-eslint/parser": "^6.7.5",
"discord.js": "^14.13.0",
"dotenv": "^16.0.3",
"eslint": "^8.32.0",
"dotenv": "^16.3.1",
"eslint": "^8.51.0",
"husky": "^8.0.3",
"prettier": "^2.8.3",
"prettier": "^3.0.3",
"pretty-quick": "^3.1.3",
"ts-node": "^10.9.1",
"typescript": "^5.1.6"
"typescript": "^5.2.2"
},
"dependencies": {
"@derockdev/discord-components-core": "^3.6.1",
Expand All @@ -55,9 +55,9 @@
"react-dom": "^18.2.0",
"simple-markdown": "^0.7.3",
"twemoji": "^14.0.2",
"undici": "^5.24.0"
"undici": "^5.26.3"
},
"peerDependencies": {
"discord.js": "^14.0.0 || ^15.0.0"
"discord.js": "^14.13.0"
}
}
20 changes: 18 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,11 @@ export async function generateFromMessages<T extends ExportReturnType = ExportRe
* @param options The options to use when creating the transcript
* @returns The generated transcript
*/

export async function createTranscript<T extends ExportReturnType = ExportReturnType.Attachment>(
channel: TextBasedChannel,
options: CreateTranscriptOptions<T> = {}
): Promise<ObjectType<T>> {
): Promise<ObjectType<T> | ObjectType<T>[]> {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't great since now everything returns something | something[] which means all typescript users will need add a as <what they expected> statement.

// validate type
if (!channel.isTextBased()) {
// @ts-expect-error(2339): run-time check
Expand Down Expand Up @@ -121,7 +122,22 @@ export async function createTranscript<T extends ExportReturnType = ExportReturn
if (resolvedLimit < allMessages.length) allMessages = allMessages.slice(0, limit);

// generate the transcript
return generateFromMessages<T>(allMessages.reverse(), channel, options);
if (!options.limitPerFile) {
return generateFromMessages<T>(allMessages.reverse(), channel, options);
} else {
// split the messages into groups
const transcripts = [];
for (let i = 0; i < allMessages.length; i += options.limitPerFile) {
options.filename = `${options.filename ?? `transcript-${channel.id}`}_part.${i + 1}`;

const allMessageGroups = allMessages.slice(i, i + options.limitPerFile);
const transcript = await generateFromMessages<T>(allMessageGroups, channel, options);

transcripts.push(transcript as ObjectType<T>);
}

return transcripts;
}
}

export default {
Expand Down
6 changes: 6 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
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;

/**
* The max number of messages per file. If not defined or null, all messages will be in one file.
* @default null
*/
limitPerFile?: number | null;
}
>;
Loading