From fdfe7ce404ee085f093f42284fd5f71b5ca8e2df Mon Sep 17 00:00:00 2001 From: kenobi Date: Mon, 17 Jul 2023 21:30:02 +0200 Subject: [PATCH] move date parsing to separate function --- server/helper/vote.controller.ts | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/server/helper/vote.controller.ts b/server/helper/vote.controller.ts index a3864a6..fa13798 100644 --- a/server/helper/vote.controller.ts +++ b/server/helper/vote.controller.ts @@ -13,7 +13,6 @@ import addDays from "date-fns/addDays" import isAfter from "date-fns/isAfter" import { ExtendedClient } from "../structures/client" import { JellyfinHandler } from "../jellyfin/handler" -import { getYear } from "date-fns" export type Vote = { emote: string, //todo habs nicht hinbekommen hier Emotes zu nutzen @@ -76,6 +75,9 @@ export default class VoteController { } logger.info(`No reroll`, { requestId, guildId }) } + parseEventIdFromMessage(message: Message | PartialMessage, requestId: string): string { + throw new Error("Method not implemented.") + } private fetchEventStartDateByEventId(eventId: string, requestId: string): Date { throw new Error("Method not implemented.") } @@ -91,18 +93,16 @@ export default class VoteController { if (!result) throw Error('No event url found in Message') eventId = result?.[0].split('/').at(-1) ?? "" } else if (!line.slice(0, 5).includes(':')) { - const datematcher = RegExp(/((0[1-9]|[12][0-9]|3[01])\.(0[1-9]|1[012]))(\ um\ )(([012][0-9]:[0-5][0-9]))/i) - const result: RegExpMatchArray | null = line.match(datematcher) - const timeFromResult = result?.at(-1) - const dateFromResult = result?.at(1)?.concat(format(new Date(), '.yyyy')).concat(" " + timeFromResult) ?? "" - eventDate = new Date(dateFromResult) + eventDate = this.parseEventDateFromLine(line) } else if (line.slice(0, 5).includes(':')) { const splitLine = line.split(":") const [emoji, movie] = splitLine - if (emoji === NONE_OF_THAT) continue const fetchedVoteFromMessage = message.reactions.cache.get(emoji) if (fetchedVoteFromMessage) { - votes.push({ movie:movie.trim(), emote: emoji, count: fetchedVoteFromMessage.count }) + if (emoji === NONE_OF_THAT) { + votes.push({ movie: NONE_OF_THAT, emote: NONE_OF_THAT, count: fetchedVoteFromMessage.count }) + } else + votes.push({ movie: movie.trim(), emote: emoji, count: fetchedVoteFromMessage.count }) } else { logger.error(`No vote reaction found for movie, assuming 0`, requestId) votes.push({ movie, emote: emoji, count: 0 }) @@ -111,8 +111,12 @@ export default class VoteController { } return { eventId, eventDate, votes } } - private parseEventIdFromMessage(message: Message | PartialMessage, requestId: string): string { - throw new Error("Method not implemented.") + public parseEventDateFromLine(line: string): Date { + const datematcher = RegExp(/((0[1-9]|[12][0-9]|3[01])\.(0[1-9]|1[012]))(\ um\ )(([012][0-9]:[0-5][0-9]))/i) + const result: RegExpMatchArray | null = line.match(datematcher) + const timeFromResult = result?.at(-1) + const dateFromResult = result?.at(1)?.concat(format(new Date(), '.yyyy')).concat(" " + timeFromResult) ?? "" + return new Date(dateFromResult) } public createVoteMessageText(eventId: string, eventStartDate: Date, movies: string[], guildId: string, requestId: string): string {