2016-07-15 116 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