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

253
Visualizações
Discord Bot JS interaction reply does not wait until the code finish executing

I just started to code and trying to build a discord js bot. I am scraping data from a website. My problem is that the await interaction.reply(randomFact); will execute immediately while my code have not finish scraping and return the result. I have tried async/await but still does not work.

module.exports = {
    data: new SlashCommandBuilder()
        .setName("tips")
        .setDescription("Scrap from website"),
    async execute(interaction) {
        let randomFact;
        let healthArray = [];
            const browser = await puppeteer.launch();

            const page = await browser.newPage();
            await page.goto("https://www.example.com/health-facts/"); 

            const pageData = await page.evaluate(() => {
                return {
                    html: document.documentElement.innerHTML,
                };
            });

            const $ = cheerio.load(pageData.html);

            $(".round-number")
                .find("h3")
                .each(function (i, el) {
                    let row = $(el).text().replace(/(\s+)/g, " ");
                    row = $(el)
                        .text()
                        .replace(/[0-9]+. /g, "")
                        .trim();
                    healthArray.push(row);
                });

            await browser.close();

            randomFact =
                healthArray[Math.floor(Math.random() * healthArray.length)];
        await interaction.reply(randomFact);
    },
};

the output error

Sorry if there is anything lacking in my post. I just joined stack overflow.

about 4 years ago · Juan Pablo Isaza
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