Compare commits
No commits in common. "aac79670377219ef4ef6c37689c6402aa9194b8a" and "790a0d65f431c1bcc7470a817cd9f687bc9f375b" have entirely different histories.
aac7967037
...
790a0d65f4
@ -18,16 +18,15 @@
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"@discordjs/rest": "^0.4.1",
|
||||
"@discordjs/rest": "^0.3.0",
|
||||
"@tsconfig/recommended": "^1.0.1",
|
||||
"@types/cors": "^2.8.12",
|
||||
"@types/express": "^4.17.13",
|
||||
"@types/node": "^17.0.25",
|
||||
"axios": "^0.26.0",
|
||||
"body-parser": "^1.19.0",
|
||||
"cors": "^2.8.5",
|
||||
"date-fns": "^2.28.0",
|
||||
"discord-api-types": "^0.31.2",
|
||||
"discord-api-types": "^0.27.3",
|
||||
"discord.js": "^13.6.0",
|
||||
"dotenv": "^16.0.0",
|
||||
"express": "^4.17.1",
|
||||
|
@ -2,8 +2,9 @@ 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, Schedule, supportedSchedule } from "../../types/scheduledEventTypes"
|
||||
import { RepetitonInfo, supportedSchedule } from "../../types/scheduledEventTypes"
|
||||
|
||||
export const repetitionMarkerIsFound = (desc: string): boolean => desc.includes('$rep')
|
||||
export function createEventInGuild(guild: Guild, eventInfo: GuildScheduledEventCreateOptions): Promise<any> {
|
||||
@ -15,8 +16,7 @@ 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 scheduleString = determineScheduleString(repetitionString)
|
||||
const schedule: Schedule = new Schedule(scheduleString)
|
||||
const schedule: supportedSchedule = determineSchedule(repetitionString)
|
||||
const { totalAmount, alreadyOccured } = determineRepetitionCount(repetitionString)
|
||||
const endDate = determineEndDate(repetitionString)
|
||||
return {
|
||||
@ -27,13 +27,14 @@ export function getRepetitonInfo(description: string): RepetitonInfo {
|
||||
}
|
||||
}
|
||||
|
||||
export function determineScheduleString(repetitionLine: string): supportedSchedule {
|
||||
export function determineSchedule(repetitionLine: string): supportedSchedule {
|
||||
const segments = repetitionLine.split(':')
|
||||
const scheduleSegment = segments[1]
|
||||
if (scheduleSegment)
|
||||
return scheduleSegment
|
||||
const easilyKnownScheduleName = findInScheduleTypes(scheduleSegment)
|
||||
if (easilyKnownScheduleName)
|
||||
return easilyKnownScheduleName
|
||||
else
|
||||
throw new CustomError('No schedule segment found', errorCodes.no_schedule)
|
||||
throw new CustomError('Inferring schedule names is not yet supported', errorCodes.schedule_not_supported)
|
||||
}
|
||||
|
||||
export function determineRepetitionCount(description: string): { totalAmount: number; alreadyOccured: number } {
|
||||
@ -61,6 +62,30 @@ 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) {
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { GuildScheduledEvent, GuildScheduledEventCreateOptions } from "discord.js"
|
||||
import { RepetitonInfo } from "../../types/scheduledEventTypes"
|
||||
import { addRepetitonStringToEventDescription, buildNewRepetitionString, createEventInGuild, getRepetitonInfo } from "./helper"
|
||||
import { addRepetitonStringToEventDescription, buildNewRepetitionString, createEventInGuild, getNewScheduledStart, 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: repetitionInfo.schedule.getNewDate(oldguildScheduledEvent.scheduledStartAt),
|
||||
scheduledStartTime: getNewScheduledStart(oldguildScheduledEvent, repetitionInfo),
|
||||
privacyLevel: oldguildScheduledEvent.privacyLevel,
|
||||
entityType: oldguildScheduledEvent.entityType,
|
||||
channel: oldguildScheduledEvent.channel?.id,
|
||||
|
@ -1,99 +1,10 @@
|
||||
import { add } from "date-fns"
|
||||
import { DateResolvable } from "discord.js"
|
||||
import { CustomError, errorCodes } from "../interfaces"
|
||||
|
||||
export interface RepetitonInfo {
|
||||
startDate?: Date, // If defined will take precedence over repetitonAmount
|
||||
endDate?: Date,// If defined will take precedence over repetitonAmount
|
||||
totalAmount: number,
|
||||
alreadyOccured: number,
|
||||
schedule: Schedule
|
||||
schedule: supportedSchedule
|
||||
}
|
||||
export const scheduleNames = ['daily', 'weekly', 'monthly', 'everyNWeeks', 'everyNDays', 'everyNMonths']
|
||||
export const scheduleNames = ['daily', 'weekly', 'monthly', 'everyTwoWeeks', 'everyNDays']
|
||||
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
|
||||
}
|
||||
}
|
||||
|
@ -1,66 +0,0 @@
|
||||
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