繁体   English   中英

如何在 discord.js v13 中获取所有公会的所有者?

[英]How do I get the owners of all the guilds in discord.js v13?

我正在尝试获取我的机器人所在的所有公会的所有者,以便我可以向他们发送时事通讯。 到目前为止,我有这个:

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());
});

它获取公会,然后获取所有者 id,然后从缓存中的 id 获取所有者。 现在通常我会使用.fetchOwner()但我不能在 forEach 循环中使用 await 它,但是如果您对此有建议,请告诉我。 运行上面列出的代码时,出现错误:

            owner.send({embeds: [newsembed]})
                  ^

TypeError: Cannot read properties of undefined (reading 'send')

我不确定为什么会出现此错误,也许所有者不在缓存中? 我试过在 forEach 循环中获取它们,但我不能在 forEach 循环中使用await guild.fetchOwner() 我尝试将 forEach 循环放在异步函数中,同样的问题。 任何帮助表示赞赏,谢谢!

您可以使forEach()循环异步

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());
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM