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
This commit is contained in:
Lewis Dale 2017-02-05 19:16:10 +00:00
parent deb70524cd
commit c76f8e27a7
5 changed files with 56 additions and 19 deletions

View File

@ -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()
}
/**

View File

@ -8,7 +8,7 @@ class Commands extends JanetModule {
name: 'Commands',
command: 'what commands are available?',
showInHelp: true,
methods: ['message'],
methods: ['message', 'pm'],
}, client)
}

View File

@ -6,7 +6,7 @@ class Reload extends JanetModule {
super({
name: 'Reload',
showInHelp: false,
command: 'reload',
command: 'load',
methods: ['pm']
}, client)
}

27
modules/video.js Normal file
View File

@ -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)
}

View File

@ -18,6 +18,7 @@
"author": "Lewis Dale <lewis@lewisdale.co.uk>",
"license": "MIT",
"dependencies": {
"compromise": "^7.0.6",
"irc": "^0.5.2",
"pubsub-js": "^1.5.4"
}