Check if less than 2 days between create and start for deciding if to close
All checks were successful
Build a docker image for node-jellyfin-role-bot / build-docker-image (push) Successful in 1m9s

This commit is contained in:
Sammy 2023-06-17 13:18:52 +02:00
parent 4cc332820f
commit 670a64af22

View File

@ -1,4 +1,4 @@
import { addDays, format, isAfter, toDate } from 'date-fns'
import { addDays, differenceInDays, format, isAfter, toDate } from 'date-fns'
import { Guild, GuildScheduledEvent, GuildScheduledEventEditOptions, GuildScheduledEventSetStatusArg, GuildScheduledEventStatus, Message, MessageCreateOptions, TextChannel } from 'discord.js'
import { v4 as uuid } from 'uuid'
import { client } from '../..'
@ -34,7 +34,7 @@ export async function closePoll(guild: Guild, requestId: string) {
logger.info("stopping poll", { guildId, requestId })
const announcementChannel: Maybe<TextChannel> = client.getAnnouncementChannelForGuild(guildId)
if(!announcementChannel) {
if (!announcementChannel) {
logger.error("Could not find the textchannel. Unable to close poll.", { guildId, requestId })
return
}
@ -57,14 +57,14 @@ export async function closePoll(guild: Guild, requestId: string) {
const votes = await (await getVotesByEmote(lastMessage, guildId, requestId))
.sort((a, b) => b.count - a.count)
.sort((a, b) => b.count - a.count)
logger.debug(`votes: ${JSON.stringify(votes, null, 2)}`, { guildId, requestId })
logger.info("Deleting vote message")
await lastMessage.delete()
const event = await getEvent(guild, guild.id, requestId)
if(event) {
if (event) {
updateEvent(event, votes, guild, guildId, requestId)
sendVoteClosedMessage(event, votes[0].movie, guildId, requestId)
}
@ -82,7 +82,7 @@ async function sendVoteClosedMessage(event: GuildScheduledEvent, movie: string,
}
const announcementChannel = client.getAnnouncementChannelForGuild(guildId)
logger.info("Sending vote closed message.", { guildId, requestId })
if(!announcementChannel) {
if (!announcementChannel) {
logger.error("Could not find announcement channel. Please fix!", { guildId, requestId })
return
}
@ -151,10 +151,10 @@ export async function checkForPollsToClose(guild: Guild): Promise<void> {
const requestId = uuid()
logger.info(`Automatic check for poll closing.`, { guildId: guild.id, requestId })
const events = (await guild.scheduledEvents.fetch()).filter(event => event.name.toLocaleLowerCase().includes("voting offen")).map(event => event)
if(events.length > 1) {
if (events.length > 1) {
logger.error("Handling more than one Event is not implemented yet. Found more than one poll to close")
return
} else if(events.length == 0) {
} else if (events.length == 0) {
logger.info("Could not find any events. Cancelling", { guildId: guild.id, requestId })
}
@ -165,14 +165,14 @@ export async function checkForPollsToClose(guild: Guild): Promise<void> {
}
const createDate: Date = toDate(updatedEvent.createdTimestamp)
const closePollMinDate: Date = addDays(createDate, 1)
const eventDate: Date = toDate(updatedEvent.scheduledStartTimestamp)
const difference: number = differenceInDays(createDate, eventDate)
if(isAfter(closePollMinDate, Date.now())) {
logger.info("Event is less than 24h old. Not closing poll!", { guildId: guild.id, requestId })
if (difference <= 2) {
logger.info("Less than two days between event create and event start. Not closing poll.", { guildId: guild.id, requestId })
return
}
const eventDate: Date = toDate(updatedEvent.scheduledStartTimestamp)
const closePollDate: Date = addDays(eventDate, -2)
if (isAfter(Date.now(), closePollDate)) {