2016-07-15 119 views
0

我目前正在學習Node.js/JavaScript,以便使用Discordie庫編寫Discord bot。鏈接承諾執行兩個操作

我有兩個單獨的操作,一個創建一個邀請到服務器,另一個踢用戶併發送消息,如果他們在其中一條消息中使用了連線。

e.message.author.openDM().then(dm => dm.sendMessage(`You have been kicked from the **${e.message.guild.name}** server for using a slur. Please consider this a probation. When you feel that you are ready to not use that sort of language, feel free to rejoin us.`)); 
e.message.author.memberOf(e.message.guild).kick(); 

是我用來引導用戶的消息,然後踢他們的方法。我有生成邀請,並從所接收的JSON拉動邀請碼單獨的命令(!invite):

var generateInvite = e.message.channel.createInvite({"temporary": false, "xkcdpass": false}); 
generateInvite.then(function(res) { e.message.channel.sendMessage("https://discord.gg/" +res.code); }); 

我想能夠生成以發送一個直接消息碼的內部的邀請踢用戶邀請回來,如果他們能夠避免再次使用那種語言,但我想不出如何正確連鎖我公司承諾:

generateInvite.then(function(res) { return res.code }).then(e.message.author.openDM().then(function(dm){ dm.sendMessage(`You have been kicked from the **${e.message.guild.name}** server for using a slur. Please consider this a probation. When you feel that you are ready to not use that sort of language, feel free to rejoin us by following this link: https://discord.gg/` + res.code)})); 

我在哪裏有這個諾言鏈回事?

+0

_「我在哪裏出錯了這個承諾鏈?」_不應該包括'('at'.openMD()'來引用函數'openDM',而不是立即調用函數,或者在'e .message.author.openDM()'應該從一個匿名函數返回;目前看起來是一個語法錯誤? – guest271314

+0

請縮進你的代碼,這很難在一行上讀取 – Tdy

回答

1

應該

const author = e.message.author; 
generateInvite.then(function(res) { 
    author.openDM().then(function(dm){ 
     dm.sendMessage(`… link: https://discord.gg/${res.code}.`); 
     author.memberOf(e.message.guild).kick(); 
    }) 
}); 

不要return res.code走不通,不傳遞一個回調的地方承諾(openDM().then(…))。

此外,您也許只想在向他發送消息後纔會踢出用戶,因此請確保這兩個操作正確排序。

您也可能想要考慮創建邀請並打開並行的dm頻道,使用Promise.all等待兩個承諾,然後在單個回調中使用它們的結果。

+0

謝謝!DM'ing _before_ kick。現在我知道如何處理未來的承諾:) – Tadhg