From 4c72633a5db314d0b858c7cb441acebc03adc45e Mon Sep 17 00:00:00 2001 From: steev Date: Sat, 3 Dec 2022 22:08:18 +0100 Subject: [PATCH] added untested role user management code --- Commons/Services/Roles.js | 101 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 98 insertions(+), 3 deletions(-) diff --git a/Commons/Services/Roles.js b/Commons/Services/Roles.js index 9f6cffb..773901a 100644 --- a/Commons/Services/Roles.js +++ b/Commons/Services/Roles.js @@ -1,13 +1,108 @@ const discord = require("discord.js") /** assigns a given role to a given user - @param {discord.User} userVal the target user + @param {discord.GuildMember} userVal the target user @param {string} roleName name of role to assign a user @param {discord.Client} bot the bots instance */ -const AddRole = async (userVal, roleName, bot) => { +const AddRole = async (userVal, roleName, bot, reason) => { + + // error handling + if (userVal == undefined) { throw "error user was undefined" } + if (roleName == undefined) { throw "error roleNames was undefined" } + if (bot == undefined) { throw "error bot was undefined" } + if (typeof(userVal) != "Discord.GuildMember") { throw "error userVal is not of type Discord.GuildMember" } + if (typeof(roleName) != "string") { throw "error roleNames is not of type string" } + if (typeof(bot) != "Discord.Client") { throw "error roleNames is not of type Discord.Client" } + + // fetching required data const guild = bot.guilds.cache.first() const role = guild.roles.find("name", roleName) - + if (role == undefined) { + throw `error couldnt find role ${role}` + } + userVal.roles.add(role, reason) +} + +/** + assigns a given roles to a given user + @param {discord.GuildMember} userVal the target user + @param {string} roleName name of role to assign a user + @param {discord.Client} bot the bots instance +*/ +const AddRoles = async (userVal, roleNames, bot, reason) => { + + // error handling + if (userVal == undefined) { throw "error user was undefined" } + if (roleNames == undefined) { throw "error roleNames was undefined" } + if (bot == undefined) { throw "error bot was undefined" } + if (typeof(userVal) != "Discord.GuildMember") { throw "error userVal is not of type Discord.GuildMember" } + if (typeof(roleNames) != "object") { throw "error roleNames is not of type object" } + if (typeof(bot) != "Discord.Client") { throw "error roleNames is not of type Discord.Client" } + + // fetching required data + const guild = bot.guilds.cache.first() + + roleNames.forEach(r => { + const role = guild.roles.find("name", r) + if (role == undefined) { + throw `error couldnt find role ${role}` + } + userVal.roles.add(role, reason) + }) +} + +/** +revokes a given role to a given user +@param {discord.GuildMember} userVal the target user +@param {string} roleName name of role to assign a user +@param {discord.Client} bot the bots instance +*/ +const RemoveRole = async (userVal, roleName, bot, reason) => { + + // error handling + if (userVal == undefined) { throw "error user was undefined" } + if (roleName == undefined) { throw "error roleNames was undefined" } + if (bot == undefined) { throw "error bot was undefined" } + if (typeof(userVal) != "Discord.GuildMember") { throw "error userVal is not of type Discord.GuildMember" } + if (typeof(roleName) != "string") { throw "error roleNames is not of type string" } + if (typeof(bot) != "Discord.Client") { throw "error roleNames is not of type Discord.Client" } + + // fetching required data + const guild = bot.guilds.cache.first() + const role = guild.roles.find("name", roleName) + + if (role == undefined) { + throw `error couldnt find role ${role}` + } + userVal.roles.remove(role, reason) +} + +/** +revokes a given roles to a given user +@param {discord.GuildMember} userVal the target user +@param {string} roleName name of role to assign a user +@param {discord.Client} bot the bots instance +*/ +const RemoveRoles = async (userVal, roleNames, bot, reason) => { + + // error handling + if (userVal == undefined) { throw "error user was undefined" } + if (roleNames == undefined) { throw "error roleNames was undefined" } + if (bot == undefined) { throw "error bot was undefined" } + if (typeof(userVal) != "Discord.GuildMember") { throw "error userVal is not of type Discord.GuildMember" } + if (typeof(roleNames) != "object") { throw "error roleNames is not of type object" } + if (typeof(bot) != "Discord.Client") { throw "error roleNames is not of type Discord.Client" } + + // fetching required data + const guild = bot.guilds.cache.first() + + roleNames.forEach(r => { + const role = guild.roles.find("name", r) + if (role == undefined) { + throw `error couldnt find role ${role}` + } + userVal.roles.remove(role, reason) + }) } \ No newline at end of file