如何在Discord机器人中创建Google搜索命令?

时间:2019-07-02 15:56:06

标签: javascript discord.js

我对Javascript和Discord.js极为陌生,这意味着我在线复制了大部分代码并试图理解它们。我尝试了这个Google搜索命令。但是,我的机器人没有发送任何东西。当我输入.google时,它发送的唯一内容是“需要输入”。当我输入搜索内容时,它无法完成任务。它在命令提示符下没有给我错误。我做错什么了吗?您有完全不同的代码吗? PS。我的代码来自https://github.com/OblivionSan/discord-googlebot/blob/master/commands/general/google.js

我已经在Google上安装了npm,但是当我这样做时,它会向我发送很多错误。

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("?");
        }
    });
};

我希望谷歌搜索,但基本上什么也得不到。我继续阅读:/

2 个答案:

答案 0 :(得分:1)

据我所知,该模块目前无法正常工作。我将 GoogleIt 模块用于 my bot,以下是我用于示例的代码:

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[0]}).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: []
}

答案 1 :(得分:0)

检查下面的内容,看看是否可行。我通常使用一个对象进行嵌入。单击生成按钮并选择discord.js时,您可以在此处生成/看到一个== https://leovoel.github.io/embed-visualizer/

// 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")
      }
    }
  }
});