diff --git a/.gitignore b/.gitignore index e1da6ae..6c7a51c 100644 --- a/.gitignore +++ b/.gitignore @@ -33,7 +33,8 @@ bower_components build/Release # Dependency directories -node_modules/ +node_modules/* +!node_modules/@bot jspm_packages/ # TypeScript v1 declaration files diff --git a/app.js b/app.js index 25e5703..c2c42be 100644 --- a/app.js +++ b/app.js @@ -1,7 +1,13 @@ // Load configuration. require('dotenv').config(); -const LOADER = require('./core/module-loader'); -const logger = require('./core/logger'); -LOADER.loadModules(); -logger.log('store', 'Bot Started', new Date()); +//load @bot hooks +require('./core/bootstrap'); + +const { loader } = require('@bot'); +const { log } = require('@bot').logger; + +log('notify', `BOTKEY: [${process.env.DISCORD_KEY}]`); +log('notify', `MONGO: [${process.env.MONGODB_URL}]`); + +loader.loadModules(); diff --git a/core/bootstrap.js b/core/bootstrap.js new file mode 100644 index 0000000..9897c04 --- /dev/null +++ b/core/bootstrap.js @@ -0,0 +1,16 @@ +const core = require('@bot'); + +const logger = require('./logger'); +core.hook('logger', logger); + +const client = require('./client'); +core.hook('client', client); + +const loader = require('./module-loader'); +core.hook('loader', loader); + +const commands = require('./commands'); +core.hook('commands', commands); + +const database = require('./database'); +core.hook('database', database); diff --git a/core/client.js b/core/client.js index 7acee28..7bae339 100644 --- a/core/client.js +++ b/core/client.js @@ -1,12 +1,13 @@ const Discord = require('discord.js'); +const { log } = require('@bot').logger; + const client = new Discord.Client(); -const LOGGER = require('./logger'); -console.log(process.env.DISCORD_KEY); + const key = process.env.DISCORD_KEY; client.on('ready', () => { client.user.setActivity("cowsay"); - LOGGER.log('notify', 'Bot Started'); + log('notify', 'Bot Started'); }); client.login(key); diff --git a/core/commands.js b/core/commands.js index a58b64b..307966c 100644 --- a/core/commands.js +++ b/core/commands.js @@ -1,6 +1,5 @@ -const discord = require('./client'); -const LOADER = require('./module-loader'); -const database = require('./database'); +const { loader } = require('@bot'); +const { discord, client } = require('@bot').client; const registeredCommands = []; let prefix = '!'; @@ -42,13 +41,28 @@ exports.getCommands = command => registeredCommands.filter(e => e.command === co exports.getAllCommands = () => registeredCommands; -discord.client.on('message', (msg) => { +exports.getModuleHelp = (discrim) => { + const module = loader.getModule(discrim); + if (module) { + const em = new discord.RichEmbed(); + const moduleCommands = this.getCommands(module.command); + em.setTitle(`${module.name} | Help`); + moduleCommands.forEach(c => { + em.addField(`${this.getPrefix()}${c.command} ${c.params}`, `${c.description}`) + }); + return em; + } else { + return `Are you sure that \`${discrim}\` is a valid module descriminator`; + } +} + +client.on('message', (msg) => { const message = msg.content; for (let i = 0; i < registeredCommands.length; i += 1) { const command = registeredCommands[i]; const r = new RegExp(`${prefix}${command.compiled}`); const match = message.match(r) ? message.match(r) : []; - if (LOADER.getState(command) && (`${prefix}${command.compiled}` === message || match[1])) { + if (loader.getState(command) && (`${prefix}${command.compiled}` === message || match[1])) { return command.response(msg, match); } } diff --git a/core/database.js b/core/database.js index 0cbee3a..130d4aa 100644 --- a/core/database.js +++ b/core/database.js @@ -1,38 +1,40 @@ -const {log} = require('./logger'); +const { log } = require('@bot').logger; const mongoose = require('mongoose'); + const Schema = mongoose.Schema; +const database = mongoose.connection; mongoose.connect(process.env.MONGODB_URL, { useNewUrlParser: true }); -const database = mongoose.connection; database.on('error', console.error.bind(console, 'connection error:')); -database.once('open', function() { - log('notify', 'MongoDB Connected'); + +database.once('open', function () { + log('notify', 'MongoDB Connected'); }); const Server = mongoose.model('Server', { - id: Number, - name: String + id: Number, + name: String }); const User = mongoose.model('User', { - server: { type: Schema.Types.ObjectId, ref: 'Server' }, - name: String, - descrim: Number, - warnings: Number, - isAdmin: Boolean + server: { type: Schema.Types.ObjectId, ref: 'Server' }, + name: String, + descrim: Number, + warnings: Number, + isAdmin: Boolean }); const Configuration = mongoose.model('Configuration', { - server: { type: Schema.Types.ObjectId, ref: 'Server' }, - prefix: String, - adminRole: String, - help: String + server: { type: Schema.Types.ObjectId, ref: 'Server' }, + prefix: String, + adminRole: String, + help: String }); module.exports = { - server: Server, - user: User, - configuration: Configuration, - database: database + server: Server, + user: User, + configuration: Configuration, + database: database }; \ No newline at end of file diff --git a/core/module-loader.js b/core/module-loader.js index 58672c5..18e493a 100644 --- a/core/module-loader.js +++ b/core/module-loader.js @@ -1,8 +1,7 @@ const fs = require('fs'); const PATH = require('path'); -const logger = require('./logger'); -const { discord } = require('./client'); -const commands = require('./commands'); + +const { logger } = require('@bot'); const loadedModules = []; @@ -33,18 +32,3 @@ exports.getModules = () => loadedModules; exports.getState = ({ command }) => loadedModules.filter(m => m.command === command && m.state === true).length > 0; exports.getModule = (discrim) => loadedModules.filter(m => m.discrim === discrim)[0]; - -exports.getModuleHelp = (discrim) => { - const module = this.getModule(discrim); - if (module) { - const em = new discord.RichEmbed(); - const moduleCommands = commands.getCommands(module.command); - em.setTitle(`${module.name} | Help`); - moduleCommands.forEach(c => { - em.addField(`${commands.getPrefix()}${c.command} ${c.params}`, `${c.description}`) - }); - return em; - } else { - return `Are you sure that \`${discrim}\` is a valid module descriminator`; - } -} \ No newline at end of file diff --git a/modules/customize.js b/modules/customize.js index 0e7ebc1..f2bee2b 100644 --- a/modules/customize.js +++ b/modules/customize.js @@ -1,11 +1,10 @@ -const commands = require('../core/commands'); -const { client } = require('../core/client'); -const LOADER = require('../core/module-loader'); +const { commands } = require('@bot'); +const { client } = require('@bot').client; exports.command = 'customize'; commands.register(this.command, '', 'Customize Help', (msg) => { - msg.channel.send(LOADER.getModuleHelp('customize')); + msg.channel.send(commands.getModuleHelp('customize')); }); commands.register(this.command, 'game (.*)', 'Change the bots game', (msg, extra) => { diff --git a/modules/help.js b/modules/help.js index 7868505..e5942e9 100644 --- a/modules/help.js +++ b/modules/help.js @@ -1,11 +1,10 @@ -const commands = require('../core/commands'); -const { discord } = require('../core/client'); -const LOADER = require('../core/module-loader'); +const { commands, loader } = require('@bot'); +const { discord } = require('@bot').client; exports.command = 'help'; commands.register(this.command, '', 'Shows the help message', (msg) => { - const modules = LOADER.getModules(); + const modules = loader.getModules(); const em = new discord.RichEmbed(); modules.forEach(m => { if (m.discrim) { @@ -16,7 +15,7 @@ commands.register(this.command, '', 'Shows the help message', (msg) => { }); commands.register(this.command, '(.*)', 'Shows the help message', (msg, extra) => { - msg.channel.send(LOADER.getModuleHelp(extra[1])); + msg.channel.send(commands.getModuleHelp(extra[1])); }); exports.name = 'Help'; diff --git a/modules/moderation/user-management.js b/modules/moderation/user-management.js index 4212600..796abc6 100644 --- a/modules/moderation/user-management.js +++ b/modules/moderation/user-management.js @@ -1,5 +1,5 @@ -const commands = require('../../core/commands'); -const { discord } = require('../../core/client'); +const { commands } = require('@bot'); +const { discord } = require('@bot').client; exports.command = 'mod'; diff --git a/modules/modulehelper.js b/modules/modulehelper.js index 8ef9cc5..2e21d90 100644 --- a/modules/modulehelper.js +++ b/modules/modulehelper.js @@ -1,11 +1,10 @@ -const LOADER = require('../core/module-loader'); -const commands = require('../core/commands'); -const { discord } = require('../core/client'); +const { commands, loader } = require('@bot'); +const { discord } = require('@bot').client; exports.command = 'modules'; commands.register(this.command, 'toggle (.*)', 'Toggle a module.', (msg, extra) => { - const module = LOADER.getModule(extra[1]); + const module = loader.getModule(extra[1]); if (module) { module.toggle(); msg.reply(`Alright, module was toggled: ${module.state}`); @@ -15,7 +14,7 @@ commands.register(this.command, 'toggle (.*)', 'Toggle a module.', (msg, extra) }); commands.register(this.command, 'status (.*)', 'Check the status of a module', (msg, extra) => { - const module = LOADER.getModule(extra[1]); + const module = loader.getModule(extra[1]); if (module) { msg.reply(`Module status: ${module.state}`); } else { @@ -24,7 +23,7 @@ commands.register(this.command, 'status (.*)', 'Check the status of a module', ( }); commands.register(this.command, '', 'Get a list of module discriminators', (msg) => { - const modules = LOADER.getModules(); + const modules = loader.getModules(); const em = new discord.RichEmbed(); em.title = "Modules"; modules.forEach(m => { diff --git a/modules/ping-pong.js b/modules/ping-pong.js index 9e92f62..38b0faa 100644 --- a/modules/ping-pong.js +++ b/modules/ping-pong.js @@ -1,4 +1,4 @@ -const commands = require('../core/commands'); +const { commands } = require('@bot'); exports.command = 'ping'; diff --git a/node_modules/@bot/index.js b/node_modules/@bot/index.js new file mode 100644 index 0000000..8e027f7 --- /dev/null +++ b/node_modules/@bot/index.js @@ -0,0 +1,7 @@ +const coreExports = {}; + +coreExports.hook = (moduleName, module) => { + coreExports[moduleName] = module; +} + +module.exports = coreExports; \ No newline at end of file diff --git a/node_modules/@bot/package.json b/node_modules/@bot/package.json new file mode 100644 index 0000000..ef2c470 --- /dev/null +++ b/node_modules/@bot/package.json @@ -0,0 +1,11 @@ +{ + "name": "bot", + "version": "1.0.0", + "description": "Wrapper around bot imports. ", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "ItsJustMeChris", + "license": "MIT" +}