首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在使用Node.js的松弛机器人tut之后,运行时没有输出?

在使用Node.js的松弛机器人tut之后,运行时没有输出?
EN

Stack Overflow用户
提问于 2016-05-22 03:29:18
回答 1查看 310关注 0票数 0

按照教程使用node.js运行一个简单的松弛机器人。教程在这里:https://scotch.io/tutorials/building-a-slack-bot-with-node-js-and-chuck-norris-super-powers

在终端中运行node bin\bot.js时,光标移到新行,但不打印任何内容,并且继续闪烁,没有任何输出。

我应该期待堆栈跟踪之类的东西吗?我不知道如何在没有任何输出的情况下进行调试。代码如下:

lib/cookiebot.js:

代码语言:javascript
复制
'use strict';

var util = require('util');
var path = require('path');
var fs = require('fs');
var SQLite = require('sqlite3').verbose();
var Bot = require('slackbots');

var CookieBot = function Constructor(settings) {
    this.settings = settings;
    this.settings.name = this.settings.name || 'cookiebot';
  //  this.dbPath = settings.dbPath || path.resolve(process.cwd(), 'data', 'cookiebot.db');

    this.user = null;
  //  this.db = null;
};

// inherits methods and properties from the Bot constructor
util.inherits(CookieBot, Bot);

module.exports = CookieBot;

CookieBot.prototype.run = function () {
    CookieBot.super_.call(this, this.settings);

    this.on('start', this._onStart);
    this.on('message', this._onMessage);
};


CookieBot.prototype._onStart = function () {
    this._loadBotUser();
  //  this._connectDb();
    this._firstRunCheck();
};

CookieBot.prototype._loadBotUser = function () {
    var self = this;
    this.user = this.users.filter(function (user) {
        return user.name === self.name;
    })[0];
};

CookieBot.prototype._connectDb = function () {
    // if (!fs.existsSync(this.dbPath)) {
    //     console.error('Database path ' + '"' + this.dbPath + '" does not exists or it\'s not readable.');
    //     process.exit(1);
    // }
    //
    // this.db = new SQLite.Database(this.dbPath);
};

CookieBot.prototype._firstRunCheck = function () {
    // var self = this;
    // self.db.get('SELECT val FROM info WHERE name = "lastrun" LIMIT 1', function (err, record) {
    //     if (err) {
    //         return console.error('DATABASE ERROR:', err);
    //     }
    //
    //     var currentTime = (new Date()).toJSON();
    //
    //     // this is a first run
    //     if (!record) {
    //         self._welcomeMessage();
    //         return self.db.run('INSERT INTO info(name, val) VALUES("lastrun", ?)', currentTime);
    //     }
    //
    //     // updates with new last running time
    //     self.db.run('UPDATE info SET val = ? WHERE name = "lastrun"', currentTime);
    // });
};

CookieBot.prototype._welcomeMessage = function () {
    this.postMessageToChannel(this.channels[0].name, 'Hi guys, roundhouse-kick anyone?' +
        '\n I can tell jokes, but very honest ones. Just say `Chuck Norris` or `' + this.name + '` to invoke me!',
        {as_user: true});
};

CookieBot.prototype._onMessage = function (message) {
    if (this._isChatMessage(message) &&
        this._isChannelConversation(message) &&
        !this._isFromCookieBot(message) &&
        this._isMentioningCookies(message)
    ) {
        this._replyWithRandomJoke(message);
    }
};

CookieBot.prototype._isChatMessage = function (message) {
    return message.type === 'message' && Boolean(message.text);
};

CookieBot.prototype._isChannelConversation = function (message) {
    return typeof message.channel === 'string' &&
        message.channel[0] === 'C';
};

CookieBot.prototype._isFromNorrisBot = function (message) {
    return message.user === this.user.id;
};

CookieBot.prototype._isMentioningCookies = function (message) {
    return message.text.toLowerCase().indexOf('cookies') > -1 ||
        message.text.toLowerCase().indexOf(this.name) > -1;
};

CookieBot.prototype._replyWithRandomJoke = function (originalMessage) {
    // var self = this;
    // self.db.get('SELECT id, joke FROM jokes ORDER BY used ASC, RANDOM() LIMIT 1', function (err, record) {
    //     if (err) {
    //         return console.error('DATABASE ERROR:', err);
    //     }
    //
    //     var channel = self._getChannelById(originalMessage.channel);
    //     self.postMessageToChannel(channel.name, record.joke, {as_user: true});
    //     self.db.run('UPDATE jokes SET used = used + 1 WHERE id = ?', record.id);
    // });
};

CookieBot.prototype._getChannelById = function (channelId) {
    return this.channels.filter(function (item) {
        return item.id === channelId;
    })[0];
};

bin\bot.js

代码语言:javascript
复制
'use strict';

var CookieBot = require('../lib/cookiebot');

var token = 'xxxxxx';
//var dbPath = process.env.BOT_DB_PATH;
var name = 'cookiebot';

var cookiebot = new CookieBot({
    token: token,
    name: name
  //  dbPath: dbPath,

});

cookiebot.run();

package.json

代码语言:javascript
复制
{
  "name": "cookiebot",
  "version": "1.0.0",
  "description": "",
  "main": "lib/cookiebot.js",
  "scripts": {
    "start": "node bin/bot.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "main": "lib/cookiebot.js",
  "bin": {
    "cookiebot": "bin/bot.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "slackbots": "^0.5.1",
    "sqlite3": "^3.1.4"
  }
}

有什么想法吗?

EN

回答 1

Stack Overflow用户

发布于 2016-05-22 04:27:56

我不能完全确定您的CookieBot从库中继承了什么,但是,您的代码中没有任何内容会向终端打印任何内容。

scotch.io教程说:

如果一切顺利,您将看到您的NorrisBot出现在您的Slack频道中,并且会在那里张贴一条问候消息。试着用他的名字叫他几次,享受他的笑话!

如果您想检查它是否工作,请检查它所连接的空闲通道。或者,在其中添加几行console.log()代码,以了解代码是如何运行的。

代码语言:javascript
复制
    CookieBot.prototype.run = function () {
      console.log( "Initialising Cookie Bot..." );
      CookieBot.super_.call(this, this.settings);

      this.on('start', this._onStart);
      this.on('message', this._onMessage);
    };

  CookieBot.prototype._onMessage = function (message) {
    console.log( "Cookie Bot received message..." );
    ...


    CookieBot.prototype._onStart = function () {
      console.log( "Cookie Bot started..." );
      ...
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37367183

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档