2023-06-09 23:56:45 +02:00
import { VoiceState } from "discord.js" ;
2023-06-04 15:02:03 +02:00
import { v4 as uuid } from "uuid" ;
2023-06-09 23:56:45 +02:00
import { jellyfinHandler } from "../.." ;
import { UserUpsertResult } from "../jellyfin/handler" ;
import { logger } from "../logger" ;
2023-06-04 15:02:03 +02:00
export const name = 'voiceStateUpdate'
export async function execute ( oldState : VoiceState , newState : VoiceState ) {
try {
2023-06-09 23:56:45 +02:00
logger . info ( JSON . stringify ( newState , null , 2 ) )
2023-06-04 15:02:03 +02:00
//ignore events like mute/unmute
if ( newState . channel ? . id === oldState . channel ? . id ) {
2023-06-09 23:56:45 +02:00
logger . info ( "Not handling VoiceState event because channelid of old and new was the same (i.e. mute/unmute event)" )
2023-06-04 15:02:03 +02:00
return
}
const scheduledEvents = ( await newState . guild . scheduledEvents . fetch ( ) )
2023-06-15 21:56:15 +02:00
. filter ( ( key ) = > key . description ? . toLowerCase ( ) . includes ( "!wp" ) && key . isActive ( ) )
. map ( ( key ) = > key )
2023-06-04 15:02:03 +02:00
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 ;
} )
} )
2023-06-09 23:56:45 +02:00
if ( userFound ) {
logger . info ( ` Not handling VoiceState event because user was already subscribed and got an account from there. User: ${ JSON . stringify ( newState . member , null , 2 ) } ` )
2023-06-04 15:02:03 +02:00
return
2023-06-09 23:56:45 +02:00
}
2023-06-04 15:02:03 +02:00
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!" )
2023-06-09 23:56:45 +02:00
const result = await jellyfinHandler . upsertUser ( newState . member , "TEMPORARY" , uuid ( ) )
if ( result === UserUpsertResult . created ) {
newState . member . createDM ( ) . then ( channel = > channel . send ( ` Hey! Du bist unserer Watchparty beigetreten, ich hab dir gerade die Zugangsdaten für den Mediaserver geschickt! ` ) )
} else {
newState . member . createDM ( ) . then ( channel = > channel . send ( ` Hey! Du bist unserer Watchparty beigetreten aber du hast bereits einen Account. Falls du ein neues Passwort brauchst nutze /reset_passwort! ` ) )
}
2023-06-04 15:02:03 +02:00
} else {
logger . error ( "WTF? Expected Member?? When doing things" )
}
2023-06-09 23:56:45 +02:00
} else {
logger . info ( "VoiceState channelId was not the id of any channel with events" )
2023-06-04 15:02:03 +02:00
}
} catch ( error ) {
logger . error ( error )
}
}