#new Date()

30 messages · Page 1 of 1 (latest)

cedar imp
#

if (query.expires.getTime() <= Date.now()) // ...

#

You can either poll this, e.g. every 5s or use setTimeout() when your bot starts

analog sphinx
analog sphinx
cedar imp
#

Cron jobs or setInterval() are the easiest ways

analog sphinx
#

Would I keep this:

const query = {
        expires: { $1t: new Date() },
    }
#

I assume yes

cedar imp
#

Are you polling or using setTimeout()?

analog sphinx
#

I have setTimeout already set up

#

so setTimeout

cedar imp
#

and you run that when your bot starts?

analog sphinx
# cedar imp and you run that when your bot starts?

I'm unsure, I copied most of this as it is quite far out of my comfort zone but here is the code:

const Contract = require('../schemas/contract')

module.exports = (client) => {
    

const check = async () => {
    const query = {
        expires: { $1t: new Date() },
    }
    const results = await Contract.find(query)
    
    for (const result of results) {
        const {guildId, userId, type} = result
        const guild = await client.guilds.fetch(guildId)
        if (!guild) { 
            console.log(`Guild "${guildId}" no longer uses this bot`)
            continue
        }
        
        if (type === 'contract'){
            const contractRole = guild.roles.cache.find((role) => role.name === 'Contract')
            if (!contractRole){
                console.log(`Guild "${guildId}" has no "Contract" role`)
                continue
            }

            const member = guild.members.cache.get(userId)
            if (!member) {
                continue
            }
            member.roles.remove(contractRole)
        }
    }

    await Contract.deleteMany(query)

    setTimeout(check, 1000 * 10)
    console.log('Expired contracts checked')
    }    
    check()
}
#

It's in a seperate file

cedar imp
#

I think you're overcomplicating this and this is actually polling.

analog sphinx
cedar imp
#

If you still want to poll I would instead use setInterval

#

It basically runs a specific function every n milliseconds

analog sphinx
cedar imp
#

you can just slightly modify your code

analog sphinx
#
const Contract = require('../schemas/contract')

module.exports = (client) => {
    

const check = async () => {
    const query = {
        expires: { $1t: new Date() },
    }
    const results = await Contract.find(query)
    
    for (const result of results) {
        const {guildId, userId, type} = result
        const guild = await client.guilds.fetch(guildId)
        if (!guild) { 
            console.log(`Guild "${guildId}" no longer uses this bot`)
            continue
        }
    }
    if (query.expires.getTime() <= Date.now()){
    await Contract.deleteMany(query)
    }

    setInterval(check, 1000 * 5)
    console.log('Expired contracts checked')
    }    
    check()
}

I deleted some of the code, but nothing is being executed. (So sorry!)

#

also ik the remove role is gone

cedar imp
#

I would get rid of the recursive check() call inside your function

#

And have your setInterval() outside of the check function

#

Also how are you mapping $1t to the result?

analog sphinx
#
const Contract = require('../schemas/contract')

module.exports = (client) => {
    

const check = async () => {
    const query = {
        expires: { $1t: new Date() },
    }
    const results = await Contract.find(query)
    
    for (const result of results) {
        const {guildId, userId, type} = result
        const guild = await client.guilds.fetch(guildId)
        if (!guild) { 
            console.log(`Guild "${guildId}" no longer uses this bot`)
            continue
        }
    }
    if (query.expires.getTime() <= Date.now()){
    await Contract.deleteMany(query)
    }

    
    console.log('Expired contracts checked')
    }    
}
setInterval(check, 1000 * 5)

Updated code

analog sphinx
boreal light
#

Stop pinging them. If they want to help they will. Nobody’s obligated to help here, everyone is a volunteer. And if you don’t ping someone but instead state that as a open question chances are higher that another person leans in to help you

fierce leaf
#

I personally would use epoch times because the current epoch time is always going to be larger than past epoch timestamps