jellyfin-discord-bot/server/events/guildMemberUpdate.ts
2023-05-04 23:38:34 +02:00

35 lines
1.2 KiB
TypeScript

import { Collection, GuildMember } from "discord.js"
import { filterRolesFromMemberUpdate } from "../helper/roleFilter"
import { ChangedRoles, PermissionLevel } from "../interfaces"
import { jellyfinHandler } from "../.."
export const name = 'guildMemberUpdate'
export async function execute(oldMember: GuildMember, newMember: GuildMember) {
try {
const changedRoles: ChangedRoles = filterRolesFromMemberUpdate(oldMember, newMember)
const triggerRoleIds: Collection<string, PermissionLevel> = 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<string, PermissionLevel> {
const outVal = new Collection<string, PermissionLevel>()
outVal.set('1096819983889215659', "VIEWER")
outVal.set('1097990848613986526', "ADMIN")
return outVal
}