New modules:

* Commands, that lists available commands
* Reload, that allows Janet to reload the module list (therefore loading new modules) via a pm
This commit is contained in:
Lewis Dale 2017-02-04 00:20:02 +00:00
parent a2638c4624
commit deb70524cd
4 changed files with 94 additions and 14 deletions

View File

@ -1,9 +1,6 @@
const fs = require('fs') const fs = require('fs')
const irc = require('irc') const irc = require('irc')
const PubSub = require('pubsub-js')
const MessagePubSub = require('pubsub-js')
class Janet { class Janet {
@ -12,7 +9,8 @@ class Janet {
this.events = { this.events = {
'join': require('pubsub-js'), 'join': require('pubsub-js'),
'message': require('pubsub-js') 'message': require('pubsub-js'),
'pm': require('pubsub-js'),
} }
this.loadModules() this.loadModules()
@ -25,18 +23,31 @@ class Janet {
) )
this.client.addListener('message', (from, to, message) => { this.client.addListener('message', (from, to, message) => {
if(message.substr(0,6) === "Janet,") { console.log("DEBUG: A message was received")
let command = message.replace('Janet, ','').trim() 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.message.publish(command, {
from: from, from: from,
to: to, to: to,
message: message message: message,
variables: variables
}) })
} }
}) })
this.client.addListener('join', (channel, who) => { this.client.addListener('join', (channel, who) => {
PubSub.publish('join', who) console.log("DEBUG: A new user joined")
this.events.join.publish('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
})
}) })
} }
@ -59,6 +70,7 @@ class Janet {
let title = file.substr(0, file.length - 3) let title = file.substr(0, file.length - 3)
if (title !== 'janetmodule') { if (title !== 'janetmodule') {
delete require.cache[require.resolve('./modules/' + title)]
let module = require('./modules/' + title)(this) let module = require('./modules/' + title)(this)
for(let method of module.methods) { for(let method of module.methods) {
@ -70,6 +82,15 @@ class Janet {
} }
} }
/**
* Clear the module list
*/
clearModules() {
for(let key in this.events) {
this.events[key].clearAllSubscriptions()
}
}
/** /**
* Says a phrase across all of the joined channels * Says a phrase across all of the joined channels
*/ */

35
modules/commands.js Normal file
View File

@ -0,0 +1,35 @@
const fs = require('fs')
const JanetModule = require('./janetmodule')
class Commands extends JanetModule {
constructor(client) {
super({
name: 'Commands',
command: 'what commands are available?',
showInHelp: true,
methods: ['message'],
}, client)
}
respond(event, data) {
fs.readdir('modules', (err, files) => {
let commands = [];
for (let file of files) {
let title = file.substr(0, file.length - 3)
if (title !== 'janetmodule') {
let module = require('../modules/' + title)(this.client)
if (module.showInHelp) {
commands.push(module.name)
}
}
}
this.client.say(commands.join(', '))
})
}
}
module.exports = (client) => {
return new Commands(client)
}

24
modules/reload.js Normal file
View File

@ -0,0 +1,24 @@
const JanetModule = require('./janetmodule')
class Reload extends JanetModule {
constructor(client) {
super({
name: 'Reload',
showInHelp: false,
command: 'reload',
methods: ['pm']
}, client)
}
respond(event, data) {
this.client.say("Just checking to see if I've got any new modules...")
this.client.clearModules();
this.client.loadModules();
this.client.say("I'm back now")
}
}
module.exports = (client) => {
return new Reload(client)
}