Skip to content
Advertisement

How to send a message to a Channel on all Servers

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:

cron.schedule('0 0 * * *', () => {
  const channel = client.channels.cache.find(ch => ch.name === 'labycheck-shop');
  channel.send('test')
})```

// (cron is node-cron)

Advertisement

Answer

  1. This feature can turn out to be spammy on the Discord API, so you might want to keep that in mind

  2. 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

cron.schedule('0 0 * * *', () => {
    client.channels.cache.filter((ch) => ch.name === "labycheck-shop").forEach((channel) => channel.send("Ding dong it's 12 AM"));
});

That’s how it should work out, I also checked the cron expression, and that looks fine as well

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement