import { TextChannel } from "discord.js" ; import { TundraBot } from "../base/TundraBot" ; import Poll from "../commands/utility/poll" ; import { DBPoll , pollModel } from "../models/Poll" ; import Deps from "../utils/deps" ; import Logger from "../utils/logger" ; import { StartupHelper } from "./startupHelper" ; export default class CheckEvents extends StartupHelper { DBPollManager : DBPoll ; constructor ( client ) { super ( client ) ; this . DBPollManager = Deps . get < DBPoll > ( DBPoll ) ; } async init ( ) { pollModel . find ( ) . then ( async ( polls ) => { const dateNow = new Date ( Date . now ( ) ) ; for ( const poll of polls ) { try { if ( ! this . client . guilds . cache . has ( poll . guildID ) ) { throw new Error ( `Guild was deleted? (${ poll . guildID })` ) ; } const channel = await this . client . channels . fetch ( poll . channelID ) . catch ( ( ) => { throw new Error ( `Channel was deleted? (${ poll . channelID })` ) ; } ) as TextChannel ; await channel . messages . fetch ( poll . messageID ) . catch ( ( ) => { throw new Error ( "Poll was deleted manually" ) ; } ) ; if ( poll . endTime > dateNow ) { setTimeout ( ( ) => { Poll . pollHandleFinish ( this . client , poll ) ; } , poll . endTime . valueOf ( ) - dateNow . valueOf ( ) ) ; } else { Poll . pollHandleFinish ( this . client , poll ) ; } } catch ( err ) { Logger . log ( "warn" , `Removing poll (messageID: ${ poll . messageID }) (guildID: ${ poll . guildID }) from database:\n${ err }` ) ; this . DBPollManager . delete ( poll ) ; } } Logger . log ( "ready" , `Loaded ${ polls . length } polls` ) ; } ) . catch ( ( err ) => { Logger . log ( "error" , `Error loading polls in CheckPolls:\n${ err }` ) ; } ) ; } }