我用've been playing with Andrew Templeton' s 'Bottie'代码(https://github.com/andrew-templeton/bottie)来制作NLP驱动的机器人 .

原始代码是为了与Slack一起工作而构建的,但我希望将其改编为Discord客户端 .

我已经取得了一些进展,但我被困在'ears.js'文件的'.hear'函数部分(如下面的代码片段所示,这是接受消息并将其发送到'NLP引擎'的主文件“) . 如果向它发送'ping',机器人会响应,但没有其他事情发生(机器人是为了讲笑话,进行对话等) . 这是我的代码:

//This is the 'ears.js' file. This allows the bot to log in and 'hear' messages.
"use strict";

var Discord = require('discord.js');
var BotKit = require('botkit');

module.exports = Ears;

var Bot = new Discord.Client({
//var Bot = BotKit.slackbot({
  debug: false,
  storage: undefined
});

function Ears(token) {
  this.scopes = [
    'direct_mention',
    'direct_message',
    'mention',
    'message'
  ];

  // if we haven't defined a token, get the token from the session variable.
  if (Bot.token == undefined) {
    this.token = '...insert token here...';
    }
}

Ears.prototype.listen = function() {
  console.log('TOKEN: ' + this.token);
  this.bot = Bot.login(this.token);

  Bot.on('message', (message) => {

    if(message.content == 'ping') {
      message.channel.sendMessage('pong');
    }

  });
  return this;
}

Ears.prototype.hear = function(pattern, callback) {
  Bot.hears(pattern, this.scopes, callback);
  return this;
};

下面是主程序的代码,'index.js'文件:

//This is the 'index.js' file - the main program.
"use strict";

var fs = require('fs');

var Train = require('./src/train');
var Brain = require('./src/brain');
var Ears = require('./src/ears');
var builtinPhrases = require('./builtins');

var Bottie = {
  Brain: new Brain(),
  Ears: new Ears(process.env.SLACK_TOKEN)
};

var customPhrasesText;
var customPhrases;
try {
  customPhrasesText = fs.readFileSync(__dirname + '/custom-phrases.json').toString();
} catch (err) {
  throw new Error('Uh oh, Bottie could not find the ' +
    'custom-phrases.json file, did you move it?');
}
try {
  customPhrases = JSON.parse(customPhrasesText);
} catch (err) {
  throw new Error('Uh oh, custom-phrases.json was ' +
    'not valid JSON! Fix it, please? :)');
}

console.log('Bottie is learning...');
Bottie.Teach = Bottie.Brain.teach.bind(Bottie.Brain);
eachKey(customPhrases, Bottie.Teach);
eachKey(builtinPhrases, Bottie.Teach);
Bottie.Brain.think();
console.log('Bottie finished learning, time to listen...');
Bottie.Ears
  .listen()
  .hear('TRAINING TIME!!!', function(speech, message) {
    console.log('Delegating to on-the-fly training module...');
    Train(Bottie.Brain, speech, message);
  })
  .hear('.*', function(speech, message) {
    var interpretation = Bottie.Brain.interpret(message.text);
    console.log('Bottie heard: ' + message.text);
    console.log('Bottie interpretation: ', interpretation);
    if (interpretation.guess) {
      console.log('Invoking skill: ' + interpretation.guess);
      Bottie.Brain.invoke(interpretation.guess, interpretation, speech, message);
    } else {
      speech.reply(message, 'Hmm... I don\'t have a response what you said... I\'ll save it and try to learn about it later.');
      // speech.reply(message, '```\n' + JSON.stringify(interpretation) + '\n```');

      // append.write [message.text] ---> to a file
      fs.appendFile('phrase-errors.txt', '\nChannel: ' + message.channel + ' User:'+ message.user + ' - ' + message.text, function (err) {
        console.log('\n\tBrain Err: Appending phrase for review\n\t\t' + message.text + '\n');
        });
    }
  });



function eachKey(object, callback) {
  Object.keys(object).forEach(function(key) {
    callback(key, object[key]);
  });
}

不幸的是,'Bottie'的作者没有回复查询,因此我将问题发布给社区的其他人 . 我是JavaScript的新手,我很感激任何帮助 .