Compare commits

...

3 Commits

Author SHA1 Message Date
c022cc32d5 refactor eventId parsing to separate function
All checks were successful
Compile the repository / compile (pull_request) Successful in 49s
Run unit tests / test (pull_request) Successful in 1m35s
prepare for querying discord api for event info instead of parsing via regex
2023-07-17 22:50:24 +02:00
e763e76413 add new test for eventId parsing 2023-07-17 22:49:12 +02:00
137d156981 fix date string in vote message 2023-07-17 22:48:57 +02:00
4 changed files with 42 additions and 25 deletions

View File

@ -18,6 +18,6 @@ export function createDateStringFromEvent(eventStartDate:Date, requestId: string
return `heute um ${time}`
}
const date = format(zonedDateTime, "eeee dd.MM", { locale: de })
const date = format(zonedDateTime, "eeee dd.MM.", { locale: de })
return `am ${date} um ${time}`
}

View File

@ -13,6 +13,7 @@ import addDays from "date-fns/addDays"
import isAfter from "date-fns/isAfter"
import { ExtendedClient } from "../structures/client"
import { JellyfinHandler } from "../jellyfin/handler"
import { ObjectGroupUpdateToJSON } from "../jellyfin"
export type Vote = {
emote: string, //todo habs nicht hinbekommen hier Emotes zu nutzen
@ -52,8 +53,8 @@ export default class VoteController {
messageReaction.message.edit((messageReaction.message.content ?? "").concat('\nDiese Abstimmung muss wiederholt werden.'))
// get movies that _had_ votes
//const oldMovieNames: Vote[] = this.parseVotesFromVoteMessage(messageReaction.message, requestId)
const eventId = this.parseEventIdFromMessage(messageReaction.message, requestId)
const eventStartDate: Date = this.fetchEventStartDateByEventId(eventId, requestId) //TODO
const parsedIds = this.parseGuildIdAndEventIdFromWholeMessage(messageReaction.message.cleanContent ?? '')
const eventStartDate: Date = this.fetchEventStartDateByEventId(parsedIds.eventId, requestId) //TODO
//
// get movies from jellyfin to fill the remaining slots
const newMovieCount = config.bot.random_movie_count //- oldMovieNames.length
@ -64,7 +65,7 @@ export default class VoteController {
// create new message
await this.closePoll(messageReaction.message.guild, requestId)
const message = this.createVoteMessageText(eventId, eventStartDate, movies, guildId, requestId)
const message = this.createVoteMessageText(parsedIds.guildId, eventStartDate, movies, guildId, requestId)
const announcementChannel = this.client.getAnnouncementChannelForGuild(guildId)
if (!announcementChannel) {
logger.error(`No announcementChannel found for ${guildId}, can't post poll`)
@ -75,26 +76,23 @@ export default class VoteController {
}
logger.info(`No reroll`, { requestId, guildId })
}
parseEventIdFromMessage(message: Message<boolean> | PartialMessage, requestId: string): string {
throw new Error("Method not implemented.")
}
private fetchEventStartDateByEventId(eventId: string, requestId: string): Date {
throw new Error("Method not implemented.")
}
public parseGuildIdAndEventIdFromWholeMessage(message: string) {
const idmatch = RegExp(/(?:http|https):\/\/discord\.com\/events\/(\d*)\/(\d*)/)
const matches = message.match(idmatch)
if (matches && matches.length == 3)
return { guildId: matches[1], eventId: matches[2] }
throw Error(`Could not find eventId in Vote Message`)
}
public parseVotesFromVoteMessage(message: VoteMessage, requestId: string): VoteMessageInfo {
const lines = message.cleanContent.split('\n')
let eventId = ""
let eventDate: Date = new Date()
let parsedIds = this.parseGuildIdAndEventIdFromWholeMessage(message.cleanContent)
let eventDate: Date = this.parseEventDateFromMessage(message.cleanContent)
let votes: Vote[] = []
for (const line of lines) {
if (line.includes('https://discord.com/events')) {
const urlMatcher = RegExp(/(http|https|ftp):\/\/(\S*)/ig)
const result = line.match(urlMatcher)
if (!result) throw Error('No event url found in Message')
eventId = result?.[0].split('/').at(-1) ?? ""
} else if (!line.slice(0, 5).includes(':')) {
eventDate = this.parseEventDateFromLine(line)
} else if (line.slice(0, 5).includes(':')) {
if (line.slice(0, 5).includes(':')) {
const splitLine = line.split(":")
const [emoji, movie] = splitLine
const fetchedVoteFromMessage = message.reactions.cache.get(emoji)
@ -109,13 +107,13 @@ export default class VoteController {
}
}
}
return <VoteMessageInfo>{ eventId, eventDate, votes }
return <VoteMessageInfo>{ eventId: parsedIds.eventId, eventDate, votes }
}
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)
public parseEventDateFromMessage(message: string): Date {
const datematcher = RegExp(/((?:0[1-9]|[12][0-9]|3[01])\.(?:0[1-9]|1[012])\.)(?:\ um\ )((?:(?:[01][0-9]|[2][0-3])\:[0-5][0-9])|(?:[2][4]\:00))!/i)
const result: RegExpMatchArray | null = message.match(datematcher)
const timeFromResult = result?.at(-1)
const dateFromResult = result?.at(1)?.concat(format(new Date(), '.yyyy')).concat(" " + timeFromResult) ?? ""
const dateFromResult = result?.at(1)?.concat(format(new Date(), 'yyyy')).concat(" " + timeFromResult) ?? ""
return new Date(dateFromResult)
}

View File

@ -13,8 +13,9 @@ test('parse votes from vote message', () => {
]
const testEventId = '1234321'
const testEventDate = new Date('2023-01-01')
const testGuildId = "888999888"
const voteController: VoteController = new VoteController(<ExtendedClient>{}, <JellyfinHandler>{})
const testMessage = voteController.createVoteMessageText(testEventId, testEventDate, testMovies, "guildid", "requestId")
const testMessage = voteController.createVoteMessageText(testEventId, testEventDate, testMovies, testGuildId, "requestId")
const expectedResult: VoteMessageInfo = {
@ -53,3 +54,21 @@ test('parse votes from vote message', () => {
expect(result.votes.length).toEqual(expectedResult.votes.length)
expect(result).toEqual(expectedResult)
})
test('parse votes from vote message', () => {
const testMovies = [
'Movie1',
'Movie2',
'Movie3',
'Movie4',
'Movie5',
]
const testEventId = '1234321'
const testEventDate = new Date('2023-01-01')
const testGuildId = "888999888"
const voteController: VoteController = new VoteController(<ExtendedClient>{}, <JellyfinHandler>{})
const testMessage = voteController.createVoteMessageText(testEventId, testEventDate, testMovies, testGuildId, "requestId")
const result = voteController.parseGuildIdAndEventIdFromWholeMessage(testMessage)
expect(result).toEqual({ guildId: testGuildId, eventId: testEventId })
})

View File

@ -10,6 +10,6 @@ function getTestDate(date: string): Date {
}
test('createDateStringFromEvent - correct formatting', () => {
expect(createDateStringFromEvent(getTestDate('01-01-2023 12:30'), "")).toEqual('heute um 12:30')
expect(createDateStringFromEvent(getTestDate('01-02-2023 12:30'), "")).toEqual('am Montag 02.01 um 12:30')
expect(createDateStringFromEvent(getTestDate('01-03-2023 12:30'), "")).toEqual('am Dienstag 03.01 um 12:30')
expect(createDateStringFromEvent(getTestDate('01-02-2023 12:30'), "")).toEqual('am Montag 02.01. um 12:30')
expect(createDateStringFromEvent(getTestDate('01-03-2023 12:30'), "")).toEqual('am Dienstag 03.01. um 12:30')
})