[英]How to add a function to a function
我目前有一个 cron 作业 function 每天早上 7:00 发送一条消息,它工作得很好,但现在我希望它从 Trello 卡中获取消息并且 Trello 部分工作得很好,但是当我把它们放在一起时不起作用。 有任何想法吗? 这是我到目前为止所拥有的:
var job = new CronJob('01 15 18 * * *', function() {
let listID = "5ec3fda8a26c384e44f063bb";
trello.getCardsOnListWithExtraParams(listID, "pos:top",
function (error, trelloCard) {
console.log('Found card:', trelloCard[0].name);
})
const wyrEmbed = new discord.RichEmbed()
.setTitle("❓**WOULD YOU RATHER**❓")
.addField(
"**THE WOULD YOU RATHER**", "test: " + trelloCard[0].name)
.setDescription(
"🌞 Top o' the mornin’ to ya! All throughout the night, I’ve been preparing a would you rather question for all of you! I can’t wait to see all of the answers you guys think of! "
)
.setTimestamp()
.setFooter("Munchies Ice Cream", client.user.avatarURL)
.setThumbnail("https://t4.rbxcdn.com/366f4da2e1924b6e4b0816086b485f05")
.setColor("7ab7e2");
const wyrthing = client.channels.get("688177088573997066")
wyrthing.send(wyrEmbed)
console.log('You will see this message every second')
},
null,
true,
'America/New_York'
);
})
您的代码基本上尝试这样做(按此顺序):
这是因为trello.getCardsOnListWithExtraParams
是一个异步 function。 您将结果trelloCard
作为回调 function 的参数,但是当您将字段添加到嵌入时,您试图在回调之外访问它。
把它放在你的 cron function 中:
let listID = "5ec3fda8a26c384e44f063bb";
trello.getCardsOnListWithExtraParams(listID, "pos:top", function (error, trelloCard) {
console.log('Found card:', trelloCard[0].name);
const wyrEmbed = new discord.RichEmbed()
.setTitle("❓**WOULD YOU RATHER**❓")
.addField("**THE WOULD YOU RATHER**", "test: " + trelloCard[0].name)
.setDescription(
"🌞 Top o' the mornin’ to ya! All throughout the night, I’ve been preparing a would you rather question for all of you! I can’t wait to see all of the answers you guys think of! "
)
.setTimestamp()
.setFooter("Munchies Ice Cream", client.user.avatarURL)
.setThumbnail("https://t4.rbxcdn.com/366f4da2e1924b6e4b0816086b485f05")
.setColor("7ab7e2");
const wyrthing = client.channels.get("688177088573997066")
wyrthing.send(wyrEmbed)
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.