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
27 changes: 22 additions & 5 deletions core/commands.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { loader } = require('@bot');
const { loader, permissions } = require('@bot');
const { client } = require('@bot').client;
const { Server, Configuration } = require('@bot').database;

Expand Down Expand Up @@ -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);
}
}
Expand Down
28 changes: 15 additions & 13 deletions core/permissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
2 changes: 2 additions & 0 deletions core/plugin-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
1 change: 1 addition & 0 deletions plugins/core/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ commands.register(this.command, '(.*)', 'Shows the help message', async (msg, ex

exports.name = 'Help';
exports.state = true;
exports.ignorePermissions = true;
8 changes: 7 additions & 1 deletion plugins/core/install.js
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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;
});
});
}
});
Expand Down
21 changes: 18 additions & 3 deletions plugins/core/permission-helper.js
Original file line number Diff line number Diff line change
@@ -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;
1 change: 1 addition & 0 deletions plugins/core/plugin-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;