Skip to content

Commit

Permalink
Merge pull request #21 from ItsJustMeChris/master
Browse files Browse the repository at this point in the history
Admin Role bypass all permissions..
  • Loading branch information
gmemstr authored Feb 18, 2019
2 parents 77827a4 + 36ab3c7 commit 488eabd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
20 changes: 16 additions & 4 deletions core/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ 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 roles = userRoles.flatMap(x => parseInt(x.id, 10));
const allowedRoles = [];
for (let x = 0; x < serverPermissions.length; x += 1) {
const perm = serverPermissions[x];
Expand All @@ -59,21 +58,34 @@ const getAllowedRoles = (serverPermissions, userRoles, plugin) => {
return allowedRoles;
};

const isAdmin = async (serverID, userRoles) => {
const roles = userRoles.flatMap(x => parseInt(x.id, 10));
const server = await Server.findOne({ serverID }).exec();
const { adminRole } = await Configuration.findOne({
server,
}).select({
adminRole: 1,
_id: 0,
}).exec();
return roles.includes(adminRole);
};

client.on('message', async (msg) => {
const message = msg.content;
if (!msg.guild && msg.author.id !== client.user.id) return msg.reply('I do not work in DMs');
if (msg.author.id === client.user.id) return false;
const serverPrefix = await this.getPrefix(msg.guild.id);
const serverPermissions = await permissions.getServerPermissions(msg.guild.id);
const userRoles = msg.member.roles.array();
const adminState = await isAdmin(msg.guild.id, userRoles);
for (let i = 0; i < registeredCommands.length; i += 1) {
const command = registeredCommands[i];
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.length >= 1)) {
if (pluginState && (`${serverPrefix}${command.compiled}` === message || match[1]) && (plugin.ignorePermissions || adminState || allowedRoles.length >= 1)) {
return command.response(msg, match);
}
}
Expand Down
5 changes: 3 additions & 2 deletions plugins/music.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ commands.register(this.command, '', 'music', 'Get the music help', async (msg) =
msg.channel.send(em);
});

commands.register(this.command, 'play (.*)', 'music play <song-name>/<youtube-url>', 'Change the bots game', async (msg, extra) => {
commands.register(this.command, 'play (.*)', 'music play <song-name>/<youtube-url>', 'Change the bots game', (msg, extra) => {
const queue = serverQueue(msg.guild.id);
if (msg.member.voiceChannel) {
msg.member.voiceChannel.join().then((con) => {
Expand All @@ -50,12 +50,13 @@ commands.register(this.command, 'play (.*)', 'music play <song-name>/<youtube-ur
queue.push({ title, url, requester });
const stream = ytdl(`http://www.youtube.com${url}`, { filter: 'audioonly' });
con.playStream(stream);
msg.channel.send(em);
return msg.channel.send(em);
});
});
} else {
return msg.reply('You need to be in a voice channel');
}
return true;
});

exports.name = 'Music';
Expand Down

0 comments on commit 488eabd

Please sign in to comment.