Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

111
Visualizações
Get Channel-Ids from file and send them individually in Discordjs

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.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

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.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda