I would like to send a message to all channels that are in all servers that are called “labycheck-shop” at 12 midnight. I have already tried this but it did not work:
JavaScript
x
7
1
cron.schedule('0 0 * * *', () => {
2
const channel = client.channels.cache.find(ch => ch.name === 'labycheck-shop');
3
channel.send('test')
4
})```
5
6
// (cron is node-cron)
7
Advertisement
Answer
This feature can turn out to be spammy on the Discord API, so you might want to keep that in mind
I believe that you might be wanting to use
<Collection>.filter
instead of<Collection>.find
. The find function in the Discord.js Collections works pretty much like<Array>.find
, which returns the first result it gets true for. So what you’ll want to do is, use<Collection>.filter
to filter the guilds of that name, and then Loop through the array and send the message.
So kinda like this
JavaScript
1
4
1
cron.schedule('0 0 * * *', () => {
2
client.channels.cache.filter((ch) => ch.name === "labycheck-shop").forEach((channel) => channel.send("Ding dong it's 12 AM"));
3
});
4
That’s how it should work out, I also checked the cron expression, and that looks fine as well