Skip to content

Commit c61e32e

Browse files
committed
chore: enable and fix eslint errors
1 parent ad1b6b5 commit c61e32e

2 files changed

Lines changed: 50 additions & 14 deletions

File tree

eslint.config.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,5 @@ module.exports = defineConfig([
1010
rules: {
1111
"no-console": "off",
1212
},
13-
<<<<<<< HEAD
1413
},
15-
||||||| %s
16-
}
17-
=======
18-
}, {
19-
ignores: ["scripts/discord-transcript-generator.js"] // Adopted file
20-
}
21-
>>>>>>> d1b8217 (chore: ignore discord-transcript-generator for ESLint)
2214
]);

scripts/discord-transcript-generator.js

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,45 @@
55
*/
66
'use strict';
77

8-
const fs = require('fs').promises;
9-
const path = require('path');
8+
/** @import { Message, Channel } from "discord.js" */
9+
10+
const fs = require('node:fs').promises;
11+
const path = require('node:path');
1012
const { Client, GatewayIntentBits, Partials } = require('discord.js');
1113

14+
/**
15+
* Converts a date string representation to an UTC date offset.
16+
* @param {string} dateString String representation of the string
17+
* @returns {number} UTC date offset
18+
*/
1219
function getUTCDate(dateString) {
1320
const dateInstance = new Date(dateString);
21+
1422
return Date.UTC(
1523
dateInstance.getYear(),
1624
dateInstance.getMonth(),
1725
dateInstance.getDate(),
1826
);
1927
}
2028

29+
/**
30+
* Returns true for messages created at the given date.
31+
* @param {Message} message Discord message
32+
* @param {Date} date Date to compare to
33+
* @returns {boolean} True iff message created on date
34+
*/
2135
function messageMatchesDate(message, date) {
2236
// Ensure that comparisons are done using UTC.
2337
return getUTCDate(date) === getUTCDate(message.createdTimestamp);
2438
}
2539

40+
/**
41+
* Generates a transcript based on the given channel messages for a given date.
42+
* @param {Message[]} messages Channel messages
43+
* @param {Date} date Date of the messages
44+
* @param {string} name Name of the transcript
45+
* @returns {Promise<string>} Generated transcript
46+
*/
2647
async function generateContent(messages, date, name) {
2748
const generatedMessages = (
2849
await Promise.all(
@@ -52,16 +73,28 @@ async function generateContent(messages, date, name) {
5273
).join('\n\n');
5374

5475
return `# ${date} ${
55-
name ? name + ' ' : ''
76+
name ? `${name} ` : ''
5677
}Transcript\n\n${generatedMessages}\n`;
5778
}
5879

80+
/**
81+
* Returns sorted messages of the given date.
82+
* @param {Message[]} messages Channel messages
83+
* @param {Date} date Date of the messages
84+
* @returns {Message[]} Sorted by created timestamp
85+
*/
5986
function getTranscriptMessages(messages, date) {
6087
return messages
6188
.filter(message => messageMatchesDate(message, date))
6289
.sort((a, b) => a.createdTimestamp - b.createdTimestamp);
6390
}
6491

92+
/**
93+
* Fetches messages of the channel of the given date.
94+
* @param {Channel} channel Channel to get the messages from
95+
* @param {Date} date Date of the messages
96+
* @returns {Promise<Message[]>} Fetched messages
97+
*/
6598
async function fetchMessages(channel, date) {
6699
let messages = [];
67100

@@ -70,7 +103,7 @@ async function fetchMessages(channel, date) {
70103
while (true) {
71104
const batch = Array.from(
72105
await channel.messages.fetch(
73-
messages.length ? { before: messages[0].id } : undefined,
106+
messages.length ? { before: messages[0].id } : void 0,
74107
)
75108
);
76109

@@ -80,20 +113,31 @@ async function fetchMessages(channel, date) {
80113

81114
messages = [...getTranscriptMessages(batch, date), ...messages];
82115

83-
if (!messageMatchesDate(batch[batch.length - 1], date)) {
116+
if (!messageMatchesDate(batch.at(-1), date)) {
84117
break;
85118
}
86119
}
87120

88121
return messages.map(message => {
89122
message.content = message.content.replace(
90-
/<@!?(\d+)>/g,
123+
/<@!?(\d+)>/gu,
91124
(match, p1) => `@${channel.client.users.cache.get(p1).username}`,
92125
);
93126
return message;
94127
});
95128
}
96129

130+
/**
131+
* Generates a transcript file of a discord channel on the given date.
132+
* @throws {Error} Discord API errors
133+
* @param {Object} options Configuration options
134+
* @param {string} options.token Discord token
135+
* @param {string} options.id ID of the channel
136+
* @param {string} options.date Date of the messages
137+
* @param {string} options.output Name of the output file
138+
* @param {?string} [options.name] Name of the transcript
139+
* @returns {Promise<void>}
140+
*/
97141
module.exports = async function generateTranscript({
98142
token,
99143
id,

0 commit comments

Comments
 (0)