Skip to content

Commit e57c1c8

Browse files
Merge pull request #15 from ItsJustMeChris/master
Added Permissions
2 parents dccb087 + 9fc3fc9 commit e57c1c8

File tree

7 files changed

+66
-22
lines changed

7 files changed

+66
-22
lines changed

core/commands.js

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { loader } = require('@bot');
1+
const { loader, permissions } = require('@bot');
22
const { client } = require('@bot').client;
33
const { Server, Configuration } = require('@bot').database;
44

@@ -45,15 +45,32 @@ exports.getPrefix = async (serverID) => {
4545

4646
exports.getAllCommands = () => registeredCommands;
4747

48+
const getAllowedRoles = (serverPermissions, userRoles, plugin) => {
49+
// eslint-disable-next-line radix
50+
const roles = userRoles.flatMap(x => parseInt(x.id));
51+
const allowedRoles = [];
52+
for (let x = 0; x < serverPermissions.length; x += 1) {
53+
const perm = serverPermissions[x];
54+
if (perm.plugin === plugin.discrim && roles.includes(perm.roleID)) {
55+
allowedRoles.push(perm.roleID);
56+
}
57+
}
58+
return allowedRoles;
59+
};
60+
4861
client.on('message', async (msg) => {
4962
const message = msg.content;
5063
const serverPrefix = await this.getPrefix(msg.guild.id);
64+
const serverPermissions = await permissions.getServerPermissions(msg.guild.id);
5165
for (let i = 0; i < registeredCommands.length; i += 1) {
5266
const command = registeredCommands[i];
53-
const r = new RegExp(`\\${serverPrefix}${command.compiled}`);
54-
const match = message.match(r) ? message.match(r) : [];
55-
const plugin = loader.commandState(command);
56-
if (plugin && (`${serverPrefix}${command.compiled}` === message || match[1])) {
67+
const regex = new RegExp(`\\${serverPrefix}${command.compiled}`);
68+
const match = message.match(regex) ? message.match(regex) : [];
69+
const pluginState = loader.commandState(command);
70+
const plugin = loader.fromCommand(command);
71+
const userRoles = msg.member.roles.array();
72+
const allowedRoles = getAllowedRoles(serverPermissions, userRoles, plugin);
73+
if (pluginState && (`${serverPrefix}${command.compiled}` === message || match[1]) && (plugin.ignorePermissions || allowedRoles >= 1)) {
5774
return command.response(msg, match);
5875
}
5976
}

core/permissions.js

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,28 @@ const { Server, Permission } = require('@bot').database;
22

33
module.exports.checkPermissions = async (serverID, roleID, plugin) => {
44
const server = await Server.findOne({ serverID }).exec();
5-
const rolePermissions = await Permission.find({
5+
const rolePermissions = await Permission.findOne({
66
server,
77
roleID,
88
plugin,
9-
});
9+
}).exec();
10+
if (rolePermissions) return true;
11+
return false;
12+
};
1013

11-
if (rolePermissions) {
12-
console.log('Has Permission');
13-
}
14+
module.exports.getServerPermissions = async (serverID) => {
15+
const server = await Server.findOne({ serverID }).exec();
16+
const serverPermissions = await Permission.find({ server }).exec();
17+
return serverPermissions;
1418
};
1519

1620
module.exports.addPermission = async (serverID, roleID, plugin) => {
1721
const server = await Server.findOne({ serverID }).exec();
18-
Permission.create({
19-
server,
20-
roleID,
21-
plugin,
22-
}, (err, permission) => {
23-
if (err) throw err;
24-
return permission;
25-
});
22+
const permissionExists = await Permission.findOne({ server, roleID, plugin }).exec();
23+
if (permissionExists) {
24+
return false;
25+
}
26+
const permission = await Permission.create({ server, roleID, plugin });
27+
if (permission) return true;
2628
return false;
2729
};

core/plugin-loader.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,5 @@ exports.commandState = ({ command }) => {
3838
};
3939

4040
exports.getPlugin = discrim => loadedPlugins.filter(m => m.discrim === discrim)[0];
41+
42+
exports.fromCommand = ({ command }) => loadedPlugins.filter(m => m.command === command)[0];

plugins/core/help.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,4 @@ commands.register(this.command, '(.*)', 'Shows the help message', async (msg, ex
3232

3333
exports.name = 'Help';
3434
exports.state = true;
35+
exports.ignorePermissions = true;

plugins/core/install.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const { client } = require('@bot').client;
2-
const { Server, Configuration } = require('@bot').database;
2+
const { Server, Configuration, Permission } = require('@bot').database;
33

44
exports.command = 'setup';
55

@@ -19,6 +19,12 @@ client.on('guildCreate', async (guild) => {
1919
Configuration.create({ server: newServer, prefix: '!', adminRole }, (configError) => {
2020
if (configError) throw configError;
2121
});
22+
Permission.create({ server: newServer, roleID: adminRole, plugin: 'plugins' }, (configError) => {
23+
if (configError) throw configError;
24+
});
25+
Permission.create({ server: newServer, roleID: adminRole, plugin: 'permissions' }, (configError) => {
26+
if (configError) throw configError;
27+
});
2228
});
2329
}
2430
});

plugins/core/permission-helper.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,32 @@
11
const { commands, loader, permissions } = require('@bot');
2+
const { discord } = require('@bot').client;
23

34
exports.command = 'permissions';
45

5-
commands.register(this.command, 'add (.*) (.*)', 'Get a list of plugin discriminators', async (msg, extra) => {
6+
commands.register(this.command, 'add (.*) (.*)', 'Add permission to a plugin to a role', async (msg, extra) => {
67
const plugin = loader.getPlugin(extra[1]);
78
if (plugin) {
89
const role = msg.mentions.roles.first();
910
if (role) {
10-
const p = await permissions.addPermission(msg.guild.id, role.id, extra[1]);
11-
console.log(p);
11+
await permissions.addPermission(msg.guild.id, role.id, extra[1]);
12+
return msg.reply(`Added use permissions to ${role}`);
1213
}
14+
return msg.reply('Error, must {@} a role..');
1315
}
16+
return msg.reply('Error plugin does not exist..');
17+
});
18+
19+
commands.register(this.command, '', 'Permissions help', async (msg) => {
20+
const pluginCommands = commands.getCommands('permissions');
21+
const em = new discord.RichEmbed();
22+
const prefix = await commands.getPrefix();
23+
em.setTitle('Customize | Help');
24+
pluginCommands.forEach((c) => {
25+
em.addField(`${prefix}${c.command} \`${c.params}\``, `${c.description}`);
26+
});
27+
msg.channel.send(em);
1428
});
1529

1630
exports.name = 'Permission Helper';
31+
exports.discrim = 'permissions';
1732
exports.state = true;

plugins/core/plugin-helper.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@ commands.register(this.command, '', 'Get a list of plugin discriminators', (msg)
3333
});
3434

3535
exports.name = 'Plugin Loader';
36+
exports.discrim = 'plugins';
3637
exports.state = true;

0 commit comments

Comments
 (0)