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,18 +1,16 @@
const fs = require('fs')
const irc = require('irc')
const PubSub = require('pubsub-js')
const MessagePubSub = require('pubsub-js')
class Janet {
constructor() {
this.config = this.loadConfig()
this.events = {
'join': require('pubsub-js'),
'message': require('pubsub-js')
'message': require('pubsub-js'),
'pm': require('pubsub-js'),
}
this.loadModules()
@ -25,18 +23,31 @@ class Janet {
)
this.client.addListener('message', (from, to, message) => {
if(message.substr(0,6) === "Janet,") {
let command = message.replace('Janet, ','').trim()
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, {
from: from,
to: to,
message: message
message: message,
variables: variables
})
}
})
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,17 +70,27 @@ class Janet {
let title = file.substr(0, file.length - 3)
if (title !== 'janetmodule') {
delete require.cache[require.resolve('./modules/' + title)]
let module = require('./modules/' + title)(this)
for(let method of module.methods) {
this.events[method].subscribe(module.command, (...args) => {
this.events[method].subscribe(module.command, (...args) => {
module.respond(...args) })
}
}
}
}
}
/**
* Clear the module list
*/
clearModules() {
for(let key in this.events) {
this.events[key].clearAllSubscriptions()
}
}
/**
* 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)
}

View File

@ -15,7 +15,7 @@ class JanetModule {
'command',
'methods'
]
for(let key of keys) {
if(!(key in opts)) {
throw new TypeError("Key " + key + " is missing from Module options")
@ -30,7 +30,7 @@ class JanetModule {
}
}
/**
* Response to the input, triggered by the command
* @return A string response

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