首页 > 解决方案 > 如何将随机谷歌图像搜索添加到不和谐机器人

问题描述

我在使用 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)位于底部附近,当我输入 discord 时,什么都不会弹出,因为我显然必须这样做function iDog(msg),但是当我这样做时,文本会稍微变暗,当我将鼠标悬停在它上面时,它会显示“iDog 已声明,但它是从未读过”,我在终端中收到错误“图像未定义”

标签: javascriptnode.jsdiscorddiscord.js

解决方案


尝试从

function iDog(msg) {

function image(message) {

推荐阅读