2023-06-04 16:35:43 +02:00
import { GuildScheduledEvent , GuildScheduledEventStatus , Collection , Snowflake , GuildScheduledEventUser } from "discord.js" ;
2023-06-04 15:02:03 +02:00
import { logger } from "../logger" ;
import { jellyfinHandler } from "../.."
import { v4 as uuid } from "uuid" ;
2023-06-04 16:35:43 +02:00
import { getGuildSpecificTriggerRoleId } from "../helper/roleFilter" ;
2023-06-04 15:02:03 +02:00
export const name = 'guildScheduledEventUpdate'
export async function execute ( oldEvent : GuildScheduledEvent , newEvent : GuildScheduledEvent ) {
try {
logger . info ( JSON . stringify ( newEvent , null , 2 ) )
2023-06-04 16:35:43 +02:00
const requestId = uuid ( )
2023-06-04 15:02:03 +02:00
if ( newEvent . description ? . includes ( "!WP" ) && [ GuildScheduledEventStatus . Active , GuildScheduledEventStatus . Completed ] . includes ( newEvent . status ) ) {
2023-06-04 16:35:43 +02:00
const roles = getGuildSpecificTriggerRoleId ( newEvent . guildId ) . map ( ( key , value ) = > value )
const members = ( await newEvent . fetchSubscribers ( { withMember : true } ) ) . filter ( member = > ! member . member . roles . cache . hasAny ( . . . roles ) )
if ( newEvent . status === GuildScheduledEventStatus . Active )
createJFUsers ( members , newEvent . name , requestId )
else {
members . forEach ( member = > {
member . member . createDM ( ) . then ( channel = > channel . send ( ` Die Watchparty ist vorbei, dein Account wurde wieder gelöscht. Wenn du einen permanenten Account haben möchtest, melde dich bei Samantha oder Marukus. ` ) )
} )
deleteJFUsers ( newEvent . guildId , requestId )
}
2023-06-04 15:02:03 +02:00
}
2023-06-04 16:35:43 +02:00
} catch ( error ) {
2023-06-04 15:02:03 +02:00
logger . error ( error )
}
}
2023-06-04 16:35:43 +02:00
async function createJFUsers ( members : Collection < Snowflake , GuildScheduledEventUser < true > > , movieName : string , requestId? : string ) {
2023-06-04 15:02:03 +02:00
logger . info ( ` Creating users for: \ n ${ JSON . stringify ( members , null , 2 ) } ` )
members . forEach ( member = > {
member . member . createDM ( ) . then ( channel = > channel . send ( ` Hey! Du hast dich für die Watchparty von ${ movieName } angemeldet! Es geht gleich los! ` ) )
2023-06-04 16:35:43 +02:00
jellyfinHandler . upsertUser ( member . member , "TEMPORARY" , requestId )
2023-06-04 15:02:03 +02:00
} )
}
2023-06-04 16:35:43 +02:00
async function deleteJFUsers ( guildId : string , requestId? : string ) {
logger . info ( ` Watchparty ended, deleting tmp users ` )
jellyfinHandler . purge ( guildId , requestId )
2023-06-04 15:02:03 +02:00
}