From c76f8e27a7b080384724ce13c258c9aa08451ed7 Mon Sep 17 00:00:00 2001 From: Lewis Dale Date: Sun, 5 Feb 2017 19:16:10 +0000 Subject: [PATCH] Janet now uses the compromise library to process commands, meaning she has NLP! Commands now only have to contain the right phrases, e.g. "play a video" and "play the video" both trigger the same command, regardless of the rest of the text Simplified the events dispatcher because shared memory references were causing events to be dispatched multiple times --- janet.js | 43 ++++++++++++++++++++++++++----------------- modules/commands.js | 2 +- modules/reload.js | 2 +- modules/video.js | 27 +++++++++++++++++++++++++++ package.json | 1 + 5 files changed, 56 insertions(+), 19 deletions(-) create mode 100644 modules/video.js diff --git a/janet.js b/janet.js index 2b3c2d5..68565f4 100644 --- a/janet.js +++ b/janet.js @@ -1,17 +1,15 @@ const fs = require('fs') +const PubSub = require('pubsub-js') const irc = require('irc') +const nlp = require('compromise') class Janet { constructor() { this.config = this.loadConfig() - - this.events = { - 'join': require('pubsub-js'), - 'message': require('pubsub-js'), - 'pm': require('pubsub-js'), - } + + this.events = PubSub this.loadModules() this.client = new irc.Client( @@ -22,13 +20,17 @@ class Janet { } ) - this.client.addListener('message', (from, to, message) => { + this.client.addListener('message', (nick, to, text, message) => { + if(to === "Janet") { //Stops pm's being processed twice + return + } + console.log("DEBUG: A message was received") let commandEnd = message.indexOf('?') if(message.substr(0,6) === "Janet," && commandEnd !== -1) { let command = message.substr(0,commandEnd + 1).replace('Janet, ','').trim() let variables = message.substr(commandEnd + 1, message.length).split(',') - this.events.message.publish(command, { + this.events.publish('message: ' + command, { from: from, to: to, message: message, @@ -39,15 +41,24 @@ class Janet { this.client.addListener('join', (channel, who) => { console.log("DEBUG: A new user joined") - this.events.join.publish('join', who) + this.events.publish('join: join', who) }) this.client.addListener('pm', (nick, text, message) => { console.log("DEBUG: A pm was received") - this.events.pm.publish(text, { - from: nick, - message: message - }) + let q = nlp(text) + let matches = q.match('#Verb . #Noun').list + + for(let match of matches) { + let verb = match.terms[0] + let noun = match.terms[2] + this.events.publish('pm: ' + verb.text, { + function: noun.text, + from: nick, + text: text, + }) + + } }) } @@ -74,7 +85,7 @@ class Janet { let module = require('./modules/' + title)(this) for(let method of module.methods) { - this.events[method].subscribe(module.command, (...args) => { + this.events.subscribe(method + ': ' + module.command, (...args) => { module.respond(...args) }) } } @@ -86,9 +97,7 @@ class Janet { * Clear the module list */ clearModules() { - for(let key in this.events) { - this.events[key].clearAllSubscriptions() - } + this.events.clearAllSubscriptions() } /** diff --git a/modules/commands.js b/modules/commands.js index e0f2bdb..1b3f258 100644 --- a/modules/commands.js +++ b/modules/commands.js @@ -8,7 +8,7 @@ class Commands extends JanetModule { name: 'Commands', command: 'what commands are available?', showInHelp: true, - methods: ['message'], + methods: ['message', 'pm'], }, client) } diff --git a/modules/reload.js b/modules/reload.js index 2c66c0c..0509171 100644 --- a/modules/reload.js +++ b/modules/reload.js @@ -6,7 +6,7 @@ class Reload extends JanetModule { super({ name: 'Reload', showInHelp: false, - command: 'reload', + command: 'load', methods: ['pm'] }, client) } diff --git a/modules/video.js b/modules/video.js new file mode 100644 index 0000000..7657f4d --- /dev/null +++ b/modules/video.js @@ -0,0 +1,27 @@ +const nlp = require('compromise') + +const JanetModule = require('./janetmodule') + +/** + * Command to get Janet to search YouTube for a video + * @author Lewis Dale + */ +class Video extends JanetModule { + constructor(client) { + super({ + name: 'Video', + showInHelp: true, + command: 'play', + methods: ['pm', 'message'] + }, client) + } + + respond(evt, data) { + let msg = nlp(data.text) + console.log(msg) + } +} + +module.exports = (client) => { + return new Video(client) +} diff --git a/package.json b/package.json index 58d8cbc..87fe225 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "author": "Lewis Dale ", "license": "MIT", "dependencies": { + "compromise": "^7.0.6", "irc": "^0.5.2", "pubsub-js": "^1.5.4" }