2023-06-17 12:00:14 +02:00
import { format } from "date-fns" ;
2023-06-13 23:15:03 +02:00
import { GuildScheduledEvent , Message , MessageCreateOptions , TextChannel } from "discord.js" ;
2023-06-17 12:00:14 +02:00
import { ScheduledTask } from "node-cron" ;
2023-06-10 14:23:10 +02:00
import { v4 as uuid } from "uuid" ;
2023-06-15 22:33:22 +02:00
import { client , yavinJellyfinHandler } from "../.." ;
2023-06-10 14:23:10 +02:00
import { config } from "../configuration" ;
2023-06-15 21:56:15 +02:00
import { Maybe } from "../interfaces" ;
2023-06-15 22:33:22 +02:00
import { logger } from "../logger" ;
2023-06-22 18:49:01 +02:00
import { SemanticClassificationFormat } from "typescript" ;
2023-06-10 14:23:10 +02:00
export const name = 'guildScheduledEventCreate'
2023-06-10 22:53:11 +02:00
export enum Emotes { "1️ ⃣" , "2️ ⃣" , "3️ ⃣" , "4️ ⃣" , "5️ ⃣" , "6️ ⃣" , "7️ ⃣" , "8️ ⃣" , "9️ ⃣" , "🔟" }
2023-06-22 18:49:01 +02:00
export const NONE_OF_THAT = "❌"
2023-06-10 22:53:11 +02:00
export let task : ScheduledTask | undefined
2023-06-10 14:23:10 +02:00
export async function execute ( event : GuildScheduledEvent ) {
const requestId = uuid ( )
logger . debug ( ` New event created: ${ JSON . stringify ( event , null , 2 ) } ` , { guildId : event.guildId , requestId } )
if ( event . name . toLowerCase ( ) . includes ( "!nextwp" ) ) {
logger . info ( "Event was a placeholder event to start a new watchparty and voting. Creating vote!" , { guildId : event.guildId , requestId } )
logger . debug ( "Renaming event" , { guildId : event.guildId , requestId } )
event . edit ( { name : "Watchparty - Voting offen" } )
2023-06-16 20:15:36 +02:00
const movies = await yavinJellyfinHandler . getRandomMovieNames ( 5 , event . guildId , requestId )
2023-06-10 14:23:10 +02:00
logger . info ( ` Got ${ movies . length } random movies. Creating voting ` , { guildId : event.guildId , requestId } )
2023-06-16 20:15:36 +02:00
logger . debug ( ` Movies: ${ JSON . stringify ( movies ) } ` , { guildId : event.guildId , requestId } )
2023-06-10 14:23:10 +02:00
2023-06-15 21:56:15 +02:00
const announcementChannel : Maybe < TextChannel > = client . getAnnouncementChannelForGuild ( event . guildId )
if ( ! announcementChannel ) {
logger . error ( "Could not find announcement channel. Aborting" , { guildId : event.guildId , requestId } )
return
}
2023-06-12 20:27:54 +02:00
logger . debug ( ` Found channel ${ JSON . stringify ( announcementChannel , null , 2 ) } ` , { guildId : event.guildId , requestId } )
2023-06-10 14:23:10 +02:00
2023-06-14 22:24:39 +02:00
if ( ! event . scheduledStartAt ) {
logger . info ( "EVENT DOES NOT HAVE STARTDATE; CANCELLING" , { guildId : event.guildId , requestId } )
return
}
const date = format ( event . scheduledStartAt , "dd.MM" )
const time = format ( event . scheduledStartAt , "HH:mm" )
2023-06-22 18:49:01 +02:00
let message = ` [Abstimmung] \ n<@& ${ config . bot . announcement_role } > Es gibt eine neue Abstimmung für die nächste Watchparty am ${ date } um ${ time } ! Stimme hierunter für den nächsten Film ab! \ n `
2023-06-10 14:23:10 +02:00
for ( let i = 0 ; i < movies . length ; i ++ ) {
2023-06-16 20:15:36 +02:00
message = message . concat ( Emotes [ i ] ) . concat ( ": " ) . concat ( movies [ i ] ) . concat ( "\n" )
2023-06-10 14:23:10 +02:00
}
2023-06-22 18:49:01 +02:00
message = message . concat ( NONE_OF_THAT ) . concat ( ": Wenn dir nichts davon gefällt." )
2023-06-10 14:23:10 +02:00
2023-06-13 23:15:03 +02:00
const options : MessageCreateOptions = {
allowedMentions : { parse : [ "roles" ] } ,
content : message
}
const sentMessage : Message < true > = await ( await announcementChannel . fetch ( ) ) . send ( options )
2023-06-10 14:23:10 +02:00
for ( let i = 0 ; i < movies . length ; i ++ ) {
2023-06-10 17:27:32 +02:00
sentMessage . react ( Emotes [ i ] )
2023-06-10 14:23:10 +02:00
}
2023-06-22 18:49:01 +02:00
sentMessage . react ( NONE_OF_THAT )
2023-06-10 14:23:10 +02:00
2023-06-10 17:27:32 +02:00
// sentMessage.pin() //todo: uncomment when bot has permission to pin messages. Also update closepoll.ts to only fetch pinned messages
2023-06-10 14:23:10 +02:00
}
2023-06-10 22:53:11 +02:00
}