From 68e1a27563079d6ecadcd3811bcd617dfda272f9 Mon Sep 17 00:00:00 2001 From: Erik Zuuring Date: Wed, 6 Mar 2024 11:11:58 +0200 Subject: [PATCH 1/2] Role management This adds a `role-management` to Valkyrie which allows you to call the function on another script to add/remove roles based on the following criteria: - guildID (Discord server ID) - memberID (Discord user ID) - roleID (Discord role ID) - action (add or remove) --- discord-scripts/role-management.ts | 34 ++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 discord-scripts/role-management.ts diff --git a/discord-scripts/role-management.ts b/discord-scripts/role-management.ts new file mode 100644 index 00000000..70b346ce --- /dev/null +++ b/discord-scripts/role-management.ts @@ -0,0 +1,34 @@ +import { Client } from "discord.js" + +export default async function manageRole( + discordClient: Client, + guildId: string, + memberId: string, + roleId: string, + action: "add" | "remove", +): Promise { + if (!discordClient) { + throw new Error("Discord client is not initialized.") + } + + const guild = await discordClient.guilds.fetch(guildId) + if (!guild) throw new Error("Guild not found.") + + const member = await guild.members.fetch(memberId) + if (!member) throw new Error("Member not found.") + + const role = await guild.roles.fetch(roleId) + if (!role) throw new Error("Role not found.") + + if (action === "add") { + if (member.roles.cache.has(roleId)) { + return + } + await member.roles.add(role) + } else if (action === "remove") { + if (!member.roles.cache.has(roleId)) { + return + } + await member.roles.remove(role) + } +} \ No newline at end of file From 4efa1af2087b1710cb52f6dafffd75aa3b68ed1d Mon Sep 17 00:00:00 2001 From: Erik Zuuring Date: Wed, 6 Mar 2024 12:41:21 +0200 Subject: [PATCH 2/2] Lint fix Prettier fix --- discord-scripts/role-management.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/discord-scripts/role-management.ts b/discord-scripts/role-management.ts index 70b346ce..18ab9f6a 100644 --- a/discord-scripts/role-management.ts +++ b/discord-scripts/role-management.ts @@ -31,4 +31,4 @@ export default async function manageRole( } await member.roles.remove(role) } -} \ No newline at end of file +}