使botkit适应Discord

时间:2017-12-24 03:04:54

标签: javascript bots slack discord botkit

我一直在和Andrew Templeton' Bottie'代码(https://github.com/andrew-templeton/bottie)来制作NLP驱动的机器人。

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

我已经取得了一些进展,但我被困在了#he;' “ear.js”的功能部分'文件(如下面的代码片段所示,这是接受消息并将其发送到' NLP引擎的主文件)。机器人会响应“ping”和“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,并希望得到任何帮助。

0 个答案:

没有答案