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

40 lines
1.6 KiB
TypeScript

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 newRepetitonString = buildNewRepetitionString(repetitionInfo)
const newEventOptions: GuildScheduledEventCreateOptions = {
name: oldguildScheduledEvent.name,
description: addRepetitonStringToEventDescription(oldguildScheduledEvent.description, newRepetitonString),
scheduledStartTime: repetitionInfo.schedule.getNewDate(oldguildScheduledEvent.scheduledStartAt),
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')
}
}