嘿,我想在持续时间结束foreach后向我的机器人发送一条消息
代码:
let beforecheck = new Date();
bot.guilds.forEach(guild => {
//my foreach code
}).then(() => {
let aftercheck = new Date();
var finished = new Discord.RichEmbed()
.setDescription("✔ Finished ✔")
.addField("? Bot Latency", bot.ping + " ms")
.addField("expected time", expectedtime + " minute(s)")
.addField("final time", (60000 / (beforecheck - aftercheck)) + " minute(s)")
message.channel.send(finished)
})
感谢您的答复
答案 0 :(得分:1)
从您的代码看来,您似乎希望forEach
花费大量时间,并且由于您要遍历guild
s这一事实,所以我期望其中涉及某种异步Discord API调用。如果两者都正确,则forEach
是该工作的错误工具。
如果订单很重要 :(在async function
中)
let beforecheck = new Date();
for(const guild of bot.guilds) { //might need bot.guilds.array()
//use await on asynchronous calls
}
let aftercheck = new Date();
var finished = new Discord.RichEmbed()
.setDescription("✔ Finished ✔")
.addField("? Bot Latency", bot.ping + " ms")
.addField("expected time", expectedtime + " minute(s)")
.addField("final time", (60000 / (beforecheck - aftercheck)) + " minute(s)")
message.channel.send(finished)
如果订单无关紧要:
let beforecheck = new Date();
await Promise.all(bot.guilds.map(guild => {
//return a promise or use async/await here
}); //can also use .then() here but async/await is better
let aftercheck = new Date();
var finished = new Discord.RichEmbed()
.setDescription("✔ Finished ✔")
.addField("? Bot Latency", bot.ping + " ms")
.addField("expected time", expectedtime + " minute(s)")
.addField("final time", (60000 / (beforecheck - aftercheck)) + " minute(s)")
message.channel.send(finished)