Modifications to module system:

* Renamed Module to JanetModule to reduce chance of clashes with NodeJS module
* Changes to event dispatcher - different PubSub instances for each type of event
* Fixed issues with arguments and scoping when modules triggered
This commit is contained in:
Lewis Dale committed 2017-02-03 23:00:23 +00:00
1 parent 4c55837193
commit 7b48f9bc21
6 files changed
+92 -27

No files matched your search

+16 -10
View File
@@ -1,21 +1,27 @@
const Module = require('./module')
const JanetModule = require('./janetmodule')
class Greet extends Module {
/**
* A simple greet module that says a friendly hello when somebody joins
* @author Lewis Dale
*/
class Greet extends JanetModule {
constructor() {
constructor(client) {
super({
name: 'Greet',
showInHelp: false,
command: 'Hello',
command: 'join',
methods: ['join']
})
this.test = {}
}, client)
}
respond(input) {
return "Hello, " + input
respond(event, who) {
if (who !== "Janet") {
this.client.say("Hello, " + who)
}
}
}
module.exports = new Greet()
module.exports = (client) => {
return new Greet(client)
}
+9 -4
View File
@@ -1,4 +1,4 @@
class Module {
class JanetModule {
/**
* Construct a new Modules instance
@@ -8,7 +8,7 @@ class Module {
* @param opts.command: The command that triggers the action
* @param opts.methods: A list of contact methods where the command is available
*/
constructor(opts = {}) {
constructor(opts = {}, client) {
let keys = [
'name',
'showInHelp',
@@ -22,6 +22,11 @@ class Module {
}
this[key] = opts[key]
if (client === undefined || client === null) {
throw new TypeError("Client object was not passed to the module")
}
this.client = client
}
}
@@ -30,9 +35,9 @@ class Module {
* Response to the input, triggered by the command
* @return A string response
*/
respond(input) {
respond() {
throw new TypeError("Function respond has not been implemented. Please override Module.respond()")
}
}
module.exports = Module
module.exports = JanetModule
+25
View File
@@ -0,0 +1,25 @@
const JanetModule = require('./janetmodule')
/**
* A simple command to ask Janet the current time
* @author Lewis Dale
*/
class Time extends JanetModule {
constructor(client) {
super({
name: 'Time',
showInHelp: true,
command: 'what time is it?',
methods: ['message']
}, client)
}
respond(event, data) {
let date = new Date()
this.client.say(data.from + ", it is currently " + date.toTimeString())
}
}
module.exports = (client) => {
return new Time(client)
}