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

36 lines
1.3 KiB
TypeScript
Raw Normal View History

import { Collection, GuildMember } from "discord.js"
import { filterRolesFromMemberUpdate } 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) {
try {
2023-06-04 01:04:07 +02:00
const requestId = uuid()
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) {
2023-06-04 01:04:07 +02:00
jellyfinHandler.upsertUser(newMember, level, requestId)
}
const removedRoleMatches = changedRoles.removedRoles.find(rRole => rRole.id === key)
if (removedRoleMatches) {
2023-06-04 01:04:07 +02:00
jellyfinHandler.removeUser(newMember, requestId)
}
})
2023-04-15 22:06:35 +02:00
} 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
}