简体   繁体   English

连锁承诺执行两项行动

[英]Chaining Promises to perform two actions

I'm currently learning Node.js/JavaScript in general in order to write a Discord bot using the Discordie library. 我目前正在总体上学习Node.js / JavaScript,以便使用Discordie库编写Discord机器人。

I have two separate actions, one creates an invitation to the server, and the other kicks the user and sends them a message if they have used a slur in one of their messages. 我有两个单独的动作,一个动作创建到服务器的邀请,另一个动作踢用户并向他们发送一条消息(如果他们在其中一条消息中使用了诽谤)。

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();

is the method that I'm using to direct message the user, then kick them. 是我用来引导用户信息,然后踢他们的方法。 I have a separate command ( !invite ) that generates an invite and pulls the invite code from the received json: 我有一个单独的命令( !invite ),该命令生成邀请并从接收到的json中提取邀请代码:

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

I would like to be able to generate an invite inside of the direct message code in order to send a kicked user an invite to come back if they can avoid using that sort of language again, however I can't figure out how to properly chain my Promises: 我希望能够在直接消息代码内生成邀请,以便向被踢用户发送邀请,让他们回来,如果他们可以避免再次使用这种语言的话,但是我不知道如何正确地进行链接我的承诺:

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)}));

Where am I going wrong with this promise chain? 这个承诺链哪里出问题了?

It should be 它应该是

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();
    })
});

Don't return res.code to nowhere, and don't pass a promise ( openDM().then(…) ) in the place of a callback. 不要return res.code到任何地方,也不要在回调的地方传递诺言( openDM().then(…) )。

Also you probably want to kick the user only after sending him the message, so make sure the two actions are properly sequenced. 另外,您可能只想在向用户发送消息后才踢它,因此请确保正确地对这两个操作进行排序。

You also might want to consider creating the inviting and opening the dm channel in parallel, use Promise.all to wait for the two promises and then use their results in a single callback. 您可能还需要考虑并行创建邀请和打开dm通道,使用Promise.all等待两个promise,然后在单个回调中使用它们的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM