#new Date()
30 messages · Page 1 of 1 (latest)
You can either poll this, e.g. every 5s or use setTimeout() when your bot starts
How would I go about polling it?
Also with this, would i keep what I have stated within query?
Cron jobs or setInterval() are the easiest ways
Would I keep this:
const query = {
expires: { $1t: new Date() },
}
I assume yes
Are you polling or using setTimeout()?
I have setTimeout already set up
so setTimeout
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
I think you're overcomplicating this and this is actually polling.
aah. Also again this wasn't really my code most of it was copyed
If you still want to poll I would instead use setInterval
It basically runs a specific function every n milliseconds
okay, also would my above code be okay or would I need to include what you showed above
you can just slightly modify your 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 (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
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?
I haven't used $1t before in my code so I most likely just copied it
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
I'm sorry but i really would like to get this done, any chance you can help?
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
I personally would use epoch times because the current epoch time is always going to be larger than past epoch timestamps