date based repetition
This commit is contained in:
		@@ -1,9 +1,8 @@
 | 
			
		||||
import { GuildScheduledEvent } from "discord.js"
 | 
			
		||||
import { repetitionMarkerIsFound } from "../handler/repeatingEvents/helper"
 | 
			
		||||
import { handleRepeatingEvent } from "../handler/repeatingEvents/repeatingEvents.controller"
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
const repetitionMarkerIsFound = (desc: string): boolean => desc.includes('$rep')
 | 
			
		||||
 | 
			
		||||
export const name = 'guildScheduledEventUpdate'
 | 
			
		||||
export function execute(oldguildScheduledEvent: GuildScheduledEvent, newguildScheduledEvent: GuildScheduledEvent) {
 | 
			
		||||
	console.dir(oldguildScheduledEvent)
 | 
			
		||||
 
 | 
			
		||||
@@ -1,8 +1,14 @@
 | 
			
		||||
import { format } from "date-fns"
 | 
			
		||||
import add from "date-fns/add"
 | 
			
		||||
import { DateResolvable, GuildScheduledEvent } from "discord.js"
 | 
			
		||||
import { DateResolvable, Guild, GuildScheduledEvent, GuildScheduledEventCreateOptions } from "discord.js"
 | 
			
		||||
import { findInScheduleTypes } from "../../helper/typeFind"
 | 
			
		||||
import { Maybe } from "../../interfaces"
 | 
			
		||||
import { RepetitonInfo, supportedSchedule } from "../../types/scheduledEventTypes"
 | 
			
		||||
 | 
			
		||||
export const repetitionMarkerIsFound = (desc: string): boolean => desc.includes('$rep')
 | 
			
		||||
export function createEventInGuild(guild: Guild, eventInfo: GuildScheduledEventCreateOptions): Promise<any> {
 | 
			
		||||
	return guild.scheduledEvents.create(eventInfo)
 | 
			
		||||
}
 | 
			
		||||
export function getRepetitonInfo(description: string): RepetitonInfo {
 | 
			
		||||
 | 
			
		||||
	const lines = description.split(`\n`)
 | 
			
		||||
@@ -11,10 +17,12 @@ export function getRepetitonInfo(description: string): RepetitonInfo {
 | 
			
		||||
		throw new Error('Cant find repetition string')
 | 
			
		||||
	const schedule: supportedSchedule = determineSchedule(repetitionString)
 | 
			
		||||
	const { totalAmount, alreadyOccured } = determineRepetitionCount(repetitionString)
 | 
			
		||||
	const endDate = determineEndDate(repetitionString)
 | 
			
		||||
	return {
 | 
			
		||||
		totalAmount,
 | 
			
		||||
		alreadyOccured,
 | 
			
		||||
		schedule
 | 
			
		||||
		schedule,
 | 
			
		||||
		endDate
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -36,6 +44,8 @@ export function determineRepetitionCount(description: string): { totalAmount: nu
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export function buildNewRepetitionString(repetitionInfo: RepetitonInfo) {
 | 
			
		||||
	if (repetitionInfo.endDate)
 | 
			
		||||
		return `$rep:${repetitionInfo.schedule}:${format(repetitionInfo.endDate, 'yyyy-MM-dd')}`
 | 
			
		||||
	return `$rep:${repetitionInfo.schedule}:${repetitionInfo.alreadyOccured + 1}/${repetitionInfo.totalAmount}`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -71,3 +81,25 @@ export function getNewScheduledStart(oldguildScheduledEvent: GuildScheduledEvent
 | 
			
		||||
	const newDate = add(oldDate, duration)
 | 
			
		||||
	return newDate
 | 
			
		||||
}
 | 
			
		||||
function determineEndDate(description: string): Maybe<Date> {
 | 
			
		||||
	const segments = description.split(':')
 | 
			
		||||
	if (segments.length === 3) {
 | 
			
		||||
		// rep:sched:countOrDate
 | 
			
		||||
		const segmentValue = segments[2]
 | 
			
		||||
		if (segmentValue.includes('/')) {
 | 
			
		||||
			if (segmentValue.match(/\//g) || [].length !== 2) {
 | 
			
		||||
				return
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		const dateValue = new Date(segmentValue)
 | 
			
		||||
		return dateValue
 | 
			
		||||
	}
 | 
			
		||||
	else if (segments.length === 4) {
 | 
			
		||||
		// rep:sched:count:date
 | 
			
		||||
		const segmentValue = segments[3]
 | 
			
		||||
		const dateValue = new Date(segmentValue)
 | 
			
		||||
		return dateValue
 | 
			
		||||
	}
 | 
			
		||||
	return
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -1,8 +1,16 @@
 | 
			
		||||
import { GuildScheduledEvent, GuildScheduledEventCreateOptions } from "discord.js"
 | 
			
		||||
import { RepetitonInfo } from "../../types/scheduledEventTypes"
 | 
			
		||||
import { addRepetitonStringToEventDescription, buildNewRepetitionString, getNewScheduledStart, getRepetitonInfo } from "./helper"
 | 
			
		||||
import { addRepetitonStringToEventDescription, buildNewRepetitionString, createEventInGuild, getNewScheduledStart, getRepetitonInfo } from "./helper"
 | 
			
		||||
 | 
			
		||||
const needsToBeRepeated = (rInfo: RepetitonInfo): boolean => rInfo.alreadyOccured < rInfo.totalAmount
 | 
			
		||||
function needsToBeRepeated(rInfo: RepetitonInfo): boolean {
 | 
			
		||||
	if (rInfo.endDate) {
 | 
			
		||||
		if (new Date() < rInfo.endDate)
 | 
			
		||||
			return true
 | 
			
		||||
	} else {
 | 
			
		||||
		return rInfo.alreadyOccured < rInfo.totalAmount
 | 
			
		||||
	}
 | 
			
		||||
	return false
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export function handleRepeatingEvent(oldguildScheduledEvent: GuildScheduledEvent, newguildScheduledEvent: GuildScheduledEvent) {
 | 
			
		||||
	if (!oldguildScheduledEvent.description) throw new Error('Event has no description -> cant handle this')
 | 
			
		||||
@@ -20,9 +28,12 @@ export function handleRepeatingEvent(oldguildScheduledEvent: GuildScheduledEvent
 | 
			
		||||
				channel: oldguildScheduledEvent.channel?.id,
 | 
			
		||||
				reason: 'Repetition'
 | 
			
		||||
			}
 | 
			
		||||
			newguildScheduledEvent.guild?.scheduledEvents.create(newEventOptions)
 | 
			
		||||
			if (!newguildScheduledEvent.guild) throw new Error('No guild on event?')
 | 
			
		||||
			createEventInGuild(newguildScheduledEvent.guild, newEventOptions)
 | 
			
		||||
		} catch (err) {
 | 
			
		||||
			console.error(err)
 | 
			
		||||
		}
 | 
			
		||||
	} else {
 | 
			
		||||
		console.log('Event does not need to be repeated')
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user