Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

84
Vistas
Async function needed somewhere

I'm coding a Discord js V12 bot, specifically the image command. Since I simply use the NPM package images-scraper, you can also look up NSFW images. So, what I am doing now, is adding a profanity filter. This is my code now:

    const blacklist = require("./../Other/profanity.js");

    blacklist.forEach((word) => {
      if (message.content.toLowerCase().includes(word.toLowerCase())) {
        message.delete();
        message.channel.send("Let's try to keep it family friendly!");
        console.log(
          `${message.author} used the word ${word} in an image search.`
        );

        return;
      } else {
        const image_results = await google.scrape(image_query, 1);
        await message.channel.send(image_results[0].url);
      };
    });

I left some unnecessary code out.

But, if I run this command, it gives me the SyntaxError: await is only valid in async function error. This await applies to the first await, before google. So, does anyone know where I can put in a async function? I already have async execute somewhere at the top, but it doesn't seem to work. I am sorry if this is a duplicate question, but since it's so specific, I just thougt I'd ask it anyway.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can't call an async function inside a forEach loop in that way, you could change it to a normal for ... of loop and put it inside a async function, you can only call a async function with await if you are already inside a async function.

I tried to write the code bellow

async function scrapeImages() {
  for(cont word of blacklist) {
    if (message.content.toLowerCase().includes(word.toLowerCase())) {
      message.delete();
      message.channel.send("Let's try to keep it family friendly!");
      console.log(
        `${message.author} used the word ${word} in an image search.`
      );

      return;
    } else {
      const image_results = await google.scrape(image_query, 1);
      await message.channel.send(image_results[0].url);
    };
  };
}

scrapeImages()

Or if you want to call scrapeImages asynchronously because of any reason you may have, you can put it inside another async function and call it this way:

await scrapeImages()
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda