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

31 lines
1.1 KiB
TypeScript
Raw Permalink Normal View History

import { Collection, GuildMember } from "discord.js"
import { filterRolesFromMemberUpdate, getGuildSpecificTriggerRoleId } from "../helper/roleFilter"
import { ChangedRoles, PermissionLevel } from "../interfaces"
import { jellyfinHandler } from "../.."
2023-06-04 01:04:07 +02:00
import { v4 as uuid } from "uuid"
2023-04-15 22:06:35 +02:00
export const name = 'guildMemberUpdate'
export async function execute(oldMember: GuildMember, newMember: GuildMember) {
2023-06-24 21:09:56 +02:00
try {
const requestId = uuid()
const changedRoles: ChangedRoles = filterRolesFromMemberUpdate(oldMember, newMember)
const triggerRoleIds: Collection<string, PermissionLevel> = getGuildSpecificTriggerRoleId()
2023-06-24 21:09:56 +02:00
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)
}
2023-04-15 22:06:35 +02:00
}