Compare commits
3 Commits
790a0d65f4
...
aac7967037
Author | SHA1 | Date | |
---|---|---|---|
|
aac7967037 | ||
|
23e3bbc4b1 | ||
|
22f0117d30 |
@ -18,15 +18,16 @@
|
|||||||
"author": "",
|
"author": "",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@discordjs/rest": "^0.3.0",
|
"@discordjs/rest": "^0.4.1",
|
||||||
"@tsconfig/recommended": "^1.0.1",
|
"@tsconfig/recommended": "^1.0.1",
|
||||||
"@types/cors": "^2.8.12",
|
"@types/cors": "^2.8.12",
|
||||||
"@types/express": "^4.17.13",
|
"@types/express": "^4.17.13",
|
||||||
|
"@types/node": "^17.0.25",
|
||||||
"axios": "^0.26.0",
|
"axios": "^0.26.0",
|
||||||
"body-parser": "^1.19.0",
|
"body-parser": "^1.19.0",
|
||||||
"cors": "^2.8.5",
|
"cors": "^2.8.5",
|
||||||
"date-fns": "^2.28.0",
|
"date-fns": "^2.28.0",
|
||||||
"discord-api-types": "^0.27.3",
|
"discord-api-types": "^0.31.2",
|
||||||
"discord.js": "^13.6.0",
|
"discord.js": "^13.6.0",
|
||||||
"dotenv": "^16.0.0",
|
"dotenv": "^16.0.0",
|
||||||
"express": "^4.17.1",
|
"express": "^4.17.1",
|
||||||
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
66
tests/scheduleparsing.test.ts
Normal file
66
tests/scheduleparsing.test.ts
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
import { Schedule } from "../server/types/scheduledEventTypes"
|
||||||
|
|
||||||
|
test('multiplier is parsed', () => {
|
||||||
|
const schedule = new Schedule('every3weeks')
|
||||||
|
const multi = schedule.getMultiplierFromVariableString()
|
||||||
|
expect(multi).toEqual(3)
|
||||||
|
})
|
||||||
|
test('get duration 3 weeks', () => {
|
||||||
|
const schedule = new Schedule('every3weeks')
|
||||||
|
const duration = schedule.calculateDuration()
|
||||||
|
expect(duration).toEqual({ weeks: 3 })
|
||||||
|
})
|
||||||
|
test('get duration 4 years', () => {
|
||||||
|
const schedule = new Schedule('every4years')
|
||||||
|
const duration = schedule.calculateDuration()
|
||||||
|
expect(duration).toEqual({ years: 4 })
|
||||||
|
})
|
||||||
|
test('get duration 27 days', () => {
|
||||||
|
const schedule = new Schedule('every27days')
|
||||||
|
const duration = schedule.calculateDuration()
|
||||||
|
expect(duration).toEqual({ days: 27 })
|
||||||
|
})
|
||||||
|
describe('get new Dates', () => {
|
||||||
|
test('every 27 days', () => {
|
||||||
|
const schedule = new Schedule('every27days')
|
||||||
|
const oldDate = new Date('2022-01-01')
|
||||||
|
const newDate = new Date('2022-01-28')
|
||||||
|
expect(schedule.getNewDate(oldDate)).toEqual(newDate)
|
||||||
|
})
|
||||||
|
test('every 2 weeks', () => {
|
||||||
|
const schedule = new Schedule('every2weeks')
|
||||||
|
const oldDate = new Date('2022-01-01')
|
||||||
|
const newDate = new Date('2022-01-15')
|
||||||
|
expect(schedule.getNewDate(oldDate)).toEqual(newDate)
|
||||||
|
})
|
||||||
|
test('every 1 month', () => {
|
||||||
|
const schedule = new Schedule('every1months')
|
||||||
|
const oldDate = new Date('2022-01-01')
|
||||||
|
const newDate = new Date('2022-02-01')
|
||||||
|
expect(schedule.getNewDate(oldDate)).toEqual(newDate)
|
||||||
|
})
|
||||||
|
test('every 4 year', () => {
|
||||||
|
const schedule = new Schedule('every4years')
|
||||||
|
const oldDate = new Date('2022-01-01')
|
||||||
|
const newDate = new Date('2026-01-01')
|
||||||
|
expect(schedule.getNewDate(oldDate)).toEqual(newDate)
|
||||||
|
})
|
||||||
|
test('every 2 days', () => {
|
||||||
|
const schedule = new Schedule('every2days')
|
||||||
|
const oldDate = new Date('2022-01-01')
|
||||||
|
const newDate = new Date('2022-01-03')
|
||||||
|
expect(schedule.getNewDate(oldDate)).toEqual(newDate)
|
||||||
|
})
|
||||||
|
test('every 2 days', () => {
|
||||||
|
const schedule = new Schedule('Every2DAys')
|
||||||
|
const oldDate = new Date('2022-01-01')
|
||||||
|
const newDate = new Date('2022-01-03')
|
||||||
|
expect(schedule.getNewDate(oldDate)).toEqual(newDate)
|
||||||
|
})
|
||||||
|
test('every 2 day[s]', () => {
|
||||||
|
const schedule = new Schedule('Every2DAy')
|
||||||
|
const oldDate = new Date('2022-01-01')
|
||||||
|
const newDate = new Date('2022-01-03')
|
||||||
|
expect(schedule.getNewDate(oldDate)).toEqual(newDate)
|
||||||
|
})
|
||||||
|
})
|
Loading…
Reference in New Issue
Block a user