Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

83
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!