feat/40-reroll-on-disinterest #54

Merged
kenobi merged 73 commits from feat/40-reroll-on-disinterest into master 2023-11-19 20:24:36 +01:00
2 changed files with 54 additions and 40 deletions
Showing only changes of commit dc66c277b2 - Show all commits

View File

@ -34,7 +34,7 @@ export async function execute(messageReaction: MessageReaction, user: User) {
if (isVoteMessage(reactedUponMessage)) {
if (messageReaction.emoji.toString() === NONE_OF_THAT) {
logger.info(`Reaction is NONE_OF_THAT on a vote message. Handling`, { requestId, guildId })
return client.voteController.handleNoneOfThatVote(messageReaction, user, reactedUponMessage, requestId, guildId)
return client.voteController.handleNoneOfThatVote(messageReaction, reactedUponMessage, requestId, guildId)
}
if (messageReaction.emoji.toString() === Emoji.one) {
// do something
magnetotail marked this conversation as resolved
Review

?

?
Review

Replace this do something with a meaningful todo

Replace this do something with a meaningful todo
Review

removed
03b6a30ffa

removed 03b6a30ffa67afca9a4bc0563f7bf092bbcdd60b

View File

@ -34,8 +34,9 @@ export default class VoteController {
this.yavinJellyfinHandler = _yavin
}
public async handleNoneOfThatVote(messageReaction: MessageReaction, user: User, reactedUponMessage: VoteMessage, requestId: string, guildId: string) {
public async handleNoneOfThatVote(messageReaction: MessageReaction, reactedUponMessage: VoteMessage, requestId: string, guildId: string) {
if (!messageReaction.message.guild) return 'No guild'
const guild = messageReaction.message.guild
logger.debug(`${reactedUponMessage.id} is vote message`, { requestId, guildId })
const watcherRoleMember = await getMembersWithRoleFromGuild(config.bot.announcement_role, messageReaction.message.guild)
@ -44,52 +45,58 @@ export default class VoteController {
const watcherRoleMemberCount = watcherRoleMember.size
logger.info(`MEMBER COUNT: ${watcherRoleMemberCount}`, { requestId, guildId })
const noneOfThatReactions = messageReaction.message.reactions.cache.get(NONE_OF_THAT)?.users.cache.filter(x => x.id !== this.client.user?.id).size ?? 0
const noneOfThatReactions = reactedUponMessage.reactions.cache.get(NONE_OF_THAT)?.users.cache.filter(x => x.id !== this.client.user?.id).size ?? 0
const memberThreshold = (watcherRoleMemberCount / 2)
logger.info(`Reroll ${noneOfThatReactions} > ${memberThreshold} ?`, { requestId, guildId })
if (noneOfThatReactions > memberThreshold) {
if (noneOfThatReactions > memberThreshold)
logger.info(`No reroll`, { requestId, guildId })
else
logger.info('Starting poll reroll', { requestId, guildId })
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 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
const newMovies = await this.yavinJellyfinHandler.getRandomMovieNames(newMovieCount, guildId, requestId)
await this.handleReroll(reactedUponMessage, guild, guild.id, requestId)
}
public async handleReroll(voteMessage: VoteMessage, guild: Guild, guildId: string, requestId: string) {
// merge
const movies = newMovies
//get movies that already had votes to give them a second chance
magnetotail marked this conversation as resolved Outdated

Parameter die eine Message sind heißen oft "message", manchmal aber auch "msg". sollte vereinheitlicht sein

Parameter die eine Message sind heißen oft "message", manchmal aber auch "msg". sollte vereinheitlicht sein
66507cb08fa50ba3a7be28388c55b21227fb2261

fc64728a78

Jetzt sollten alle Occurences beseitigt sein.

fc64728a780f99b56aebff7f0a7c5d24a901d90d Jetzt sollten alle Occurences beseitigt sein.
const voteInfo = await this.parseVoteInfoFromVoteMessage(voteMessage, requestId)
// create new message
await this.closePoll(messageReaction.message.guild, 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`)
return
}
const sentMessage = await this.sendVoteMessage(message, movies.length, announcementChannel)
sentMessage.pin()
// get movies from jellyfin to fill the remaining slots
const newMovieCount = config.bot.random_movie_count - voteInfo.votes.filter(x => x.count > 2).length
const newMovies = await this.yavinJellyfinHandler.getRandomMovieNames(newMovieCount, guildId, requestId)
magnetotail marked this conversation as resolved Outdated

above WHAT threshold? What does it do??

above WHAT threshold? What does it do??
20da25f2bf9a473704f8b4660e5f05183679ba39

Mit einem Kommentar versehen und entsprechend deines vorschlags umbenannt

Mit einem Kommentar versehen und entsprechend deines vorschlags umbenannt
// merge
magnetotail marked this conversation as resolved Outdated

threshold seems to be a magic number

threshold seems to be a magic number

Or rename method to "hasAtLeastOneVote"

Or rename method to "hasAtLeastOneVote"
296a490e935cbdb79b70d73d2df9bc12a5774c53

done

done
const movies = newMovies.concat(voteInfo.votes.map(x => x.movie))
// create new message
await this.closePoll(guild, requestId)
const message = this.createVoteMessageText(guild.id, voteInfo.eventDate, movies, guildId, requestId)
magnetotail marked this conversation as resolved Outdated

comment is misleading, voteinfo is also used to get the eventid and eventdate in line 93

comment is misleading, voteinfo is also used to get the eventid and eventdate in line 93
119343c916b023a926e534575ae803cdec5b9594
const announcementChannel = this.client.getAnnouncementChannelForGuild(guildId)
if (!announcementChannel) {
logger.error(`No announcementChannel found for ${guildId}, can't post poll`)
return
magnetotail marked this conversation as resolved Outdated

maybe extract this if-else to a method to keep code more compact

maybe extract this if-else to a method to keep code more compact

7d794a8001
done

7d794a8001a66d068f949c893d689a068c3caeed done
}
logger.info(`No reroll`, { requestId, guildId })
const sentMessage = await this.sendVoteMessage(message, movies.length, announcementChannel)
sentMessage.pin()
}
private fetchEventStartDateByEventId(eventId: string, requestId: string): Date {
throw new Error("Method not implemented.")
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 })
if (guildEvent.scheduledStartAt)
return guildEvent.scheduledStartAt
}
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 {
public async parseVoteInfoFromVoteMessage(message: VoteMessage, requestId: string): Promise<VoteMessageInfo> {
const lines = message.cleanContent.split('\n')
let parsedIds = this.parseGuildIdAndEventIdFromWholeMessage(message.cleanContent)
let eventDate: Date = this.parseEventDateFromMessage(message.cleanContent)
magnetotail marked this conversation as resolved Outdated

is not a message, only messagetext

is not a message, only messagetext
a455fd8ff7e6b8ffb032fb4aed9389da68ee513b

done

done
if (!message.guild)
throw new Error(`Message ${message.id} not a guild message`)
let eventStartDate: Maybe<Date> = await this.fetchEventStartDateByEventId(message.guild, parsedIds.eventId, requestId)
if (!eventStartDate) eventStartDate = this.parseEventDateFromMessage(message.cleanContent, message.guild.id, requestId)
let votes: Vote[] = []
for (const line of lines) {
if (line.slice(0, 5).includes(':')) {
@ -107,18 +114,25 @@ export default class VoteController {
}
}
}
return <VoteMessageInfo>{ eventId: parsedIds.eventId, eventDate, votes }
return <VoteMessageInfo>{ eventId: parsedIds.eventId, eventDate: eventStartDate, votes }
}
public parseEventDateFromMessage(message: string): Date {
public parseEventDateFromMessage(message: string, guildId: string, requestId: string): Date {
logger.warn(`Falling back to RegEx parsing to get Event Date`, { guildId, requestId })
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) ?? ""
return new Date(dateFromResult)
}
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 createVoteMessageText(eventId: string, eventStartDate: Date, movies: string[], guildId: string, requestId: string): string {
let message = `[Abstimmung] für https://discord.com/events/${guildId}/${eventId} \n<@&${config.bot.announcement_role}> Es gibt eine neue Abstimmung für die nächste Watchparty ${createDateStringFromEvent(eventStartDate, guildId, requestId)}! Stimme hierunter für den nächsten Film ab!\n`
for (let i = 0; i < movies.length; i++) {