Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Load configuration.
require('dotenv').config();

//load @bot hooks.
// load @bot hooks.
require('./core/bootstrap');

// Load HTTP server for redirecting to auth URL.
Expand All @@ -16,7 +16,7 @@ log('notify', `MONGO: [${process.env.MONGODB_URL}]`);
loader.init();

// Simple HTTP server to redirect to Discord bot auth URL.
const [PORT] = process.env.PORT;
const { PORT } = process.env;
const server = http.createServer((request, response) => {
response.writeHead(302, { Location: process.env.DISCORD_REDIRECT_URL });
response.end('Redirecting...');
Expand Down
4 changes: 4 additions & 0 deletions core/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ const database = require('./database');

core.hook('database', database);

const permissions = require('./permissions');

core.hook('permissions', permissions);

const commands = require('./commands');

core.hook('commands', commands);
6 changes: 3 additions & 3 deletions core/database.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
const { log } = require('@bot').logger;
const mongoose = require('mongoose');

const [Schema] = mongoose.Schema;
const { Schema } = mongoose;
const database = mongoose.connection;

mongoose.connect(process.env.MONGODB_URL, { useNewUrlParser: true });

// eslint-disable-next-line no-console
database.on('error', console.error.bind(console, 'connection error:'));

database.once('open', () => {
Expand Down Expand Up @@ -40,9 +41,8 @@ const ModeratorRole = mongoose.model('ModeratorRole', {

const Permission = mongoose.model('Permission', {
server: { type: Schema.Types.ObjectId, ref: 'Server' },
module: String,
plugin: String,
roleID: Number,
permission: String,
});

module.exports = {
Expand Down
1 change: 1 addition & 0 deletions core/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const path = require('path');
const date = new Date();

exports.log = (level, ...data) => {
// eslint-disable-next-line no-console
console.log(`[${level}] ${data}`);
if (level === 'store') {
fs.appendFile(path.join(__dirname, 'logs', `${date.getMonth() + 1}-${date.getDate()}-${date.getFullYear()}.txt`), `${data}\n`, (err) => {
Expand Down
27 changes: 27 additions & 0 deletions core/permissions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const { Server, Permission } = require('@bot').database;

module.exports.checkPermissions = async (serverID, roleID, plugin) => {
const server = await Server.findOne({ serverID }).exec();
const rolePermissions = await Permission.find({
server,
roleID,
plugin,
});

if (rolePermissions) {
console.log('Has Permission');
}
};

module.exports.addPermission = async (serverID, roleID, plugin) => {
const server = await Server.findOne({ serverID }).exec();
Permission.create({
server,
roleID,
plugin,
}, (err, permission) => {
if (err) throw err;
return permission;
});
return false;
};
8 changes: 6 additions & 2 deletions core/plugin-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ const load = (path) => {
if (stat.isDirectory()) {
load(fPath);
} else {
const mod = require(fPath);
// eslint-disable-next-line import/no-dynamic-require
const mod = require(fPath); // eslint-disable-line global-require
loadedPlugins.push(mod);
logger.log('notify', `Plugin: [${mod.name || 'ERROR'}] Version: [${mod.version || '0'}] Loaded.`);
}
Expand All @@ -31,6 +32,9 @@ exports.init = () => {

exports.getPlugins = () => loadedPlugins;

exports.commandState = ({ command }) => loadedPlugins.filter(m => m.command === command && m.state === true).length > 0;
// eslint-disable-next-line arrow-body-style
exports.commandState = ({ command }) => {
return loadedPlugins.filter(m => m.command === command && m.state === true).length > 0;
};

exports.getPlugin = discrim => loadedPlugins.filter(m => m.discrim === discrim)[0];
1 change: 1 addition & 0 deletions plugins/core/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const { Server, Configuration } = require('@bot').database;

exports.command = 'setup';

// eslint-disable-next-line arrow-body-style
const roleReducer = (lastRole, curRole) => {
return lastRole.permissions < curRole.permissions ? curRole : lastRole;
};
Expand Down
17 changes: 17 additions & 0 deletions plugins/core/permission-helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const { commands, loader, permissions } = require('@bot');

exports.command = 'permissions';

commands.register(this.command, 'add (.*) (.*)', 'Get a list of plugin discriminators', async (msg, extra) => {
const plugin = loader.getPlugin(extra[1]);
if (plugin) {
const role = msg.mentions.roles.first();
if (role) {
const p = await permissions.addPermission(msg.guild.id, role.id, extra[1]);
console.log(p);
}
}
});

exports.name = 'Permission Helper';
exports.state = true;
2 changes: 1 addition & 1 deletion plugins/core/plugin-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ commands.register(this.command, '', 'Get a list of plugin discriminators', (msg)
msg.reply(em);
});

exports.name = 'Module Loader';
exports.name = 'Plugin Loader';
exports.state = true;
6 changes: 3 additions & 3 deletions plugins/customize.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ commands.register(this.command, '', 'Customize Help', async (msg) => {
const pluginCommands = commands.getCommands('customize');
const em = new discord.RichEmbed();
const prefix = await commands.getPrefix();
em.setTitle(`Customize | Help`);
pluginCommands.forEach(c => {
em.addField(`${prefix}${c.command} ${c.params}`, `${c.description}`)
em.setTitle('Customize | Help');
pluginCommands.forEach((c) => {
em.addField(`${prefix}${c.command} ${c.params}`, `${c.description}`);
});
msg.channel.send(em);
});
Expand Down