remove redundant code from perm handler
This commit is contained in:
parent
382cad43ed
commit
6136be3ef2
2 changed files with 18 additions and 19 deletions
|
@ -57,14 +57,14 @@ class MessageHandler {
|
||||||
'The category this command is apart of has been disabled by a server administrator.'
|
'The category this command is apart of has been disabled by a server administrator.'
|
||||||
);
|
);
|
||||||
|
|
||||||
const missingUserPerms = this.client.helpers.checkPermissions(command, message);
|
const missingUserPerms = this.client.helpers.checkPermissions(message.channel, message.author.id, command.userPerms);
|
||||||
if (missingUserPerms) return message.channel.createMessage(
|
if (missingUserPerms) return message.channel.createMessage(
|
||||||
`You can't use this command because you lack these permissions: \`${missingUserPerms.join('`, `')}\``
|
`You can't use this command because you lack these permissions: \`${missingUserPerms.join('`, `')}\``
|
||||||
);
|
);
|
||||||
|
|
||||||
const missingBotPerms = this.client.helpers.checkPermissions(command, message);
|
const missingBotPerms = this.client.helpers.checkPermissions(message.channel, this.client.user.id, command.botPerms);
|
||||||
if (missingBotPerms) return message.channel.createMessage(
|
if (missingBotPerms) return message.channel.createMessage(
|
||||||
`I can't run this command because I lack these permissions: \`${missingUserPerms.join('`, `')}\``
|
`I can't run this command because I lack these permissions: \`${missingBotPerms.join('`, `')}\``
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -80,14 +80,15 @@ class MessageHandler {
|
||||||
|
|
||||||
// Cooldown
|
// Cooldown
|
||||||
if (this.client.cooldowns.get(command.name).has(message.author.id)) {
|
if (this.client.cooldowns.get(command.name).has(message.author.id)) {
|
||||||
const init = this.client.cooldowns.get(command.name).get(message.author.id);
|
const timestamp = this.client.cooldowns.get(command.name).get(message.author.id);
|
||||||
const curr = new Date();
|
const currentTime = Date.now();
|
||||||
const diff = Math.round((curr - init) / 1000);
|
const cooldown = command.cooldown / 1000;
|
||||||
const time = command.cooldown / 1000;
|
const timePassed = Math.floor((currentTime - timestamp) / 1000);
|
||||||
return message.channel.createMessage(`${message.author.mention}, this command is on cooldown! You'll be able to use it again in ${time - diff} seconds.`);
|
return message.channel.createMessage(
|
||||||
|
`⏲️ ${message.author.mention}, you need to wait ${cooldown - timePassed} seconds before using this command again.`
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
this.client.cooldowns.get(command.name).set(message.author.id, new Date());
|
this.client.cooldowns.get(command.name).set(message.author.id, new Date());
|
||||||
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
this.client.cooldowns.get(command.name).delete(message.author.id);
|
this.client.cooldowns.get(command.name).delete(message.author.id);
|
||||||
}, this.client.commands.get(command.name).cooldown);
|
}, this.client.commands.get(command.name).cooldown);
|
||||||
|
|
|
@ -75,20 +75,18 @@ class Helpers {
|
||||||
return role;
|
return role;
|
||||||
}
|
}
|
||||||
|
|
||||||
checkPermissions (command, message) {
|
checkPermissions (channel, user_id, requiredPerms) {
|
||||||
|
const minimumPerms = ['readMessages', 'sendMessages', 'embedLinks'];
|
||||||
|
const pendingPerms = (!requiredPerms) ? minimumPerms : minimumPerms.concat(requiredPerms);
|
||||||
const missingPerms = [];
|
const missingPerms = [];
|
||||||
|
|
||||||
if (message.member.bot) {
|
pendingPerms.forEach(p => {
|
||||||
command.botPerms.forEach(p => {
|
if (!channel.permissionsOf(user_id).has(p)) missingPerms.push(p);
|
||||||
if (!message.channel.permissionsOf(this.client.user.id).has(p)) missingPerms.push(p);
|
|
||||||
});
|
});
|
||||||
} else {
|
|
||||||
command.userPerms.forEach(p => {
|
|
||||||
if (!message.channel.permissionsOf(message.author.id).has(p)) missingPerms.push(p);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if (missingPerms.length > 0) return missingPerms;
|
if (missingPerms.length > 0) return missingPerms;
|
||||||
|
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue