Add command permission checks

Level 0: Every user - Public commands
Level 1: Guild owners or members with respective admin role - Elevated guild commands
Level 2: Global admins - Every command including levels below
This commit is contained in:
2022-08-19 19:18:01 +02:00
parent 6a7e3f6647
commit eb4ffae173
8 changed files with 147 additions and 4 deletions

View File

@@ -1,4 +1,5 @@
const { User } = require("../models");
const { User, Guild } = require("../models");
const GeneralUtils = require("./general");
module.exports = {
name: "UserUtils",
@@ -65,5 +66,37 @@ module.exports = {
let newCooldown = new Date(new Date().getTime() + cooldown);
user[`next${cooldownType[0].toUpperCase() + cooldownType.slice(1)}`] = newCooldown;
await user.save();
},
getPermissionLevel: async function(user) {
/* THIS FUNCTION EXPECTS A DISCORD USER INSTANCE!
* Returns the permission level of the user
* 0 - no permissions
* 1 - guild permissions
* 2 - admin permissions
*/
let guild = await Guild.findOne({
where: {
guildId: user.guild.id
}
});
//Global Admin
let adminIDs = await GeneralUtils.getBotProperty("adminIDs");
if (adminIDs.includes(user.id)) {
return 2;
}
//Guild Admin if role is present
if(user._roles.includes(String(guild.adminRoleId))) {
return 1;
}
//or if user is owner
if(user.guild.ownerId === user.id) {
return 1;
}
//Regular User
return 0;
}
}