2023-06-09 23:56:45 +02:00
import { GuildMember , GuildScheduledEvent , GuildScheduledEventStatus } from "discord.js" ;
2023-06-04 15:02:03 +02:00
import { v4 as uuid } from "uuid" ;
2023-06-14 22:24:39 +02:00
import { client , jellyfinHandler } from "../.." ;
2023-06-04 16:35:43 +02:00
import { getGuildSpecificTriggerRoleId } from "../helper/roleFilter" ;
2023-06-09 23:56:45 +02:00
import { logger } from "../logger" ;
2023-06-04 15:02:03 +02:00
export const name = 'guildScheduledEventUpdate'
export async function execute ( oldEvent : GuildScheduledEvent , newEvent : GuildScheduledEvent ) {
try {
2023-06-04 16:35:43 +02:00
const requestId = uuid ( )
2023-06-15 21:56:15 +02:00
logger . debug ( ` Got scheduledEvent update. New Event: ${ JSON . stringify ( newEvent , null , 2 ) } ` , { guildId : newEvent.guildId , requestId } )
if ( ! newEvent . guild ) {
logger . error ( "Event has no guild, aborting." , { guildId : newEvent.guildId , requestId } )
return
}
2023-06-04 15:02:03 +02:00
2023-06-09 23:56:45 +02:00
if ( newEvent . description ? . toLowerCase ( ) . includes ( "!wp" ) && [ GuildScheduledEventStatus . Active , GuildScheduledEventStatus . Completed ] . includes ( newEvent . status ) ) {
2023-06-15 21:56:15 +02:00
const roles = getGuildSpecificTriggerRoleId ( ) . map ( ( key , value ) = > value )
const eventMembers = ( await newEvent . fetchSubscribers ( { withMember : true } ) ) . filter ( member = > ! member . member . roles . cache . hasAny ( . . . roles ) ) . map ( ( value ) = > value . member )
const channelMembers = newEvent . channel ? . members . filter ( member = > ! member . roles . cache . hasAny ( . . . roles ) ) . map ( ( value ) = > value )
2023-06-09 23:56:45 +02:00
const allMembers = eventMembers . concat ( channelMembers ? ? [ ] )
const members : GuildMember [ ] = [ ]
2023-06-15 21:56:15 +02:00
for ( const member of allMembers ) {
if ( ! members . find ( x = > x . id == member . id ) )
2023-06-09 23:56:45 +02:00
members . push ( member )
}
2023-06-15 21:56:15 +02:00
2023-06-04 16:35:43 +02:00
if ( newEvent . status === GuildScheduledEventStatus . Active )
createJFUsers ( members , newEvent . name , requestId )
else {
2023-06-15 21:56:15 +02:00
const announcementChannel = await client . getAnnouncementChannelForGuild ( newEvent . guild . id )
if ( ! announcementChannel ) {
logger . error ( "Could not find announcement channel. Aborting" , { guildId : newEvent.guild.id , requestId } )
return
}
const announcements = ( await announcementChannel . messages . fetch ( ) ) . filter ( message = > ! message . pinned )
2023-06-14 22:24:39 +02:00
announcements . forEach ( message = > message . delete ( ) )
2023-06-04 16:35:43 +02:00
members . forEach ( member = > {
2023-06-09 23:56:45 +02:00
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. ` ) )
2023-06-04 16:35:43 +02:00
} )
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-10 14:23:10 +02:00
2023-06-09 23:56:45 +02:00
async function createJFUsers ( members : GuildMember [ ] , 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 = > {
2023-06-09 23:56:45 +02:00
member . createDM ( ) . then ( channel = > channel . send ( ` Hey! Du hast dich für die Watchparty von ${ movieName } angemeldet! Es geht gleich los! ` ) )
jellyfinHandler . upsertUser ( 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 ) {
2023-06-12 20:36:05 +02:00
logger . info ( ` Watchparty ended, deleting tmp users ` , { guildId , requestId } )
2023-06-04 16:35:43 +02:00
jellyfinHandler . purge ( guildId , requestId )
2023-06-04 15:02:03 +02:00
}