fix incorrect reroll behaviour

now correctly fetches old movies, filters already voted on movies, gets new movies, creates new poll message, deletes old message
This commit is contained in:
kenobi 2023-08-13 18:35:22 +02:00
parent b76df79d2a
commit ce4dc81f7d

View File

@ -57,31 +57,53 @@ export default class VoteController {
}
}
private async removeMessage(msg: Message): Promise<Message<boolean>> {
if (msg.pinned) {
await msg.unpin()
}
return await msg.delete()
}
public isAboveThreshold(vote: Vote): boolean {
const aboveThreshold = (vote.count - 1) >= 1
logger.debug(`${vote.movie} : ${vote.count} -> above: ${aboveThreshold}`)
return aboveThreshold
}
public async handleReroll(voteMessage: VoteMessage, guild: Guild, guildId: string, requestId: string) {
//get movies that already had votes to give them a second chance
const voteInfo: VoteMessageInfo = await this.parseVoteInfoFromVoteMessage(voteMessage, requestId)
const votedOnMovies = voteInfo.votes.filter(this.isAboveThreshold).filter(x => x.emote !== NONE_OF_THAT)
logger.info(`Found ${votedOnMovies.length} with votes`, { requestId, guildId })
// get movies from jellyfin to fill the remaining slots
const newMovieCount: number = config.bot.random_movie_count - voteInfo.votes.filter(x => x.count > 2).length
const newMovieCount: number = config.bot.random_movie_count - votedOnMovies.length
logger.info(`Fetching ${newMovieCount} from jellyfin`)
const newMovies: string[] = await this.yavinJellyfinHandler.getRandomMovieNames(newMovieCount, guildId, requestId)
// merge
const movies: string[] = newMovies.concat(voteInfo.votes.map(x => x.movie))
const movies: string[] = newMovies.concat(votedOnMovies.map(x => x.movie))
// create new message
await this.closePoll(guild, requestId)
const message = this.createVoteMessageText(guild.id, voteInfo.eventDate, movies, guildId, requestId)
logger.info(`Creating new poll message with new movies: ${movies}`, { requestId, guildId })
const message = this.createVoteMessageText(voteInfo.eventId, voteInfo.eventDate, movies, guildId, requestId)
const announcementChannel = this.client.getAnnouncementChannelForGuild(guildId)
if (!announcementChannel) {
logger.error(`No announcementChannel found for ${guildId}, can't post poll`)
return
}
try {
logger.info(`Trying to remove old vote Message`, { requestId, guildId })
this.removeMessage(voteMessage)
} catch (err) {
logger.error(`Error during removeMessage: ${err}`)
}
const sentMessage = await this.sendVoteMessage(message, movies.length, announcementChannel)
sentMessage.pin()
logger.info(`Sent and pinned new poll message`, { requestId, guildId })
}
private async fetchEventStartDateByEventId(guild: Guild, eventId: string, requestId: string): Promise<Maybe<Date>> {
const guildEvent: GuildScheduledEvent = await guild.scheduledEvents.fetch(eventId)
if (!guildEvent) logger.error(`GuildScheduledEvent with id${eventId} could not be found`, { requestId, guildId: guild.id })
@ -196,14 +218,15 @@ export default class VoteController {
logger.info("Deleting vote message")
await lastMessage.delete()
const event = await this.getEvent(guild, guild.id, requestId)
if (event) {
if (event && votes?.length > 0) {
this.updateEvent(event, votes, guild, guildId, requestId)
this.sendVoteClosedMessage(event, votes[0].movie, guildId, requestId)
}
lastMessage.unpin() //todo: uncomment when bot has permission to pin/unpin
}
/**
* gets votes for the movies without the NONE_OF_THAT votes
*/
public async getVotesByEmote(message: Message, guildId: string, requestId: string): Promise<Vote[]> {
const votes: Vote[] = []
logger.debug(`Number of items in emotes: ${Object.values(Emotes).length}`, { guildId, requestId })
@ -241,10 +264,10 @@ export default class VoteController {
logger.info("Updating event.", { guildId, requestId })
voteEvent.edit(options)
}
public async sendVoteClosedMessage(event: GuildScheduledEvent, movie: string, guildId: string, requestId: string) {
const date = event.scheduledStartAt ? format(event.scheduledStartAt, "dd.MM") : "Fehler, event hatte kein Datum"
public async sendVoteClosedMessage(event: GuildScheduledEvent, movie: string, guildId: string, requestId: string): Promise<Message<boolean>> {
const date = event.scheduledStartAt ? format(event.scheduledStartAt, "dd.MM.") : "Fehler, event hatte kein Datum"
const time = event.scheduledStartAt ? format(event.scheduledStartAt, "HH:mm") : "Fehler, event hatte kein Datum"
const body = `[Abstimmung beendet] für https://discord.com/events/${event.guildId}/${event.id}\n<@&${config.bot.announcement_role}> Wir gucken ${movie} am ${date} um ${time}`
const body = `[Abstimmung beendet] für https://discord.com/events/${event.guildId}/${event.id}\n<@&${config.bot.announcement_role}> Wir gucken ${movie} am ${date} um ${time}`
const options: MessageCreateOptions = {
content: body,
allowedMentions: { parse: ["roles"] }
@ -252,13 +275,14 @@ export default class VoteController {
const announcementChannel = this.client.getAnnouncementChannelForGuild(guildId)
logger.info("Sending vote closed message.", { guildId, requestId })
if (!announcementChannel) {
logger.error("Could not find announcement channel. Please fix!", { guildId, requestId })
return
const errorMessages = "Could not find announcement channel. Please fix!"
logger.error(errorMessages, { guildId, requestId })
throw errorMessages
}
announcementChannel.send(options)
return announcementChannel.send(options)
}
private extractMovieFromMessageByEmote(message: Message, emote: string): string {
const lines = message.cleanContent.split("\n")
private extractMovieFromMessageByEmote(lastMessages: Message, emote: string): string {
const lines = lastMessages.cleanContent.split("\n")
const emoteLines = lines.filter(line => line.includes(emote))
if (!emoteLines) {