【问题标题】:How to add a random google image search to a discord bot如何将随机谷歌图像搜索添加到不和谐机器人
【发布时间】:2020-06-24 05:33:43
【问题描述】:

我在使用 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 已声明,但从未被读取”,我在终端中收到错误“图像未定义”

【问题讨论】:

    标签: javascript node.js discord discord.js


    【解决方案1】:

    尝试改变

    function iDog(msg) {
    

    function image(message) {
    

    【讨论】:

    • 虽然此代码可以解决 OP 的问题,但最好包含关于您的代码如何解决 OP 问题的说明。通过这种方式,未来的访问者可以从您的帖子中学习,并将其应用到他们自己的代码中。 SO 不是编码服务,而是知识资源。此外,高质量、完整的答案更有可能得到支持。这些功能,以及所有帖子都是独立的要求,是 SO 作为一个平台的一些优势,使其与论坛区分开来。您可以编辑以添加其他信息和/或使用源文档补充您的解释。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多