move scheduling of pollclose task to startup
All checks were successful
Build a docker image for node-jellyfin-role-bot / build-docker-image (push) Successful in 1m37s
All checks were successful
Build a docker image for node-jellyfin-role-bot / build-docker-image (push) Successful in 1m37s
Also moved check function to closepoll.ts
This commit is contained in:
@ -1,10 +1,8 @@
|
||||
import { addDays, format, isAfter } from "date-fns";
|
||||
import toDate from "date-fns/fp/toDate";
|
||||
import { format } from "date-fns";
|
||||
import { GuildScheduledEvent, Message, MessageCreateOptions, TextChannel } from "discord.js";
|
||||
import { ScheduledTask, schedule } from "node-cron";
|
||||
import { ScheduledTask } from "node-cron";
|
||||
import { v4 as uuid } from "uuid";
|
||||
import { client, yavinJellyfinHandler } from "../..";
|
||||
import { closePoll } from "../commands/closepoll";
|
||||
import { config } from "../configuration";
|
||||
import { Maybe } from "../interfaces";
|
||||
import { logger } from "../logger";
|
||||
@ -59,46 +57,7 @@ export async function execute(event: GuildScheduledEvent) {
|
||||
sentMessage.react(Emotes[i])
|
||||
}
|
||||
|
||||
if (!task) {
|
||||
task = schedule("0 * * * * *", () => checkForPollsToClose(event))
|
||||
}
|
||||
|
||||
// sentMessage.pin() //todo: uncomment when bot has permission to pin messages. Also update closepoll.ts to only fetch pinned messages
|
||||
}
|
||||
}
|
||||
|
||||
async function checkForPollsToClose(event: GuildScheduledEvent): Promise<void> {
|
||||
const requestId = uuid()
|
||||
logger.info(`Automatic check for poll closing.`, { guildId: event.guildId, requestId })
|
||||
if (!event.guild) {
|
||||
logger.error("No guild in event. Cancelling.", { guildId: event.guildId, requestId })
|
||||
return
|
||||
}
|
||||
//refetch event in case the time changed or the poll is already closed
|
||||
const events = (await event.guild.scheduledEvents.fetch())
|
||||
.filter(event => event.name.toLowerCase().includes("voting offen"))
|
||||
.map((value) => value)
|
||||
|
||||
if (!events || events.length <= 0) {
|
||||
logger.info("Did not find any events. Cancelling", { guildId: event.guildId, requestId })
|
||||
return
|
||||
} else if (events.length > 1) {
|
||||
logger.error(`More than one event found. Don't know which one is the right one :( Events: ${JSON.stringify(events, null, 2)}`, { guildId: event.guildId, requestId })
|
||||
return
|
||||
}
|
||||
const updatedEvent = events[0] //add two hours because of different timezones in discord api and Date.now()
|
||||
if (!updatedEvent.scheduledStartTimestamp) {
|
||||
logger.error("Event does not have a scheduled start time. Cancelling", { guildId: event.guildId, requestId })
|
||||
return
|
||||
}
|
||||
|
||||
const eventDate: Date = toDate(updatedEvent.scheduledStartTimestamp)
|
||||
const closePollDate: Date = addDays(eventDate, -2)
|
||||
|
||||
if (isAfter(Date.now(), closePollDate)) {
|
||||
logger.info("Less than two days until event. Closing poll", { guildId: event.guildId, requestId })
|
||||
closePoll(event.guild, requestId)
|
||||
} else {
|
||||
logger.info(`ScheduledStart: ${closePollDate}. Now: ${toDate(Date.now())}`, { guildId: event.guildId, requestId })
|
||||
}
|
||||
}
|
@ -27,6 +27,7 @@ export async function execute(oldEvent: GuildScheduledEvent, newEvent: GuildSche
|
||||
if (!members.find(x => x.id == member.id))
|
||||
members.push(member)
|
||||
}
|
||||
|
||||
|
||||
if (newEvent.status === GuildScheduledEventStatus.Active)
|
||||
createJFUsers(members, newEvent.name, requestId)
|
||||
|
Reference in New Issue
Block a user