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:
parent
b76df79d2a
commit
ce4dc81f7d
|
@ -57,30 +57,52 @@ 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) {
|
public async handleReroll(voteMessage: VoteMessage, guild: Guild, guildId: string, requestId: string) {
|
||||||
|
|
||||||
//get movies that already had votes to give them a second chance
|
//get movies that already had votes to give them a second chance
|
||||||
const voteInfo: VoteMessageInfo = await this.parseVoteInfoFromVoteMessage(voteMessage, requestId)
|
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
|
// 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)
|
const newMovies: string[] = await this.yavinJellyfinHandler.getRandomMovieNames(newMovieCount, guildId, requestId)
|
||||||
|
|
||||||
// merge
|
// 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
|
// 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)
|
const announcementChannel = this.client.getAnnouncementChannelForGuild(guildId)
|
||||||
if (!announcementChannel) {
|
if (!announcementChannel) {
|
||||||
logger.error(`No announcementChannel found for ${guildId}, can't post poll`)
|
logger.error(`No announcementChannel found for ${guildId}, can't post poll`)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
const sentMessage = await this.sendVoteMessage(message, movies.length, announcementChannel)
|
|
||||||
sentMessage.pin()
|
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>> {
|
private async fetchEventStartDateByEventId(guild: Guild, eventId: string, requestId: string): Promise<Maybe<Date>> {
|
||||||
const guildEvent: GuildScheduledEvent = await guild.scheduledEvents.fetch(eventId)
|
const guildEvent: GuildScheduledEvent = await guild.scheduledEvents.fetch(eventId)
|
||||||
|
@ -196,14 +218,15 @@ export default class VoteController {
|
||||||
logger.info("Deleting vote message")
|
logger.info("Deleting vote message")
|
||||||
await lastMessage.delete()
|
await lastMessage.delete()
|
||||||
const event = await this.getEvent(guild, guild.id, requestId)
|
const event = await this.getEvent(guild, guild.id, requestId)
|
||||||
if (event) {
|
if (event && votes?.length > 0) {
|
||||||
this.updateEvent(event, votes, guild, guildId, requestId)
|
this.updateEvent(event, votes, guild, guildId, requestId)
|
||||||
this.sendVoteClosedMessage(event, votes[0].movie, guildId, requestId)
|
this.sendVoteClosedMessage(event, votes[0].movie, guildId, requestId)
|
||||||
}
|
}
|
||||||
|
|
||||||
lastMessage.unpin() //todo: uncomment when bot has permission to pin/unpin
|
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[]> {
|
public async getVotesByEmote(message: Message, guildId: string, requestId: string): Promise<Vote[]> {
|
||||||
const votes: Vote[] = []
|
const votes: Vote[] = []
|
||||||
logger.debug(`Number of items in emotes: ${Object.values(Emotes).length}`, { guildId, requestId })
|
logger.debug(`Number of items in emotes: ${Object.values(Emotes).length}`, { guildId, requestId })
|
||||||
|
@ -241,8 +264,8 @@ export default class VoteController {
|
||||||
logger.info("Updating event.", { guildId, requestId })
|
logger.info("Updating event.", { guildId, requestId })
|
||||||
voteEvent.edit(options)
|
voteEvent.edit(options)
|
||||||
}
|
}
|
||||||
public async sendVoteClosedMessage(event: GuildScheduledEvent, movie: string, guildId: string, requestId: string) {
|
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 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 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 = {
|
const options: MessageCreateOptions = {
|
||||||
|
@ -252,13 +275,14 @@ export default class VoteController {
|
||||||
const announcementChannel = this.client.getAnnouncementChannelForGuild(guildId)
|
const announcementChannel = this.client.getAnnouncementChannelForGuild(guildId)
|
||||||
logger.info("Sending vote closed message.", { guildId, requestId })
|
logger.info("Sending vote closed message.", { guildId, requestId })
|
||||||
if (!announcementChannel) {
|
if (!announcementChannel) {
|
||||||
logger.error("Could not find announcement channel. Please fix!", { guildId, requestId })
|
const errorMessages = "Could not find announcement channel. Please fix!"
|
||||||
return
|
logger.error(errorMessages, { guildId, requestId })
|
||||||
|
throw errorMessages
|
||||||
}
|
}
|
||||||
announcementChannel.send(options)
|
return announcementChannel.send(options)
|
||||||
}
|
}
|
||||||
private extractMovieFromMessageByEmote(message: Message, emote: string): string {
|
private extractMovieFromMessageByEmote(lastMessages: Message, emote: string): string {
|
||||||
const lines = message.cleanContent.split("\n")
|
const lines = lastMessages.cleanContent.split("\n")
|
||||||
const emoteLines = lines.filter(line => line.includes(emote))
|
const emoteLines = lines.filter(line => line.includes(emote))
|
||||||
|
|
||||||
if (!emoteLines) {
|
if (!emoteLines) {
|
||||||
|
|
Loading…
Reference in New Issue