announcement for all server bot is in 1

announcement for all server bot is in

bot.guilds.cache.forEach((guild) => {
 console.log(guild.name);

 //This would make the client to send a message "Announcement!" to a channel named "news" in every server, if such a channel didn't exist in a guild, the message wouldn't be sent for that exact guild
 try {
  guild.channels.cache.find((x) => x.name == 'news').send('Announcement!');
 } catch {
  //If message couldn't be sent
  console.log("Message wasn't sent for " + guild.name);
 }
});

Here is what the above code is Doing:
1. It’s looping through all the guilds the bot is in
2. It’s logging the name of the guild
3. It’s trying to send a message to a channel named “news” in every guild
4. If the channel doesn’t exist, it’s logging that the message wasn’t sent for that guild

Similar Posts