I am trying to get the owner of all the guilds my bot is in, so I can send a newsletter to them. So far I have this:
const guilds = message.client.guilds.cache.map(guild => guild);
guilds.forEach(guild => {
const owner = message.client.users.cache.get(guild.ownerId)
owner.send({embeds: [newsembed]})
.catch(console.error());
});
which gets the guilds, then gets the owner id, then gets the owner from the id in the cache. Now normally I would use .fetchOwner()
but I cannot use await it in a forEach loop, but if you have a suggestion for that please do tell. When running the code listed above I get the error:
owner.send({embeds: [newsembed]})
^
TypeError: Cannot read properties of undefined (reading 'send')
I am not sure why I get this error, maybe the owner is not in the cache? I have tried fetching them in a forEach loop but I cant use await guild.fetchOwner()
in forEach loops. I tried putting the forEach loop in an async function, same problem. Any help is appreciated, thanks!
CodePudding user response:
You can make the forEach()
loop asynchronous
const guilds = message.client.guilds.cache.map(guild => guild);
guilds.forEach(async guild => {
const owner = await guild.fetchOwner();
owner.send({embeds: [newsembed]})
.catch(console.error());
});