jellyfin-discord-bot/server/events/handlePermJFAccountByRole.ts

31 lines
1.1 KiB
TypeScript

import { Collection, GuildMember } from "discord.js"
import { filterRolesFromMemberUpdate, getGuildSpecificTriggerRoleId } from "../helper/roleFilter"
import { ChangedRoles, PermissionLevel } from "../interfaces"
import { jellyfinHandler } from "../.."
import { v4 as uuid } from "uuid"
export const name = 'guildMemberUpdate'
export async function execute(oldMember: GuildMember, newMember: GuildMember) {
try {
const requestId = uuid()
const changedRoles: ChangedRoles = filterRolesFromMemberUpdate(oldMember, newMember)
const triggerRoleIds: Collection<string, PermissionLevel> = getGuildSpecificTriggerRoleId()
triggerRoleIds.forEach((level, key) => {
const addedRoleMatches = changedRoles.addedRoles.find(aRole => aRole.id === key)
if (addedRoleMatches) {
jellyfinHandler.upsertUser(newMember, level, requestId)
}
const removedRoleMatches = changedRoles.removedRoles.find(rRole => rRole.id === key)
if (removedRoleMatches) {
jellyfinHandler.removeUser(newMember, level, requestId)
}
})
} catch (error) {
console.error(error)
}
}