3

I've been playing with Andrew Templeton's 'Bottie' code (https://github.com/andrew-templeton/bottie) to make NLP-driven bots.

The original code was built to work with Slack but I wish to adapt it to the Discord client instead.

I've made some progress, but am stuck at '.hear' function part of the 'ears.js' file (as shown in the code snippet below, this is the main file that accepts messages and sends it to the 'NLP engine'). The bot responds if a 'ping' is sent to it, but nothing else happens (the bot was built to tell jokes, have conversations,etc.). Here's my code:

//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;
};

Below is the code for the main program, the 'index.js' file:

//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]);
  });
}

Unfortunately, the author of 'Bottie' did not respond to queries, hence I'm posting the question here to the rest of the community. I'm fairly new to JavaScript and would appreciate any help on this.

Code Monkey
  • 800
  • 1
  • 9
  • 27

0 Answers0