如何将随机Google图片搜索添加到Discord机器人

时间:2020-01-26 04:34:43

标签: javascript node.js discord discord.js

我在使用discord.js时遇到问题,我在2个小时前就做对了,但是我尝试向它添加另一个图像搜索,现在我无法将其恢复到原来的状态。我尝试重组小块代码,方括号,括号和分号,甚至尝试撤消Visual Studio中的更改。


     switch(args[0]) {
             case 'iDog':
               image(msg);
               break;
           }
           //emoji commands
           switch(args[0]) {
             case 'eYOUTUBE':
               if(args[1] === 'emoji') {
                 msg.react('668632526641692680')
               } else {
                 msg.reply('You must specify either 5 things'); //I'll decide the things later or learn how to react with a uploaded emoji
               }
             break;
             case 'eTWITTER':
              if(args[1] === 'emoji') {
                msg.react('668632550742163457')
              } else {
                msg.reply('You must specify either 5 things'); //I'll decide the things later or learn how to react with a uploaded emoji
              }
              break;
              case 'eINSTAGRAM':
              if(args[1] === 'emoji') {
                msg.react('668632483176120331')
              } else {
                msg.reply('You must specify either 5 things'); //I'll decide the things later or learn how to react with a uploaded emoji
              }
              break;
              case 'eREDDIT':
              if(args[1] === 'emoji') {
                msg.react('668632612897685524')
              } else {
                msg.reply('You must specify either 5 things'); //I'll decide the things later or learn how to react with a uploaded emoji
              }
              break;
              case 'eMEDIAMANIAC':
                if(args[1] === 'emoji') {
                  msg.react('668632652437389312')
                } else {
                  msg.reply('You must specify either 5 things'); //I'll decide the things later or learn how to react with a uploaded emoji
                }
           }



            switch(args[0]){
                case 'FACEBOOK':
                         if(args[1] === 'PROF'){
                          msg.reply('PLEASE TYPE PROF. NAME');  
                        }else{
                         msg.reply('ERROR, please type FACEBOOK followed by PROF');
                       }
                    break;            
                case 'INSTAGRAM':
                         if(args[1] === 'PROF'){
                          msg.reply('PLEASE TYPE PROF. NAME');  
                        }else{
                          msg.reply('ERROR, please type INSTAGRAM followed by PROF');
                       }
                   break;           
                case 'TWITTER':          
                         if(args[1] === 'PROF'){
                          msg.reply('PLEASE TYPE PROF. NAME');  
                        }else{
                          msg.reply('ERROR, please type TWITTER followed by PROF');
                        }
                        break;
                case 'REDDIT':
                        if(args[1] === 'PROF'){
                          msg.reply('PLEASE TYPE PROF. NAME');
                        }else{
                          msg.reply('ERROR, PLEASE TYPE REDDIT FOLLOWED BY PROF');
                        }
                        break;



                        case 'clear':
                          if(!args[1]) {
                            msg.reply('ERROR')
                          } else {
                            msg.channel.bulkDelete(args[1]);
                          }
                          var clear = msg.channel.bulkDelete(args[1]);
                          msg,channel.send('You have cleared' + clear + 'messages!');
                          break;


                        }

     })


     function iDog(msg) {

      var options = {
        url: "http://results.dogpile.com/serp?qc=images&q=" + "dog",
        method: "GET",
        headers: {
            "Accept": "text/html",
            "User-Agent": "Chrome"
           }

        };

        request(options, function(error, response, responseBody) {
          if (error) {
              return;
          }


          $ = cheerio.load(responseBody);


          var links = $(".image a.link");

          var urls = new Array(links.length).fill(0).map((v, i) => links.eq(i).attr("href"));

          console.log(urls);

          if (!urls.length) {

              return;
          }

          // Send result
          msg.channel.send( urls[Math.floor(Math.random() * urls.length)]);
      });


     }


    //bot uses the token ID to login
    bot.login(token);

如果我在底部附近做function image(msg),则在键入不和谐音时不会弹出任何内容,因为显然我必须做function iDog(msg),但是当我这样做时,文字会变得更暗,当我将鼠标悬停时在它上面,它显示“已声明iDog,但从未读取过”,并且在终端中收到错误消息“未定义图像”

1 个答案:

答案 0 :(得分:0)

尝试从以下位置更改

function iDog(msg) {

function image(message) {