diff --git a/config.json b/config.json new file mode 100644 index 0000000..6193005 --- /dev/null +++ b/config.json @@ -0,0 +1,10 @@ +{ + "irc": { + "server": "jfc.im", + "port": 6697, + "user": "Janet", + "channels": [ + "#team2" + ] + } +} diff --git a/janet.js b/janet.js new file mode 100644 index 0000000..352971a --- /dev/null +++ b/janet.js @@ -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(); diff --git a/modules/greet.js b/modules/greet.js new file mode 100644 index 0000000..6268088 --- /dev/null +++ b/modules/greet.js @@ -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() diff --git a/modules/module.js b/modules/module.js new file mode 100644 index 0000000..60663b5 --- /dev/null +++ b/modules/module.js @@ -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 diff --git a/package.json b/package.json new file mode 100644 index 0000000..18377e5 --- /dev/null +++ b/package.json @@ -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 ", + "license": "MIT", + "dependencies": { + "irc": "^0.5.2" + } +}