DNDQoteGrabber/main.js

115 lines
3.1 KiB
JavaScript
Raw Normal View History

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.
let newLine = '*' + line.replace(/-/g, '* -**') + '**';
console.log("Writing: " + newLine)
lines.push(newLine);
}
// Write to JSON file
fs.writeFileSync('output.json', JSON.stringify(lines, null, 2));
} 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+$/;
const text = msg.content;
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)) {
fs.appendFile('message.txt', message.content + "\n", function(err) {
if (err) throw err;
});
}
parseMessages();
});
client.login('OTg0NDYxMDkyODkxODIwMTYy.GEYK76.eXLEGBVhcaav_F1_rxR0NOo0uLIKxtDk9ZnXxM');