Benjamin
Benjamin
2 min read

Categories

Tags

Welcome everyone to the first discord.js guide. In the post, I will be showing you how you can create a google command for your bot. The first thing you will need to do is create a file called google.js, in these tutorials, I will be showing you just the code and not the full command eg how it works with my command handler, although if you are a patreon you will have access to the full command.

One of the first things you will need to do is install the required package so open up your console and go to your bot directory and run

sudo npm I google

This command will install the required package in order to create this command

Code

const google = require('google');
const Discord = require(`discord.js`);
exports.run = (bot, message, args) => {
    if (!args) {
        message.channel.send({
            embed: {
                color: 0xff2727,
                description: `:warning: **${message.author.username}**, You didn't give me anything to search. {^google \`input\`}`,
                footer: {
                    text: 'API Latancy is ' + `${Date.now() - message.createdTimestamp}` + ' ms',
                }
            }
        });
    }
    google.resultsPerPage = 25;
    google(args, function (err, res) {
        if (err) message.channel.send({
            embed: {
                color: 0xff2727,
                description: `:warning: **${message.author.username}**, ${err}`,
                footer: {
                    text: 'API Latancy is ' + `${Date.now() - message.createdTimestamp}` + ' ms',
                }
            }
        });
        for (var i = 0; i < res.links.length; ++i) {
            if(i == 3){
                return
            }
            var link = res.links[i];
            if (!link.href) {
                res.next;
            } else {
                let embed = new Discord.RichEmbed()
                    .setColor(`#ffffff`)
                    .setAuthor(`Result for "${args}"`, `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 Latancy is ' + `${Date.now() - message.createdTimestamp}` + ' ms', message.author.displayAvatarURL);
                message.author.send({
                    embed: embed
                });
            } message.react("👌");
        }
    });
};

If you have defined client in your main file then you will need to change all the bot’s to client in this code

Near the bottom of the code, I have made it so the command only shows the top 3 results but if you would like to make it show more then 3 results you can change the following

if(i == 3){
return
}

I have also made it so it pms the author instead of posting it in the channel it was run in to prevent spam, although if you would like to change this you can change message.author.send to message.channel.send

In order to test this command all you need to do is run [PREFIX]google Bencey And see if the command worked correctly, if not you will need to check the bot logs and see what is wrong

I hope this guide helps you to create a functioning google command with your bot (If not you’re doing something wrong), If you want to see more guides like this please make sure you let me know :)

  • Bencey

Sponsored By My Gay Lover (Panda)