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
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:
parent
4cc332820f
commit
670a64af22
@ -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 { Guild, GuildScheduledEvent, GuildScheduledEventEditOptions, GuildScheduledEventSetStatusArg, GuildScheduledEventStatus, Message, MessageCreateOptions, TextChannel } from 'discord.js'
|
||||||
import { v4 as uuid } from 'uuid'
|
import { v4 as uuid } from 'uuid'
|
||||||
import { client } from '../..'
|
import { client } from '../..'
|
||||||
@ -23,7 +23,7 @@ export default new Command({
|
|||||||
}
|
}
|
||||||
const guildId = command.guildId
|
const guildId = command.guildId
|
||||||
logger.info("Got command for closing poll!", { guildId, requestId })
|
logger.info("Got command for closing poll!", { guildId, requestId })
|
||||||
|
|
||||||
command.followUp("Alles klar, beende die Umfrage :)")
|
command.followUp("Alles klar, beende die Umfrage :)")
|
||||||
closePoll(command.guild, requestId)
|
closePoll(command.guild, requestId)
|
||||||
}
|
}
|
||||||
@ -34,7 +34,7 @@ export async function closePoll(guild: Guild, requestId: string) {
|
|||||||
logger.info("stopping poll", { guildId, requestId })
|
logger.info("stopping poll", { guildId, requestId })
|
||||||
|
|
||||||
const announcementChannel: Maybe<TextChannel> = client.getAnnouncementChannelForGuild(guildId)
|
const announcementChannel: Maybe<TextChannel> = client.getAnnouncementChannelForGuild(guildId)
|
||||||
if(!announcementChannel) {
|
if (!announcementChannel) {
|
||||||
logger.error("Could not find the textchannel. Unable to close poll.", { guildId, requestId })
|
logger.error("Could not find the textchannel. Unable to close poll.", { guildId, requestId })
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -55,16 +55,16 @@ export async function closePoll(guild: Guild, requestId: string) {
|
|||||||
|
|
||||||
logger.debug(`Last message: ${JSON.stringify(lastMessage, null, 2)}`, { guildId, requestId })
|
logger.debug(`Last message: ${JSON.stringify(lastMessage, null, 2)}`, { guildId, requestId })
|
||||||
|
|
||||||
|
|
||||||
const votes = await (await getVotesByEmote(lastMessage, guildId, requestId))
|
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.debug(`votes: ${JSON.stringify(votes, null, 2)}`, { guildId, requestId })
|
||||||
|
|
||||||
logger.info("Deleting vote message")
|
logger.info("Deleting vote message")
|
||||||
await lastMessage.delete()
|
await lastMessage.delete()
|
||||||
const event = await getEvent(guild, guild.id, requestId)
|
const event = await getEvent(guild, guild.id, requestId)
|
||||||
if(event) {
|
if (event) {
|
||||||
updateEvent(event, votes, guild, guildId, requestId)
|
updateEvent(event, votes, guild, guildId, requestId)
|
||||||
sendVoteClosedMessage(event, votes[0].movie, 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)
|
const announcementChannel = 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 })
|
logger.error("Could not find announcement channel. Please fix!", { guildId, requestId })
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -151,10 +151,10 @@ export async function checkForPollsToClose(guild: Guild): Promise<void> {
|
|||||||
const requestId = uuid()
|
const requestId = uuid()
|
||||||
logger.info(`Automatic check for poll closing.`, { guildId: guild.id, requestId })
|
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)
|
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")
|
logger.error("Handling more than one Event is not implemented yet. Found more than one poll to close")
|
||||||
return
|
return
|
||||||
} else if(events.length == 0) {
|
} else if (events.length == 0) {
|
||||||
logger.info("Could not find any events. Cancelling", { guildId: guild.id, requestId })
|
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 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())) {
|
if (difference <= 2) {
|
||||||
logger.info("Event is less than 24h old. Not closing poll!", { guildId: guild.id, requestId })
|
logger.info("Less than two days between event create and event start. Not closing poll.", { guildId: guild.id, requestId })
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const eventDate: Date = toDate(updatedEvent.scheduledStartTimestamp)
|
|
||||||
const closePollDate: Date = addDays(eventDate, -2)
|
const closePollDate: Date = addDays(eventDate, -2)
|
||||||
|
|
||||||
if (isAfter(Date.now(), closePollDate)) {
|
if (isAfter(Date.now(), closePollDate)) {
|
||||||
|
Loading…
Reference in New Issue
Block a user