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

49 lines
2.0 KiB
TypeScript

import { GuildScheduledEvent, GuildScheduledEventStatus, Collection, Snowflake, GuildScheduledEventUser, User, VoiceState } from "discord.js";
import { logger } from "../logger";
import { jellyfinHandler } from "../.."
import { v4 as uuid } from "uuid";
export const name = 'voiceStateUpdate'
export async function execute(oldState: VoiceState, newState: VoiceState) {
try {
//ignore events like mute/unmute
if(newState.channel?.id === oldState.channel?.id) {
return
}
const scheduledEvents = (await newState.guild.scheduledEvents.fetch())
.filter((key, value) => key.description?.includes("!WP") && key.isActive())
.map((key, value) => key)
const scheduledEventUsers = (await Promise.all(scheduledEvents.map(event => event.fetchSubscribers({withMember: true}))))
//Dont handle users, that are already subscribed to the event. We only want to handle unsubscribed users here
let userFound = false;
scheduledEventUsers.forEach(collection => {
collection.each(key => {
logger.info(JSON.stringify(key, null, 2))
if(key.member.user.id === newState.member?.user.id)
userFound = true;
})
})
if(userFound)
return
if (scheduledEvents.find(event => event.channelId === newState.channelId)) {
if(newState.member){
logger.info("YO! Da ist jemand dem Channel mit dem Event beigetreten, ich kümmer mich mal um nen Account!")
newState.member.createDM().then(channel => channel.send(`Hey! Du bist unserer Watchparty beigetreten, ich leg dir mal nen Account an, damit du mitschauen kannst!`))
jellyfinHandler.upsertUser(newState.member, "TEMPORARY", uuid())
} else {
logger.error("WTF? Expected Member?? When doing things")
}
}
}catch(error){
logger.error(error)
}
}