I need help. I want to make my bot send a message to all channels in a .txt file every 6 hours but not just all messages at the same time. Betweem every message should be a 4-5 seconds cooldown. here is what I got to work:
const { Client } = require('discord.js');
const client = new Client({intents: []})
const config = require("./config.json");
const fs = require('fs'); //fs reads text files :DD
var text = fs.readFileSync('./text.txt', {"encoding": "utf-8"})
function loop() { //loop start
var sixhours = Math.floor(Math.random() * 3800000) + 21600000; //generate the random 6 hour wait
setTimeout(function(){ //wait 6 hours to send text to server
console.log(`Waited with time: ${sixhours}`)
console.log('Start sending to server.')
//Server1
var message_cooldown = Math.floor(Math.random() * 5000) + 3000; //generate the cooldown
setTimeout(function(){
client.channels.cache.get('567823454237345').send(text)
console.log('Done with Server1.')
}, message_cooldown);
// Server2
setTimeout(function(){
client.channels.cache.get('5678356424363456').send(text)
console.log('Done with Server2.')
}, message_cooldown);
//done
loop();
}, sixhours);
}
loop(); //loop end
client.login(config.token);
These two channels aren't the only ones, so is there a way to get all channel ids from a file and do the same process? It would take really long to paste every single id with code in there. With the shorter version you also have the possibility to add channel ids via command.
Edit: Answer sucked
You use JSONs to store and get data.
Get Data From JSON:
const fs = require('fs');
fs.readFile('example.json', 'utf-8', (err, data) => {
if (err) {
throw err;
}
const jsonData = JSON.parse(data.toString());
console.log(jsonData);
})
Write Data To JSON:
const fs = require('fs');
const exampleObj = {
"name": "Doe, John",
"age": "23",
"DoB": "1/1/1999"
};
const jsonData = JSON.stringify(exampleObj)
fs.writeFile('example.json', jsonData, (err) => {
if (err) {
throw err;
}
console.log("Data Written");
});
Change and utilize the code as you must for the intended effect. I am assuming you know how to structure a JSON file so I am leaving that out.