node-event-bot/server/handler/repeatingEvents/repeatingEvents.controller.ts

29 lines
1.4 KiB
TypeScript

import { GuildScheduledEvent, GuildScheduledEventCreateOptions } from "discord.js"
import { RepetitonInfo } from "../../types/scheduledEventTypes"
import { addRepetitonStringToEventDescription, buildNewRepetitionString, getNewScheduledStart, getRepetitonInfo } from "./helper"
const needsToBeRepeated = (rInfo: RepetitonInfo): boolean => rInfo.alreadyOccured < rInfo.totalAmount
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 newRepetitonString = buildNewRepetitionString(repetitionInfo)
const newEventOptions: GuildScheduledEventCreateOptions = {
name: oldguildScheduledEvent.name,
description: addRepetitonStringToEventDescription(oldguildScheduledEvent.description, newRepetitonString),
scheduledStartTime: getNewScheduledStart(oldguildScheduledEvent, repetitionInfo),
privacyLevel: oldguildScheduledEvent.privacyLevel,
entityType: oldguildScheduledEvent.entityType,
channel: oldguildScheduledEvent.channel?.id,
reason: 'Repetition'
}
newguildScheduledEvent.guild?.scheduledEvents.create(newEventOptions)
} catch (err) {
console.error(err)
}
}
}