1

I was making a discord bot using NodeJS, and everytime i go boot up the bot it shows this error:

headers = (await fetch(filtered.fileUrl, { method: 'HEAD' })).headers
                 ^^^^^
SyntaxError: Unexpected identifier

What i knew is that, fetch requires node-fetch. At first, i thought the problem was that i was using [email protected], which doesn't support const fetch = require('node-fetch') but even when i downgraded it to [email protected] this exact issue still persists.

Full code:

const fetch = require('node-fetch')
const Booru = require('booru')
const Discord = require('discord.js')
const { escapeMarkdown } = Discord.Util
const path = require('path')
const Color = `RANDOM`;
    module.exports = {
      info: {
        name: "booru",
        description: "booru image scraper",
        cooldown: 30,
      },
      async execute(client, message, args, Discord) {
        const tag_query = args.join(' ');

        if (!message.content.includes('help')) {
          if (!message.content.includes('something')) {
            const hornyEmbed = new Discord.MessageEmbed()
              .setTitle('embed cut to preserve space')
            if (!message.channel.nsfw) return message.channel.send(hornyEmbed)

            Booru.search('gelbooru', tag_query, {
                limit: 1,
                random: true
              })
              .then(posts => {
                const filtered = posts.blacklist(['blacklist, of course.'])
                if (filtered.length === 0) {
                  const notfoundEmbed = new Discord.MessageEmbed()
                    .setDescription("embed cut to preserve space")
                  message.channel.send(notfoundEmbed)
                }

                let tags =
                  filtered.tags.join(', ').length < 50 
                  ? Discord.Util.escapeMarkdown(filtered.tags.join(', ')) 
                  : Discord.Util.escapeMarkdown(filtered.tags.join(', ').substr(0, 50)) 
                  + `... [See All](https://giraffeduck.com/api/echo/?w=${Discord.Util
                                            .escapeMarkdown(filtered.tags.join(',').replace(/(%20)/g, '_'))
                                            .replace(/([()])/g, '\\$1')
                                            .substring(0, 1200)})`

                let headers
                let tooBig = false
                let imgError = false

                try {
                  headers = (await fetch(filtered.fileUrl, {
                    method: 'HEAD'
                  })).headers
                } catch (e) {
                  imgError = true
                }

                if (headers) {
                  tooBig = parseInt(headers.get('content-length'), 10) / 1000000 > 10
                }

                for (let post of posts) {

                  embed_nsfw = new Discord.MessageEmbed()
                    .setTitle('you horny')
                    .setColor('#FFC0CB')
                    .setAuthor('-')
                    .setDescription(`-` +
                      `**Provided by:** Gelbooru.com | `, +`[**Booru Page**](${filtered.postView}) | ` +
                      `**Rating:** ${filtered.rating.toUpperCase()} | ` +
                      `**File:** ${path.extname(filtered.file_url).toLowerCase()}, ${headers ? fileSizeSI(headers.get('content-length')) : '? kB'}\n` +
                      `**Tags:** ${tags}` +
                      (!['.jpg', '.jpeg', '.png', '.gif'].includes(
                          path.extname(filtered.fileURL).toLowerCase(),
                        ) ?
                        '`The file will probably not embed.`' :
                        '') +
                      (tooBig ? '\n`The image is over 10MB and will not embed.`' : '') +
                      (imgError ? '\n`I got an error while trying to get the image.`' : ''),
                    )
                    .setImage(filtered.sampleUrl)
                  message.channel.send(embed_nsfw);
                }
              })
          }
          if (message.content.includes('something')) {
            const helpEmbed = new Discord.MessageEmbed()
              .setTitle('cut to preserver space')
            message.channel.send(helpEmbed)
          }
        }
        if (message.content.includes('help')) {
          const helpEmbed = new Discord.MessageEmbed()
            .setTitle('cut to preserver space')
          message.channel.send(helpEmbed)
        }
      }
    }

I know this code is extremely messy, because it's basically a frankenstein's code put together from different codes i found. So aside from the solution for the main topic, a pointer on another issue i hadn't noticed will also be appreciated.

NodeJS version 16.13.0, NPM version 8.1.0, DiscordJS version 12.5.3, Running on a Heroku server.

1 Answer 1

3

try add async to .then(posts => .

Booru.search('gelbooru', tag_query, {
...
  .then(async posts => {
    const filtered = posts.blacklist(['blacklist, of course.'])
    if (filtered.length === 0) {
      const notfoundEmbed = new Discord.MessageEmbed()
      .setDescription("embed cut to preserve space")
      message.channel.send(notfoundEmbed)
    }
...

  try {
    headers = (await fetch(filtered.fileUrl, {
      method: 'HEAD'
    })).headers
  } 
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, that fixed the current issue. However, another issue comes up after this was fixed. It shows this error; TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined The error possibly occurs around the .setDescription() on the const embed_nsfw. Any idea where did i went wrong?
@dimactavish Is this the full error message? In my experience, The "path" argument must be of type string. occurs when you pass undefined value to the functions of the path module. So you can check out if filtered.file_url have a value.
Thanks for the help, i just realized that it was suppossed to be filtered.fileUrl instead of filtered.file_url.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.