import { GuildScheduledEvent, GuildScheduledEventCreateOptions } from "discord.js" import { RepetitonInfo } from "../../types/scheduledEventTypes" import { addRepetitonStringToEventDescription, buildNewRepetitionString, createEventInGuild, getRepetitonInfo } from "./helper" function needsToBeRepeated(rInfo: RepetitonInfo): boolean { if (rInfo.endDate) { if (new Date() < rInfo.endDate) return true } else { return rInfo.alreadyOccured < rInfo.totalAmount } return false } export function handleRepeatingEvent(oldguildScheduledEvent: GuildScheduledEvent, newguildScheduledEvent: GuildScheduledEvent) { if (!oldguildScheduledEvent.description) throw new Error('Event has no description -> cant handle this') const repetitionInfo = getRepetitonInfo(oldguildScheduledEvent.description) if (needsToBeRepeated(repetitionInfo)) { try { const newDate = repetitionInfo.schedule.getNewDate(oldguildScheduledEvent.scheduledStartAt) if (repetitionInfo.endDate && (repetitionInfo.endDate <= newDate)) { console.log(`Wont repeat: EndDate: ${repetitionInfo.endDate} RepetitionDate: ${newDate}`) return } const newRepetitonString = buildNewRepetitionString(repetitionInfo) const newEventOptions: GuildScheduledEventCreateOptions = { name: oldguildScheduledEvent.name, description: addRepetitonStringToEventDescription(oldguildScheduledEvent.description, newRepetitonString), scheduledStartTime: newDate, privacyLevel: oldguildScheduledEvent.privacyLevel, entityType: oldguildScheduledEvent.entityType, channel: oldguildScheduledEvent.channel?.id, reason: 'Repetition' } if (!newguildScheduledEvent.guild) throw new Error('No guild on event?') createEventInGuild(newguildScheduledEvent.guild, newEventOptions) } catch (err) { console.error(err) } } else { console.log('Event does not need to be repeated') } }