From 4ff8c83cdf02c367c3953210185ecd35fbb9f320 Mon Sep 17 00:00:00 2001 From: mightypanders Date: Tue, 18 Apr 2023 23:45:36 +0200 Subject: [PATCH] WIP: handle update of user, determine changed roles and hand over to JF --- server/events/guildMemberUpdate.ts | 35 ++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/server/events/guildMemberUpdate.ts b/server/events/guildMemberUpdate.ts index 0f86346..af23d50 100644 --- a/server/events/guildMemberUpdate.ts +++ b/server/events/guildMemberUpdate.ts @@ -1,12 +1,39 @@ -import { GuildMember } from "discord.js" +import { Collection, GuildMember } from "discord.js" +import { filterRolesFromMemberUpdate } from "../helper/roleFilter" +import { ChangedRoles, PermissionLevel } from "../interfaces" +import ar from "date-fns/locale/ar" +import { jellyfinHandler } from "../.." export const name = 'guildMemberUpdate' export async function execute(oldMember: GuildMember, newMember: GuildMember) { try { - console.log(`User ${oldMember.displayName} has been updated`) - console.log(`OldRoles: ${JSON.stringify(oldMember.roles.cache.map(x => x.name))}`) - console.log(`NewRoles: ${JSON.stringify(newMember.roles.cache.map(x => x.name))}`) + + const changedRoles: ChangedRoles = filterRolesFromMemberUpdate(oldMember, newMember) + const triggerRoleIds: Collection = getGuildSpecificTriggerRoleId(oldMember.guild.id) + + triggerRoleIds.forEach((level, key) => { + const addedRoleMatches = changedRoles.addedRoles.find(aRole => aRole.id === key) + if (addedRoleMatches) { + jellyfinHandler.upsertUser(newMember, level) + } + const removedRoleMatches = changedRoles.removedRoles.find(rRole => rRole.id === key) + if (removedRoleMatches) { + jellyfinHandler.removeUser(newMember) + } + + + }) + + } catch (error) { console.error(error) } } + +function getGuildSpecificTriggerRoleId(id: string): Collection { + const outVal = new Collection() + outVal.set('1096819983889215659', "VIEWER") + outVal.set('1097990848613986526', "ADMIN") + return outVal +} +