From d6300e8bec85d6791bb9159dc442ea04af6e6d7b Mon Sep 17 00:00:00 2001 From: Sammy Date: Fri, 16 Jun 2023 20:15:36 +0200 Subject: [PATCH] improve movie name resolving When creating the poll the bot will now only request movie names instead of movies This improves the errorhandling because the movie names cannot be null Also the movieName function filters empty movienames and will guarantee the requested number of names --- server/events/guildScheduledEventCreate.ts | 6 +++--- server/jellyfin/handler.ts | 13 ++++++++++++- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/server/events/guildScheduledEventCreate.ts b/server/events/guildScheduledEventCreate.ts index d14721e..7a28fee 100644 --- a/server/events/guildScheduledEventCreate.ts +++ b/server/events/guildScheduledEventCreate.ts @@ -24,10 +24,10 @@ export async function execute(event: GuildScheduledEvent) { logger.info("Event was a placeholder event to start a new watchparty and voting. Creating vote!", { guildId: event.guildId, requestId }) logger.debug("Renaming event", { guildId: event.guildId, requestId }) event.edit({ name: "Watchparty - Voting offen" }) - const movies = await yavinJellyfinHandler.getRandomMovies(5, event.guildId, requestId) + const movies = await yavinJellyfinHandler.getRandomMovieNames(5, event.guildId, requestId) logger.info(`Got ${movies.length} random movies. Creating voting`, { guildId: event.guildId, requestId }) - logger.debug(`Movies: ${JSON.stringify(movies.map(movie => movie.name))}`, { guildId: event.guildId, requestId }) + logger.debug(`Movies: ${JSON.stringify(movies)}`, { guildId: event.guildId, requestId }) const announcementChannel: Maybe = client.getAnnouncementChannelForGuild(event.guildId) if(!announcementChannel) { @@ -45,7 +45,7 @@ export async function execute(event: GuildScheduledEvent) { let message = `[Abstimmung]\n<@&${config.bot.announcement_role}> Es gibt eine neue Abstimmung für die nächste Watchparty am ${date} um ${time}}! Stimme hierunter für den nächsten Film ab!\n` for (let i = 0; i < movies.length; i++) { - message = message.concat(Emotes[i]).concat(": ").concat(movies[i].name ?? "Film hatte keinen Namen :(").concat("\n") + message = message.concat(Emotes[i]).concat(": ").concat(movies[i]).concat("\n") } const options: MessageCreateOptions = { diff --git a/server/jellyfin/handler.ts b/server/jellyfin/handler.ts index a1af178..e47ac6c 100644 --- a/server/jellyfin/handler.ts +++ b/server/jellyfin/handler.ts @@ -242,10 +242,21 @@ export class JellyfinHandler { const index = Math.floor(Math.random() * allMovies.length) movies.push(...allMovies.splice(index, 1)) // maybe out of bounds? ? } - return movies } + public async getRandomMovieNames(count: number, guildId: string, requestId: string): Promise { + logger.info(`${count} random movie names requested`, { guildId, requestId }) + + let movieCount = 0 + let movieNames: string[] + do { + movieNames = (await this.getRandomMovies(count, guildId, requestId)).filter(movie => movie.name && movie.name.length > 0).map(movie => movie.name) + movieCount = movieNames.length + } while (movieCount < count) + return movieNames + } + } export enum UserUpsertResult { enabled, created }