'searching image from the discord bot

I am using google-images and image-search-google npm package, but the outcome from the "usage" part of the package is either empty or 'error: can't send an empty message' I'm really confused

I tried exactly from the page, but still the same result, I found a tutorial that alike, but the same outcome now I am really confused

const {RichEmbed, Attachment} = require('discord.js')
const GoogleImage = require('image-search-google')
const {saveGoogle, google_api} = require('../config.json')
const google = new GoogleImage(saveGoogle, google_api);

module.exports.run = async(bot, message, args) => {
        google.search("John Cena").then(result => {
          if(!result) return console.log('FAILED');
          console.log(result)

          const attachment = new Attachment(result.url);
          message.channel.send(attachment);

        }).catch(e => console.log(e))
};

(node:12184) UnhandledPromiseRejectionWarning: TypeError: The resource must be a string or Buffer.
    at ClientDataResolver.resolveFile (D:\Workspace\DiscordBot\node_modules\discord.js\src\client\ClientDataResolver.js:278:27)
    at Promise.all.options.files.map.file (D:\Workspace\DiscordBot\node_modules\discord.js\src\structures\interfaces\TextBasedChannel.js:173:30)
    at Array.map (<anonymous>)
    at TextChannel.send (D:\Workspace\DiscordBot\node_modules\discord.js\src\structures\interfaces\TextBasedChannel.js:172:40)
    at google.search.then.result (D:\Workspace\DiscordBot\commands\image.js:13:27)
    at process._tickCallback (internal/process/next_tick.js:68:7)

This is the outcome I get with 'Can't send an empty message' error



Solution 1:[1]

There's two main problems I see with your current code:

  1. google.search(...) should be returning a (promised) array of objects.
  2. An image must be sent via an attachment in Discord.js.

To solve these issues, you can use this revised code:

const { Attachment } = require('discord.js');

module.exports.run = async (bot, message, args) => {
  try {
    const [result] = await google.search('John Cena', { page: 1 });

    if (!result) return await message.channel.send(':x: No images found!');

    const attachment = new Attachment(result.url);
    await message.channel.send(attachment);
  } catch(err) {
    console.error(err);
  }
}; // This semicolon isn't a mistake; you're assigning a value to a property.

Here's why it works:

  • [result] is part of the destructuring syntax. This line declares result as the first element of the array returned by the search. This fixes the problem of result being an array of objects.
  • attachment is a Discord Attachment which can be sent properly in a message. The first parameter of the constructor is the URL/path to the image. For this, we can use the url property of the object from the image search. The second parameter, which I omitted due to its lack of necessity in this case, would be the name of the file to show in Discord.
  • The attachment is sent in the message, and the image appears. Voila!

Other resources:

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1