nodejs,函数无缘无故地跳转到完全不相关的行?

时间:2015-08-31 11:06:14

标签: javascript node.js

所以我试图在已经存在的项目constructor中添加命令以检索英雄联盟玩家的信息,我在npm上使用InsomBot来查找,对此软件包进行的唯一修改是更新API URL,因为它们已过时且不正确,我的代码在下面,调试器中的问题图像也在下面,一旦程序到达第105行,它就会跳转因为我似乎没有理由错过第111行,或许我在这里错过了一个简单的缩进错误,或者我没有正确地关闭某些内容,等等。任何帮助都将不胜感激。

i.stack.imgur.com/PnDHU.png(似乎我无法发布2个以上的链接)

var cc = require('config-multipaas'),
  env = require('./env.json'),
  Discord = require('discord.js'),
  Imgur = require("imgur-search"),
  Giphy = require('giphy-wrapper')(env["giphy_key"]),
  urban = require('urban'),
  api = require('lol-api');


//lol
api.configure("API_KEY_HIDDEN");


var server_port = process.env.OPENSHIFT_NODEJS_PORT || 8080
var server_ip_address = process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1'

var config_overrides = {
  PORT: server_port
}
var config = cc(config_overrides);

var mybot = new Discord.Client();
var isearch = new Imgur(env["imgur_key"]);

var termCount = new Map();
var seenURLs = new Map();

mybot.on("message", function (msg) {

  var message = msg.content;

  //keywords
  var giphy = "/giphy ";
  var imgurKey = "/img ";
  var hatter = "hater";
  var def = "/define ";
  var commands = "/commands";
  var lolstatus = "/lolstatus";

  // Reply to direct mentions
  if (msg.isMentioned(mybot.user)) {
    mybot.reply(msg, "right back atcha");
    return;
  }

  // Giphy
  var giphyIndex = message.indexOf(giphy);
  if (giphyIndex > -1) {
    var term = message.substring(giphyIndex + giphy.length).trim().replace(/\s/g, "+");

    var count = termCount.get(term) || 0;
    // console.log("count for term " + term + " is: " + count);
    termCount.set(term,count+1);

    Giphy.search(term, 100, count, function (err, data) {
      if (err) {
        return;
      }

      var items = data.data;
      var index = Math.floor(Math.random() * items.length / 2.0);

      // console.log("found " + items.length + " items for " + term);
      while (index < items.length && seenURLs.get(items[index].url) !== undefined) {
        index++;
      }
      // console.log("using? result number " + index);

      if (items.length > index) {
        var item = items[index];
        seenURLs.set(item.url, 1);
        mybot.sendMessage(msg, item.url);
      } else {
        var apology = "sorry, I couldn't find any giphys for the term: " + term;
        mybot.reply(msg, apology);
      }
    });

    return;
  }
  //Imgur
  var imgurIndex = message.indexOf(imgurKey);
  if (imgurIndex > -1) {
    var term = message.substring(imgurIndex + imgurKey.length).trim().replace(/\s/g, "+");
    // console.log("searching imgur for term: " + term);
    isearch.search(term).then(function(results) {
      // console.log("found results: " + JSON.stringify(results,null,2));
      if (results === undefined || results.length === 0) {
        mybot.reply(msg, "sorry, I couldn't find any imgurs for the term: " + term);
        return;
      }

      var image = results[Math.floor(Math.random() * results.length)];
      mybot.sendMessage(msg, "Here's a description of an image: " + image.title + " " + image.description + " " + image.link);
    });
    return;
  }
  //lol
  var lolIndex = message.indexOf(lolstatus);
  debugger;
  if (lolIndex > -1) {
    debugger;
    var term = message.substring(lolIndex + lolstatus.length).trim().replace(/\s/g, "+");
    debugger;
    api.summonerByName(term, 'na', function(results){
      debugger;
      console.log(results);
    });
  }
  //Define
  var defIndex = message.indexOf(def);
  if (defIndex > -1) {
    var term = message.substring(defIndex + def.length).trim().replace(/\s/g, "+");
    urban(term).first(function(json) {
      if (json !== undefined) {
        // console.log("got json from UD: " + JSON.stringify(json,null,2));
        var definition = "" + json.word + ": " + json.definition + "\nupvotes: " + json.thumbs_up + "   downvotes: " + json.thumbs_down + "\n\nExample: " + json.example;
        mybot.reply(msg, definition);
      }
      else {
        var apology = "sorry, I couldn't find a definition for: " + term;
        mybot.reply(msg, apology);
      }
    });
  }

  //Hatter
  if (message === hatter) {
    mybot.sendMessage(msg, "https://pbs.twimg.com/media/CM5gg9YVAAAVMcn.png");
    return;
  }

  //Commands
  if (message === commands) {
    mybot.sendMessage(msg, "Available commands:[/] giphy | img | define");
    return;
  }

});

mybot.login(env["discord_email"], env["discord_pass"]);

第105行= api.summonerByName(term,&#39; na&#39;,function(results){ 第111行= var defIndex = message.indexOf(def);

1 个答案:

答案 0 :(得分:1)

第111行包含第105行之后的下一个语句,因此它非常有意义。

api.summonerByName()是一种异步方法,因此只有在有结果可用时才会调用其回调(第106和107行),但程序的其余部分将继续运行。