Memento
Memento

Reputation: 11

How to create a google search command in a discord bot?

I am extremely new to Javascript and Discord.js, meaning that I copy most of my code online and try to understand them. I tried this google search command. However, my bot does not send anything. The only thing it sends is "Need Input" when I type .google. When I do input a search, it does not complete the task. It does not give me an error in my command prompt. Have I done something wrong? Do you have a completely different code? PS. My code is from https://github.com/OblivionSan/discord-googlebot/blob/master/commands/general/google.js

I have installed npm i google, but it sends me a lot of errors when I do.

const google = require('google');
const Discord = require(`discord.js`);
exports.run = (client, message) => {
   if (!suffix) {
        message.channel.send({
            embed: {
                color: 0xff2727,
                description: `:warning: **${message.author.username}**, You didn't give me anything to search. {.google \`input\`}`,
            }
        });
    }
    google.resultsPerPage = 5;
    google(suffix, function (err, res) {
        if (err) message.channel.send({
            embed: {
                color: 0xff2727,
                description: `:warning: **${message.author.username}**, ${err}`,
                footer: {
                    text: 'API Lantancy is ' + `${Date.now() - message.createdTimestamp}` + ' ms',
                }
            }
        });
        for (var i = 0; i < res.links.length; ++i) {
            var link = res.links[i];
            if (!link.href) {
                res.next;
            } else {
                let embed = new Discord.RichEmbed()
                    .setColor(`#ffffff`)
                    .setAuthor(`Result for "${suffix}"`, `https://upload.wikimedia.org/wikipedia/commons/thumb/5/53/Google_%22G%22_Logo.svg/2000px-Google_%22G%22_Logo.svg.png`)
                    .setDescription(`**Link**: [${link.title}](${link.href})\n**Description**:\n${link.description}`)
                    .setTimestamp()
                    .setFooter('API Lantancy is ' + `${Date.now() - message.createdTimestamp}` + ' ms', message.author.displayAvatarURL);
                return message.channel.send({
                    embed: embed
                });
            } return message.react("👌");
        }
    });
};

I expect a google search but get basically nothing. I get left on read :/

Upvotes: 1

Views: 14849

Answers (2)

user14488588
user14488588

Reputation:

That module is not working as of now, as far as I can tell. I'm using the GoogleIt module for my bot, and here's the code that I'm using for a sample:

const googleIt = require('google-it')
const Discord = require(`discord.js`);

exports.run = (bot, message, args) => {
    const embed = new Discord.RichEmbed()
        .setTitle("Google Search Results")
        .setColor(3426654)
        .setTimestamp()
            

    googleIt({'query': args.join(' ')}).then(results => {
        results.forEach(function(item, index) { 
            embed.addField((index + 1) + ": " + item.title, "<" + item.link + ">");
        });
        
        message.channel.send(embed);
    }).catch(e => {
        // any possible errors that might have occurred (like no Internet connection)
    });
};

module.exports.help = {
    name: 'google',
    aliases: []
}

Upvotes: 2

jemiloii
jemiloii

Reputation: 25749

Check what I have below and see if that works. I usually use an object for the embed. You can generate / see one here => https://leovoel.github.io/embed-visualizer/ when you click the generate button and select discord.js

// this config option doesn't really need to be in your method / function
google.resultsPerPage = 5;

client.on('message', (message) => {
  // Using !search as a suffix in a regex
  if (/!search/.test(message.content))) {
    // remove the suffix
    const search = message.content.replace('!search ', '');

    google('node.js best practices', (err, res) => {
      if (err) console.error(err)

      for (var i = 0; i < res.links.length; ++i) {
        var link = res.links[i];

        // At this point, you should see your data and just have to format your embed
        console.log(link.title + ' - ' + link.href)
        console.log(link.description + "\n")
      }
    }
  }
});

Upvotes: 1

Related Questions