major overhaul of schedule parsing code
This commit is contained in:
parent
22f0117d30
commit
23e3bbc4b1
|
@ -2,9 +2,8 @@ import { format } from "date-fns"
|
||||||
import add from "date-fns/add"
|
import add from "date-fns/add"
|
||||||
import { DateResolvable, Guild, GuildScheduledEvent, GuildScheduledEventCreateOptions } from "discord.js"
|
import { DateResolvable, Guild, GuildScheduledEvent, GuildScheduledEventCreateOptions } from "discord.js"
|
||||||
import { client } from "../../.."
|
import { client } from "../../.."
|
||||||
import { findInScheduleTypes } from "../../helper/typeFind"
|
|
||||||
import { CustomError, errorCodes, Maybe } from "../../interfaces"
|
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 const repetitionMarkerIsFound = (desc: string): boolean => desc.includes('$rep')
|
||||||
export function createEventInGuild(guild: Guild, eventInfo: GuildScheduledEventCreateOptions): Promise<any> {
|
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:'))
|
const repetitionString = lines.find(x => x.startsWith('$rep:'))
|
||||||
if (!repetitionString)
|
if (!repetitionString)
|
||||||
throw new CustomError('Cant find repetition string', errorCodes.no_string_present)
|
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 { totalAmount, alreadyOccured } = determineRepetitionCount(repetitionString)
|
||||||
const endDate = determineEndDate(repetitionString)
|
const endDate = determineEndDate(repetitionString)
|
||||||
return {
|
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 segments = repetitionLine.split(':')
|
||||||
const scheduleSegment = segments[1]
|
const scheduleSegment = segments[1]
|
||||||
const easilyKnownScheduleName = findInScheduleTypes(scheduleSegment)
|
if (scheduleSegment)
|
||||||
if (easilyKnownScheduleName)
|
return scheduleSegment
|
||||||
return easilyKnownScheduleName
|
|
||||||
else
|
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 } {
|
export function determineRepetitionCount(description: string): { totalAmount: number; alreadyOccured: number } {
|
||||||
|
@ -62,30 +61,6 @@ export function addRepetitonStringToEventDescription(oldguildScheduledEvent: str
|
||||||
return newLines.join('\n')
|
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> {
|
function determineEndDate(description: string): Maybe<Date> {
|
||||||
const segments = description.split(':')
|
const segments = description.split(':')
|
||||||
if (segments.length === 3) {
|
if (segments.length === 3) {
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import { GuildScheduledEvent, GuildScheduledEventCreateOptions } from "discord.js"
|
import { GuildScheduledEvent, GuildScheduledEventCreateOptions } from "discord.js"
|
||||||
import { RepetitonInfo } from "../../types/scheduledEventTypes"
|
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 {
|
function needsToBeRepeated(rInfo: RepetitonInfo): boolean {
|
||||||
if (rInfo.endDate) {
|
if (rInfo.endDate) {
|
||||||
|
@ -22,7 +22,7 @@ export function handleRepeatingEvent(oldguildScheduledEvent: GuildScheduledEvent
|
||||||
const newEventOptions: GuildScheduledEventCreateOptions = {
|
const newEventOptions: GuildScheduledEventCreateOptions = {
|
||||||
name: oldguildScheduledEvent.name,
|
name: oldguildScheduledEvent.name,
|
||||||
description: addRepetitonStringToEventDescription(oldguildScheduledEvent.description, newRepetitonString),
|
description: addRepetitonStringToEventDescription(oldguildScheduledEvent.description, newRepetitonString),
|
||||||
scheduledStartTime: getNewScheduledStart(oldguildScheduledEvent, repetitionInfo),
|
scheduledStartTime: repetitionInfo.schedule.getNewDate(oldguildScheduledEvent.scheduledStartAt),
|
||||||
privacyLevel: oldguildScheduledEvent.privacyLevel,
|
privacyLevel: oldguildScheduledEvent.privacyLevel,
|
||||||
entityType: oldguildScheduledEvent.entityType,
|
entityType: oldguildScheduledEvent.entityType,
|
||||||
channel: oldguildScheduledEvent.channel?.id,
|
channel: oldguildScheduledEvent.channel?.id,
|
||||||
|
|
|
@ -1,10 +1,99 @@
|
||||||
|
import { add } from "date-fns"
|
||||||
|
import { DateResolvable } from "discord.js"
|
||||||
|
import { CustomError, errorCodes } from "../interfaces"
|
||||||
|
|
||||||
export interface RepetitonInfo {
|
export interface RepetitonInfo {
|
||||||
startDate?: Date, // If defined will take precedence over repetitonAmount
|
startDate?: Date, // If defined will take precedence over repetitonAmount
|
||||||
endDate?: Date,// If defined will take precedence over repetitonAmount
|
endDate?: Date,// If defined will take precedence over repetitonAmount
|
||||||
totalAmount: number,
|
totalAmount: number,
|
||||||
alreadyOccured: number,
|
alreadyOccured: number,
|
||||||
schedule: supportedSchedule
|
schedule: Schedule
|
||||||
}
|
}
|
||||||
export const scheduleNames = ['daily', 'weekly', 'monthly', 'everyTwoWeeks', 'everyNDays']
|
export const scheduleNames = ['daily', 'weekly', 'monthly', 'everyNWeeks', 'everyNDays', 'everyNMonths']
|
||||||
export type supportedSchedule = typeof scheduleNames[number]
|
export type supportedSchedule = typeof scheduleNames[number]
|
||||||
|
export interface IScheduleType {
|
||||||
|
name: supportedSchedule,
|
||||||
|
multiplier: number,
|
||||||
|
duration: Duration
|
||||||
|
}
|
||||||
|
export const scheduleTypes: IScheduleType[] = [
|
||||||
|
{
|
||||||
|
name: 'daily',
|
||||||
|
multiplier: 1,
|
||||||
|
duration: {
|
||||||
|
days: 1
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'weekly',
|
||||||
|
multiplier: 1,
|
||||||
|
duration: {
|
||||||
|
weeks: 1
|
||||||
|
}
|
||||||
|
},
|
||||||
|
]
|
||||||
|
export class Schedule {
|
||||||
|
private scheduleName: string
|
||||||
|
private multiplier = 1
|
||||||
|
private duration: Duration
|
||||||
|
private baseScheduleTypes = ['daily', 'weekly', 'monthly', 'yearly']
|
||||||
|
private _scheduleString: string
|
||||||
|
constructor(scheduleString: string) {
|
||||||
|
this._scheduleString = scheduleString.toLowerCase()
|
||||||
|
this.scheduleName = this._scheduleString
|
||||||
|
if (this.baseScheduleTypes.includes(this._scheduleString)) {
|
||||||
|
this.multiplier = 1
|
||||||
|
}
|
||||||
|
if (this._scheduleString.includes('every')) {
|
||||||
|
this.scheduleName = this.getBaseScheduleNameFromVariableString()
|
||||||
|
this.multiplier = this.getMultiplierFromVariableString()
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (this.scheduleName) {
|
||||||
|
case 'daily':
|
||||||
|
this.duration = { days: 1 }
|
||||||
|
break
|
||||||
|
case 'weekly':
|
||||||
|
this.duration = { weeks: 1 }
|
||||||
|
break
|
||||||
|
case 'monthly':
|
||||||
|
this.duration = { months: 1 }
|
||||||
|
break
|
||||||
|
case 'yearly':
|
||||||
|
this.duration = { years: 1 }
|
||||||
|
break
|
||||||
|
default:
|
||||||
|
throw new CustomError('Schedule type not supported', errorCodes.schedule_not_supported)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private getBaseScheduleNameFromVariableString(): string {
|
||||||
|
if (this._scheduleString.includes('week')) return 'weekly'
|
||||||
|
if (this._scheduleString.includes('day')) return 'daily'
|
||||||
|
if (this._scheduleString.includes('month')) return 'monthly'
|
||||||
|
if (this._scheduleString.includes('year')) return 'yearly'
|
||||||
|
return ''
|
||||||
|
}
|
||||||
|
public getMultiplierFromVariableString(): number {
|
||||||
|
const matches = this._scheduleString.match(/\d+/)
|
||||||
|
if (matches) {
|
||||||
|
const multi = matches[0]
|
||||||
|
if (multi)
|
||||||
|
return parseInt(multi)
|
||||||
|
}
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
public calculateDuration(): Duration {
|
||||||
|
const dur: Duration = {
|
||||||
|
days: this.duration.days ? this.duration.days * this.multiplier : undefined,
|
||||||
|
weeks: this.duration.weeks ? this.duration.weeks * this.multiplier : undefined,
|
||||||
|
months: this.duration.months ? this.duration.months * this.multiplier : undefined,
|
||||||
|
years: this.duration.years ? this.duration.years * this.multiplier : undefined,
|
||||||
|
}
|
||||||
|
return dur
|
||||||
|
}
|
||||||
|
|
||||||
|
public getNewDate(oldDate: Date): Date {
|
||||||
|
const newDate = add(oldDate, this.calculateDuration())
|
||||||
|
return newDate
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue