Reputation: 494
I'm currently learning Node.js/JavaScript in general in order to write a Discord bot using the Discordie library.
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:
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?
Upvotes: 0
Views: 329
Reputation: 664185
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.
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.
Upvotes: 1