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

40 lines
1.6 KiB
TypeScript
Raw Normal View History

import { GuildScheduledEvent, GuildScheduledEventCreateOptions } from "discord.js"
import { RepetitonInfo } from "../../types/scheduledEventTypes"
2022-04-21 19:46:03 +02:00
import { addRepetitonStringToEventDescription, buildNewRepetitionString, createEventInGuild, getNewScheduledStart, getRepetitonInfo } from "./helper"
2022-04-21 19:46:03 +02:00
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: getNewScheduledStart(oldguildScheduledEvent, repetitionInfo),
privacyLevel: oldguildScheduledEvent.privacyLevel,
entityType: oldguildScheduledEvent.entityType,
channel: oldguildScheduledEvent.channel?.id,
reason: 'Repetition'
}
2022-04-21 19:46:03 +02:00
if (!newguildScheduledEvent.guild) throw new Error('No guild on event?')
createEventInGuild(newguildScheduledEvent.guild, newEventOptions)
} catch (err) {
console.error(err)
}
2022-04-21 19:46:03 +02:00
} else {
console.log('Event does not need to be repeated')
}
}