2023-12-20 14:09:06 +00:00
|
|
|
const Discord = require('discord.js');
|
|
|
|
const fs = require('fs');
|
|
|
|
const readline = require('readline');
|
|
|
|
const client = new Discord.Client({
|
|
|
|
intents: [
|
|
|
|
Discord.GatewayIntentBits.Guilds,
|
|
|
|
Discord.GatewayIntentBits.MessageContent,
|
|
|
|
Discord.GatewayIntentBits.GuildMessages
|
|
|
|
]
|
|
|
|
})
|
|
|
|
|
|
|
|
async function parseMessages() {
|
|
|
|
try {
|
|
|
|
const fileStream = fs.createReadStream('message.txt');
|
|
|
|
|
|
|
|
const rl = readline.createInterface({
|
|
|
|
input: fileStream,
|
|
|
|
crlfDelay: Infinity
|
|
|
|
});
|
|
|
|
|
|
|
|
let lines = [];
|
|
|
|
for await (const line of rl) {
|
|
|
|
console.log("reading: " + line);
|
|
|
|
// Process the line.
|
2023-12-20 16:53:10 +00:00
|
|
|
let newline = "*"+line.replace(/-/g, " - ").replace(/ /g, " ").replace(/- /g, '* - **').replace(/"/g, "'").replace(/ \* - \*\*/g, "*\n - **")+"**";
|
|
|
|
/*
|
|
|
|
let newline1 = line.replace(/-/g, " - ");
|
|
|
|
let newline2 = newline1.replace(/ /g, " ");S
|
|
|
|
let newLine3 = '*' + newline2.replace(/- /g, '* - **') + '**';
|
|
|
|
let newline4 = newLine3.replace(/"/g, "'");
|
|
|
|
*/
|
|
|
|
console.log("Writing: " + newline)
|
|
|
|
lines.push(newline);
|
2023-12-20 14:09:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write to JSON file
|
2023-12-20 16:53:10 +00:00
|
|
|
fs.writeFileSync('funfacts.json', JSON.stringify(lines, null, 2));
|
2023-12-20 14:09:06 +00:00
|
|
|
} catch (error) {
|
|
|
|
console.error('An error occurred:', error);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
parseMessages();
|
|
|
|
|
|
|
|
async function fetchAllMessages() {
|
|
|
|
return new Promise(async (resolve, reject) => {
|
|
|
|
const channel = client.channels.cache.get("1184068486436229160");
|
|
|
|
let messages = [];
|
|
|
|
|
|
|
|
// Create message pointer
|
|
|
|
let message = await channel.messages
|
|
|
|
.fetch({
|
|
|
|
limit: 1
|
|
|
|
})
|
|
|
|
.then(messagePage => (messagePage.size === 1 ? messagePage.at(0) : null));
|
|
|
|
|
|
|
|
while (message) {
|
|
|
|
await channel.messages
|
|
|
|
.fetch({
|
|
|
|
limit: 100,
|
|
|
|
before: message.id
|
|
|
|
})
|
|
|
|
.then(messagePage => {
|
|
|
|
messagePage.forEach(msg => messages.push(msg));
|
|
|
|
|
|
|
|
// Update our message pointer to be the last message on the page of messages
|
|
|
|
message = 0 < messagePage.size ? messagePage.at(messagePage.size - 1) : null;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
messages.forEach(msg => {
|
|
|
|
const regex = /\- ?\S+$/;
|
2023-12-20 16:53:10 +00:00
|
|
|
const text = msg.content.replace(/\r?\n/g, ' ');
|
2023-12-20 14:09:06 +00:00
|
|
|
|
|
|
|
if (regex.test(text)) {
|
|
|
|
fs.appendFile('message.txt', text + "\n", function(err) {
|
|
|
|
if (err) reject(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
client.on('ready', async () => {
|
|
|
|
console.log("Ready!")
|
|
|
|
|
|
|
|
fs.writeFile('message.txt', '', function(err) {
|
|
|
|
if (err) {
|
|
|
|
return console.log(err);
|
|
|
|
}
|
|
|
|
console.log("File was cleared!");
|
|
|
|
});
|
|
|
|
|
|
|
|
fetchAllMessages().then(() => {
|
|
|
|
parseMessages();
|
|
|
|
}).catch((err) => {
|
|
|
|
console.error('An error occurred:', err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
client.on('messageCreate', message => {
|
|
|
|
|
|
|
|
const regex = /\- ?\S+$/;
|
|
|
|
const text = message.content;
|
|
|
|
|
|
|
|
|
|
|
|
if (regex.test(text)) {
|
|
|
|
|
2023-12-20 16:53:10 +00:00
|
|
|
fs.appendFile('message.txt', message.content.replace(/\r?\n/g, ' ') + "\n", function(err) {
|
2023-12-20 14:09:06 +00:00
|
|
|
if (err) throw err;
|
|
|
|
});
|
2023-12-20 16:53:10 +00:00
|
|
|
|
|
|
|
parseMessages();
|
2023-12-20 14:09:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
client.login('OTg0NDYxMDkyODkxODIwMTYy.GEYK76.eXLEGBVhcaav_F1_rxR0NOo0uLIKxtDk9ZnXxM');
|