Module system, initial creation of Janet

This commit is contained in:
Lewis Dale 2017-02-03 16:28:18 +00:00
parent aedcdfff7c
commit 4c55837193
5 changed files with 147 additions and 0 deletions

10
config.json Normal file
View File

@ -0,0 +1,10 @@
{
"irc": {
"server": "jfc.im",
"port": 6697,
"user": "Janet",
"channels": [
"#team2"
]
}
}

56
janet.js Normal file
View File

@ -0,0 +1,56 @@
const fs = require('fs')
const irc = require('irc')
class Janet {
constructor() {
this.config = this.loadConfig()
this.modules = {
'pm': [],
'join': [],
'message': []
}
this.loadModules()
this.client = new irc.Client(
this.config.irc.server,
this.config.irc.user,
{
channels: this.config.irc.channels
}
)
}
/**
* Load the config file config.json
* @return An object containing the bot configuration
**/
loadConfig() {
return JSON.parse(fs.readFileSync('./config.json', 'utf-8'))
}
/**
* Loads the modules from the modules directory
*/
loadModules() {
let files = fs.readdirSync('modules')
for (let file of files) {
if (file.substr(-3) === '.js') {
let title = file.substr(0, file.length - 3)
if (title !== 'module') {
let module = require('./modules/' + title)
for(let method of module.methods) {
if (method in this.modules) {
this.modules[method].push(module)
}
}
}
}
}
}
}
const janet = new Janet();

21
modules/greet.js Normal file
View File

@ -0,0 +1,21 @@
const Module = require('./module')
class Greet extends Module {
constructor() {
super({
name: 'Greet',
showInHelp: false,
command: 'Hello',
methods: ['join']
})
this.test = {}
}
respond(input) {
return "Hello, " + input
}
}
module.exports = new Greet()

38
modules/module.js Normal file
View File

@ -0,0 +1,38 @@
class Module {
/**
* Construct a new Modules instance
* @param opts: An object containing module information
* @param opts.name: The name of the module
* @param opts.showInHelp: Boolean to determine if a module should be a listed command
* @param opts.command: The command that triggers the action
* @param opts.methods: A list of contact methods where the command is available
*/
constructor(opts = {}) {
let keys = [
'name',
'showInHelp',
'command',
'methods'
]
for(let key of keys) {
if(!(key in opts)) {
throw new TypeError("Key " + key + " is missing from Module options")
}
this[key] = opts[key]
}
}
/**
* Response to the input, triggered by the command
* @return A string response
*/
respond(input) {
throw new TypeError("Function respond has not been implemented. Please override Module.respond()")
}
}
module.exports = Module

22
package.json Normal file
View File

@ -0,0 +1,22 @@
{
"name": "janet",
"version": "0.0.1",
"description": "A NodeJS-based IRC bot",
"main": "janet.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git@lewisdale.me:lewis/Janet.git"
},
"keywords": [
"irc",
"bot"
],
"author": "Lewis Dale <lewis@lewisdale.co.uk>",
"license": "MIT",
"dependencies": {
"irc": "^0.5.2"
}
}