Reputation: 1616
I have the below cron job scheduled to run every minute to update a time in an embed.
cron.schedule('* * * * *', function () {
con.query(`SELECT * FROM events WHERE closed = 'No'`, (err, rows) => {
if (err) throw err
rows.slice().forEach(row => {
const channel = row.channel
const messageID = row.messageID
bot.channels.cache.get(channel).messages.fetch(messageID).then(message => {
const embed = message.embeds[0]
let now = Date.now()
const targetDate = new Date(`${row.event_date} ${row.event_time}:00`)
var difference = (targetDate - now) / 1000;
var days = Math.floor(difference / (3600 * 24));
var hours = Math.floor((difference - (days * (3600 * 24))) / 3600);
var minutes = Math.floor((difference - (days * (3600 * 24)) - (hours * 3600)) / 60);
let timeTill;
if (days > 0) {
timeTill = `${days} Days ${hours} Hours ${minutes} Min`
} else if (days === 0 && hours > 0) {
timeTill = `${hours} Hours ${minutes} Min`
} else if (days === 0 && hours === 0) {
timeTill = `${minutes} Min`
} else if (difference <= 0) {
timeTill = `Event Started - get with <@!${row.hostID}> to join up in-game.`
}
embed.fields[9] = {
name: 'Starts in:',
value: `${timeTill}`,
inline: false
}
message.edit(new Discord.MessageEmbed(embed));
})
})
})
})
Works just fine but I want to take the work load off my website server and just have the bot search for the information needed. Here is what I have so far but it doesn't do anything. Doesn't error, don't update the embed. Nothing. Pretty sure I'm overlooking something and just need another set of eyes to tell me what that is.
cron.schedule('* * * * *', function () {
bot.channels.cache.filter((channel) => channel.name.endsWith('events')).forEach((channel) => {
channel.messages.cache.filter((message) => {
const embed = message.embeds[0]
let messageID;
let event_date;
let event_time;
if (!embed) {
return
} else {
if (embed.color === 65280) {
messageID = message.id
event_date = message.embeds[0].fields[0].value
event_time = message.embeds[0].fields[1].value
} else {
return
}
}
let now = Date.now()
const targetDate = new Date(`${event_date} ${event_time}:00`)
var difference = (targetDate - now) / 1000;
var days = Math.floor(difference / (3600 * 24));
var hours = Math.floor((difference - (days * (3600 * 24))) / 3600);
var minutes = Math.floor((difference - (days * (3600 * 24)) - (hours * 3600)) / 60);
let timeTill;
if (days > 0) {
timeTill = `${days} Days ${hours} Hours ${minutes} Min`
} else if (days === 0 && hours > 0) {
timeTill = `${hours} Hours ${minutes} Min`
} else if (days === 0 && hours === 0) {
timeTill = `${minutes} Min`
} else if (difference <= 0) {
timeTill = `Event Started - get with <@!${row.hostID}> to join up in-game.`
}
embed.fields[9] = {
name: 'Starts in:',
value: `${timeTill}`,
inline: false
}
message.edit(new Discord.MessageEmbed(embed))
})
})
})
Upvotes: 0
Views: 113
Reputation: 1616
Final code:
cron.schedule('* * * * *', function () {
bot.channels.cache.filter((channel) => channel.name.endsWith('events')).forEach((channel) => {
channel.messages.fetch({
limit: 100
}).then(messages => {
messages.forEach(message => {
if (message.embeds.length > 0) {
const embed = message.embeds[0]
let messageID;
let event_date;
let event_time;
if (embed.color === 65280) {
messageID = message.id
event_date = message.embeds[0].fields[0].value
event_time = message.embeds[0].fields[1].value.slice(0, -4)
let now = Date.now()
const targetDate = new Date(`${event_date} ${event_time}:00`)
var difference = (targetDate - now) / 1000;
var days = Math.floor(difference / (3600 * 24));
var hours = Math.floor((difference - (days * (3600 * 24))) / 3600);
var minutes = Math.floor((difference - (days * (3600 * 24)) - (hours * 3600)) / 60);
let timeTill;
if (days > 0) {
timeTill = `${days} Days ${hours} Hours ${minutes} Min`
} else if (days === 0 && hours > 0) {
timeTill = `${hours} Hours ${minutes} Min`
} else if (days === 0 && hours === 0) {
timeTill = `${minutes} Min`
} else if (difference <= 0) {
timeTill = `Event Started - get with <@!${row.hostID}> to join up in-game.`
}
embed.fields[9] = {
name: 'Starts in:',
value: `${timeTill}`,
inline: false
}
message.edit(new Discord.MessageEmbed(embed)).catch(error => {
console.error(error);
})
console.log(`Event ${message.embeds[0].author.name} has been edited. New start time is ${timeTill}.`)
} else {
return
}
} else {
return
}
})
})
})
})
Upvotes: 0
Reputation: 2837
The problem is that the messages you are trying to find might not be cached. The channel.messages.cache
will only contain messages that were sent after the bot was started. If you want to go through older messages, you will need to fetch the messages. You might want to take a look at my post about fetching messages until a specific date.
Upvotes: 1