From 8e57d007432671af26d6f61e596ed7054c3a7dce Mon Sep 17 00:00:00 2001 From: Jacob Date: Sat, 19 Oct 2024 11:50:08 +0800 Subject: [PATCH] Multiple Roles Support --- config.example.json | 2 +- src/discord/commands/updateCommand.js | 28 ++++++++++++++++++++------- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/config.example.json b/config.example.json index cf1d0b1d..9b17ed19 100644 --- a/config.example.json +++ b/config.example.json @@ -116,7 +116,7 @@ "ranks": [ { "name": "IN_GAME_RANK_NAME", - "role": "DISCORD_ROLE_ID" + "roles": ["DISCORD_ROLE_ID"] } ] }, diff --git a/src/discord/commands/updateCommand.js b/src/discord/commands/updateCommand.js index 41da8efd..2e206ffc 100644 --- a/src/discord/commands/updateCommand.js +++ b/src/discord/commands/updateCommand.js @@ -9,9 +9,10 @@ const { readFileSync } = require("fs"); module.exports = { name: "update", verificationCommand: true, + requiresBot: true, description: "Update your current roles", - execute: async (interaction, user) => { + execute: async (interaction, user = undefined, unverify = false) => { try { const linkedData = readFileSync("data/linked.json"); if (!linkedData) { @@ -39,7 +40,7 @@ module.exports = { const roles = [ config.verification.verifiedRole, config.verification.guildMemberRole, - ...config.verification.ranks.map((r) => r.role), + ...config.verification.ranks.flatMap((r) => r.roles), ]; for (const role of roles) { @@ -47,14 +48,23 @@ module.exports = { continue; } + if (role === config.verification.unverifiedRole) { + continue; + } + if (interaction.member.roles.cache.has(role)) { await interaction.member.roles.remove(role, "Updated Roles"); } } + if (!interaction.member.roles.cache.has(config.verification.unverifiedRole)) { + await interaction.member.roles.add(config.verification.unverifiedRole, "Updated Roles"); + } + interaction.member.setNickname(null, "Updated Roles"); - throw new HypixelDiscordChatBridgeError("You are not linked to a Minecraft account."); + if (unverify === false) throw new HypixelDiscordChatBridgeError("You are not linked to a Minecraft account."); + return; } if (!interaction.member.roles.cache.has(config.verification.verifiedRole)) { @@ -77,13 +87,17 @@ module.exports = { if (config.verification.ranks.length > 0 && guildMember.rank) { const rank = config.verification.ranks.find((r) => r.name.toLowerCase() == guildMember.rank.toLowerCase()); if (rank) { - for (const role of config.verification.ranks) { - if (interaction.member.roles.cache.has(role.role)) { - await interaction.member.roles.remove(role.role, "Updated Roles"); + for (const rankRole of config.verification.ranks) { + for (const role of rankRole.roles) { + if (interaction.member.roles.cache.has(role)) { + await interaction.member.roles.remove(role, "Updated Roles"); + } } } - await interaction.member.roles.add(rank.role, "Updated Roles"); + for (const role of rank.roles) { + await interaction.member.roles.add(role, "Updated Roles"); + } } } } else {