2022-04-13 21:38:59 +02:00
|
|
|
import add from "date-fns/add"
|
|
|
|
import { DateResolvable, GuildScheduledEvent } from "discord.js"
|
|
|
|
import { findInScheduleTypes } from "../../helper/typeFind"
|
|
|
|
import { RepetitonInfo, supportedSchedule } from "../../types/scheduledEventTypes"
|
|
|
|
|
|
|
|
export function getRepetitonInfo(description: string): RepetitonInfo {
|
|
|
|
|
|
|
|
const lines = description.split(`\n`)
|
|
|
|
const repetitionString = lines.find(x => x.startsWith('$rep:'))
|
|
|
|
if (!repetitionString)
|
|
|
|
throw new Error('Cant find repetition string')
|
|
|
|
const schedule: supportedSchedule = determineSchedule(repetitionString)
|
|
|
|
const { totalAmount, alreadyOccured } = determineRepetitionCount(repetitionString)
|
|
|
|
return {
|
|
|
|
totalAmount,
|
|
|
|
alreadyOccured,
|
|
|
|
schedule
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function determineSchedule(repetitionLine: string): supportedSchedule {
|
|
|
|
const segments = repetitionLine.split(':')
|
|
|
|
const scheduleSegment = segments[1]
|
|
|
|
const easilyKnownScheduleName = findInScheduleTypes(scheduleSegment)
|
|
|
|
if (easilyKnownScheduleName)
|
|
|
|
return easilyKnownScheduleName
|
|
|
|
else
|
|
|
|
throw new Error('Inferring schedule names is not yet supported')
|
|
|
|
}
|
|
|
|
|
|
|
|
export function determineRepetitionCount(description: string): { totalAmount: number; alreadyOccured: number } {
|
|
|
|
const segments = description.split(':')
|
|
|
|
const amountSegment = segments[2]
|
|
|
|
const amounts = amountSegment.split('/')
|
|
|
|
return { totalAmount: Number(amounts[1]) ?? 0, alreadyOccured: Number(amounts[0]) ?? 0 }
|
|
|
|
}
|
|
|
|
|
|
|
|
export function buildNewRepetitionString(repetitionInfo: RepetitonInfo) {
|
|
|
|
return `$rep:${repetitionInfo.schedule}:${repetitionInfo.alreadyOccured + 1}/${repetitionInfo.totalAmount}`
|
|
|
|
}
|
|
|
|
|
|
|
|
export function addRepetitonStringToEventDescription(oldguildScheduledEvent: string, newRepetitonString: string): string | undefined {
|
|
|
|
const lines = oldguildScheduledEvent.split(`\n`)
|
|
|
|
const repLineIndex = lines.findIndex(x => x.startsWith('$rep:'))
|
|
|
|
const newLines = lines.filter((_, index) => repLineIndex !== index)
|
|
|
|
newLines.push(newRepetitonString)
|
|
|
|
return newLines.join('\n')
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getNewScheduledStart(oldguildScheduledEvent: GuildScheduledEvent<"SCHEDULED" | "ACTIVE" | "COMPLETED" | "CANCELED">, rInfo: RepetitonInfo): DateResolvable {
|
|
|
|
const oldDate = oldguildScheduledEvent.scheduledStartAt
|
|
|
|
let daysToAdd = 0
|
2022-04-13 21:52:32 +02:00
|
|
|
let monthsToAdd = 0
|
2022-04-13 21:38:59 +02:00
|
|
|
switch (rInfo.schedule) {
|
|
|
|
case 'daily':
|
|
|
|
daysToAdd = 1
|
|
|
|
break
|
|
|
|
case 'weekly':
|
|
|
|
daysToAdd = 7
|
|
|
|
break
|
2022-04-13 21:52:32 +02:00
|
|
|
case 'monthly':
|
|
|
|
monthsToAdd = 1
|
|
|
|
break
|
2022-04-13 21:38:59 +02:00
|
|
|
default:
|
|
|
|
throw new Error('No schedule found, cant add days')
|
|
|
|
}
|
|
|
|
const duration: Duration = {
|
2022-04-13 21:52:32 +02:00
|
|
|
days: daysToAdd,
|
|
|
|
months: monthsToAdd
|
2022-04-13 21:38:59 +02:00
|
|
|
}
|
|
|
|
const newDate = add(oldDate, duration)
|
|
|
|
return newDate
|
|
|
|
}
|