diff --git a/core/commands.js b/core/commands.js index 9c1e285..e2cfcb2 100644 --- a/core/commands.js +++ b/core/commands.js @@ -1,4 +1,4 @@ -const { loader } = require('@bot'); +const { loader, permissions } = require('@bot'); const { client } = require('@bot').client; const { Server, Configuration } = require('@bot').database; @@ -45,15 +45,32 @@ exports.getPrefix = async (serverID) => { exports.getAllCommands = () => registeredCommands; +const getAllowedRoles = (serverPermissions, userRoles, plugin) => { + // eslint-disable-next-line radix + const roles = userRoles.flatMap(x => parseInt(x.id)); + const allowedRoles = []; + for (let x = 0; x < serverPermissions.length; x += 1) { + const perm = serverPermissions[x]; + if (perm.plugin === plugin.discrim && roles.includes(perm.roleID)) { + allowedRoles.push(perm.roleID); + } + } + return allowedRoles; +}; + client.on('message', async (msg) => { const message = msg.content; const serverPrefix = await this.getPrefix(msg.guild.id); + const serverPermissions = await permissions.getServerPermissions(msg.guild.id); for (let i = 0; i < registeredCommands.length; i += 1) { const command = registeredCommands[i]; - const r = new RegExp(`\\${serverPrefix}${command.compiled}`); - const match = message.match(r) ? message.match(r) : []; - const plugin = loader.commandState(command); - if (plugin && (`${serverPrefix}${command.compiled}` === message || match[1])) { + const regex = new RegExp(`\\${serverPrefix}${command.compiled}`); + const match = message.match(regex) ? message.match(regex) : []; + const pluginState = loader.commandState(command); + const plugin = loader.fromCommand(command); + const userRoles = msg.member.roles.array(); + const allowedRoles = getAllowedRoles(serverPermissions, userRoles, plugin); + if (pluginState && (`${serverPrefix}${command.compiled}` === message || match[1]) && (plugin.ignorePermissions || allowedRoles >= 1)) { return command.response(msg, match); } } diff --git a/core/permissions.js b/core/permissions.js index 68c421e..90d2aa8 100644 --- a/core/permissions.js +++ b/core/permissions.js @@ -2,26 +2,28 @@ 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({ + const rolePermissions = await Permission.findOne({ server, roleID, plugin, - }); + }).exec(); + if (rolePermissions) return true; + return false; +}; - if (rolePermissions) { - console.log('Has Permission'); - } +module.exports.getServerPermissions = async (serverID) => { + const server = await Server.findOne({ serverID }).exec(); + const serverPermissions = await Permission.find({ server }).exec(); + return serverPermissions; }; 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; - }); + const permissionExists = await Permission.findOne({ server, roleID, plugin }).exec(); + if (permissionExists) { + return false; + } + const permission = await Permission.create({ server, roleID, plugin }); + if (permission) return true; return false; }; diff --git a/core/plugin-loader.js b/core/plugin-loader.js index a369537..725c3c4 100644 --- a/core/plugin-loader.js +++ b/core/plugin-loader.js @@ -38,3 +38,5 @@ exports.commandState = ({ command }) => { }; exports.getPlugin = discrim => loadedPlugins.filter(m => m.discrim === discrim)[0]; + +exports.fromCommand = ({ command }) => loadedPlugins.filter(m => m.command === command)[0]; diff --git a/plugins/core/help.js b/plugins/core/help.js index 0c0a79e..82d57f3 100644 --- a/plugins/core/help.js +++ b/plugins/core/help.js @@ -32,3 +32,4 @@ commands.register(this.command, '(.*)', 'Shows the help message', async (msg, ex exports.name = 'Help'; exports.state = true; +exports.ignorePermissions = true; diff --git a/plugins/core/install.js b/plugins/core/install.js index ee8bca6..fb79e8e 100644 --- a/plugins/core/install.js +++ b/plugins/core/install.js @@ -1,5 +1,5 @@ const { client } = require('@bot').client; -const { Server, Configuration } = require('@bot').database; +const { Server, Configuration, Permission } = require('@bot').database; exports.command = 'setup'; @@ -19,6 +19,12 @@ client.on('guildCreate', async (guild) => { Configuration.create({ server: newServer, prefix: '!', adminRole }, (configError) => { if (configError) throw configError; }); + Permission.create({ server: newServer, roleID: adminRole, plugin: 'plugins' }, (configError) => { + if (configError) throw configError; + }); + Permission.create({ server: newServer, roleID: adminRole, plugin: 'permissions' }, (configError) => { + if (configError) throw configError; + }); }); } }); diff --git a/plugins/core/permission-helper.js b/plugins/core/permission-helper.js index 15421a9..3ff4180 100644 --- a/plugins/core/permission-helper.js +++ b/plugins/core/permission-helper.js @@ -1,17 +1,32 @@ const { commands, loader, permissions } = require('@bot'); +const { discord } = require('@bot').client; exports.command = 'permissions'; -commands.register(this.command, 'add (.*) (.*)', 'Get a list of plugin discriminators', async (msg, extra) => { +commands.register(this.command, 'add (.*) (.*)', 'Add permission to a plugin to a role', 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); + await permissions.addPermission(msg.guild.id, role.id, extra[1]); + return msg.reply(`Added use permissions to ${role}`); } + return msg.reply('Error, must {@} a role..'); } + return msg.reply('Error plugin does not exist..'); +}); + +commands.register(this.command, '', 'Permissions help', async (msg) => { + const pluginCommands = commands.getCommands('permissions'); + 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}`); + }); + msg.channel.send(em); }); exports.name = 'Permission Helper'; +exports.discrim = 'permissions'; exports.state = true; diff --git a/plugins/core/plugin-helper.js b/plugins/core/plugin-helper.js index ee51338..f261a6e 100644 --- a/plugins/core/plugin-helper.js +++ b/plugins/core/plugin-helper.js @@ -33,4 +33,5 @@ commands.register(this.command, '', 'Get a list of plugin discriminators', (msg) }); exports.name = 'Plugin Loader'; +exports.discrim = 'plugins'; exports.state = true;