major overhaul of schedule parsing code

This commit is contained in:
mightypanders
2022-04-24 15:51:07 +02:00
parent 22f0117d30
commit 23e3bbc4b1
3 changed files with 100 additions and 36 deletions

View File

@ -2,9 +2,8 @@ import { format } from "date-fns"
import add from "date-fns/add"
import { DateResolvable, Guild, GuildScheduledEvent, GuildScheduledEventCreateOptions } from "discord.js"
import { client } from "../../.."
import { findInScheduleTypes } from "../../helper/typeFind"
import { CustomError, errorCodes, Maybe } from "../../interfaces"
import { RepetitonInfo, supportedSchedule } from "../../types/scheduledEventTypes"
import { RepetitonInfo, Schedule, supportedSchedule } from "../../types/scheduledEventTypes"
export const repetitionMarkerIsFound = (desc: string): boolean => desc.includes('$rep')
export function createEventInGuild(guild: Guild, eventInfo: GuildScheduledEventCreateOptions): Promise<any> {
@ -16,7 +15,8 @@ export function getRepetitonInfo(description: string): RepetitonInfo {
const repetitionString = lines.find(x => x.startsWith('$rep:'))
if (!repetitionString)
throw new CustomError('Cant find repetition string', errorCodes.no_string_present)
const schedule: supportedSchedule = determineSchedule(repetitionString)
const scheduleString = determineScheduleString(repetitionString)
const schedule: Schedule = new Schedule(scheduleString)
const { totalAmount, alreadyOccured } = determineRepetitionCount(repetitionString)
const endDate = determineEndDate(repetitionString)
return {
@ -27,14 +27,13 @@ export function getRepetitonInfo(description: string): RepetitonInfo {
}
}
export function determineSchedule(repetitionLine: string): supportedSchedule {
export function determineScheduleString(repetitionLine: string): supportedSchedule {
const segments = repetitionLine.split(':')
const scheduleSegment = segments[1]
const easilyKnownScheduleName = findInScheduleTypes(scheduleSegment)
if (easilyKnownScheduleName)
return easilyKnownScheduleName
if (scheduleSegment)
return scheduleSegment
else
throw new CustomError('Inferring schedule names is not yet supported', errorCodes.schedule_not_supported)
throw new CustomError('No schedule segment found', errorCodes.no_schedule)
}
export function determineRepetitionCount(description: string): { totalAmount: number; alreadyOccured: number } {
@ -62,30 +61,6 @@ export function addRepetitonStringToEventDescription(oldguildScheduledEvent: str
return newLines.join('\n')
}
export function getNewScheduledStart(oldguildScheduledEvent: GuildScheduledEvent<"SCHEDULED" | "ACTIVE" | "COMPLETED" | "CANCELED">, rInfo: RepetitonInfo): DateResolvable {
const oldDate = oldguildScheduledEvent.scheduledStartAt
let daysToAdd = 0
let monthsToAdd = 0
switch (rInfo.schedule) {
case 'daily':
daysToAdd = 1
break
case 'weekly':
daysToAdd = 7
break
case 'monthly':
monthsToAdd = 1
break
default:
throw new CustomError('No schedule found, cant add days', errorCodes.no_schedule)
}
const duration: Duration = {
days: daysToAdd,
months: monthsToAdd
}
const newDate = add(oldDate, duration)
return newDate
}
function determineEndDate(description: string): Maybe<Date> {
const segments = description.split(':')
if (segments.length === 3) {

View File

@ -1,6 +1,6 @@
import { GuildScheduledEvent, GuildScheduledEventCreateOptions } from "discord.js"
import { RepetitonInfo } from "../../types/scheduledEventTypes"
import { addRepetitonStringToEventDescription, buildNewRepetitionString, createEventInGuild, getNewScheduledStart, getRepetitonInfo } from "./helper"
import { addRepetitonStringToEventDescription, buildNewRepetitionString, createEventInGuild, getRepetitonInfo } from "./helper"
function needsToBeRepeated(rInfo: RepetitonInfo): boolean {
if (rInfo.endDate) {
@ -22,7 +22,7 @@ export function handleRepeatingEvent(oldguildScheduledEvent: GuildScheduledEvent
const newEventOptions: GuildScheduledEventCreateOptions = {
name: oldguildScheduledEvent.name,
description: addRepetitonStringToEventDescription(oldguildScheduledEvent.description, newRepetitonString),
scheduledStartTime: getNewScheduledStart(oldguildScheduledEvent, repetitionInfo),
scheduledStartTime: repetitionInfo.schedule.getNewDate(oldguildScheduledEvent.scheduledStartAt),
privacyLevel: oldguildScheduledEvent.privacyLevel,
entityType: oldguildScheduledEvent.entityType,
channel: oldguildScheduledEvent.channel?.id,