[英]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.