2023-06-04 15:02:03 +02:00
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! ` ) )
2023-06-04 16:35:43 +02:00
jellyfinHandler . upsertUser ( newState . member , "TEMPORARY" , uuid ( ) )
2023-06-04 15:02:03 +02:00
} else {
logger . error ( "WTF? Expected Member?? When doing things" )
}
}
} catch ( error ) {
logger . error ( error )
}
}