2022-04-13 21:38:59 +02:00
|
|
|
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-13 21:38:59 +02:00
|
|
|
|
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
|
|
|
|
}
|
2022-04-13 21:38:59 +02:00
|
|
|
|
|
|
|
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)
|
2022-04-13 21:38:59 +02:00
|
|
|
} catch (err) {
|
|
|
|
console.error(err)
|
|
|
|
}
|
2022-04-21 19:46:03 +02:00
|
|
|
} else {
|
|
|
|
console.log('Event does not need to be repeated')
|
2022-04-13 21:38:59 +02:00
|
|
|
}
|
|
|
|
}
|