date based repetition

This commit is contained in:
mightypanders 2022-04-21 19:46:03 +02:00
parent 19f0dccaec
commit 85770a6031
3 changed files with 49 additions and 7 deletions

View File

@ -1,9 +1,8 @@
import { GuildScheduledEvent } from "discord.js" import { GuildScheduledEvent } from "discord.js"
import { repetitionMarkerIsFound } from "../handler/repeatingEvents/helper"
import { handleRepeatingEvent } from "../handler/repeatingEvents/repeatingEvents.controller" import { handleRepeatingEvent } from "../handler/repeatingEvents/repeatingEvents.controller"
const repetitionMarkerIsFound = (desc: string): boolean => desc.includes('$rep')
export const name = 'guildScheduledEventUpdate' export const name = 'guildScheduledEventUpdate'
export function execute(oldguildScheduledEvent: GuildScheduledEvent, newguildScheduledEvent: GuildScheduledEvent) { export function execute(oldguildScheduledEvent: GuildScheduledEvent, newguildScheduledEvent: GuildScheduledEvent) {
console.dir(oldguildScheduledEvent) console.dir(oldguildScheduledEvent)

View File

@ -1,8 +1,14 @@
import { format } from "date-fns"
import add from "date-fns/add" 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 { findInScheduleTypes } from "../../helper/typeFind"
import { Maybe } from "../../interfaces"
import { RepetitonInfo, 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> {
return guild.scheduledEvents.create(eventInfo)
}
export function getRepetitonInfo(description: string): RepetitonInfo { export function getRepetitonInfo(description: string): RepetitonInfo {
const lines = description.split(`\n`) const lines = description.split(`\n`)
@ -11,10 +17,12 @@ export function getRepetitonInfo(description: string): RepetitonInfo {
throw new Error('Cant find repetition string') throw new Error('Cant find repetition string')
const schedule: supportedSchedule = determineSchedule(repetitionString) const schedule: supportedSchedule = determineSchedule(repetitionString)
const { totalAmount, alreadyOccured } = determineRepetitionCount(repetitionString) const { totalAmount, alreadyOccured } = determineRepetitionCount(repetitionString)
const endDate = determineEndDate(repetitionString)
return { return {
totalAmount, totalAmount,
alreadyOccured, alreadyOccured,
schedule schedule,
endDate
} }
} }
@ -36,6 +44,8 @@ export function determineRepetitionCount(description: string): { totalAmount: nu
} }
export function buildNewRepetitionString(repetitionInfo: RepetitonInfo) { 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}` return `$rep:${repetitionInfo.schedule}:${repetitionInfo.alreadyOccured + 1}/${repetitionInfo.totalAmount}`
} }
@ -71,3 +81,25 @@ export function getNewScheduledStart(oldguildScheduledEvent: GuildScheduledEvent
const newDate = add(oldDate, duration) const newDate = add(oldDate, duration)
return newDate 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
}

View File

@ -1,8 +1,16 @@
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, 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) { export function handleRepeatingEvent(oldguildScheduledEvent: GuildScheduledEvent, newguildScheduledEvent: GuildScheduledEvent) {
if (!oldguildScheduledEvent.description) throw new Error('Event has no description -> cant handle this') 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, channel: oldguildScheduledEvent.channel?.id,
reason: 'Repetition' reason: 'Repetition'
} }
newguildScheduledEvent.guild?.scheduledEvents.create(newEventOptions) if (!newguildScheduledEvent.guild) throw new Error('No guild on event?')
createEventInGuild(newguildScheduledEvent.guild, newEventOptions)
} catch (err) { } catch (err) {
console.error(err) console.error(err)
} }
} else {
console.log('Event does not need to be repeated')
} }
} }