I have a command which sends a message to every user in the guild. Of course there is a possibility where users have DM's turned off. I want to count those users (And send a message to the channel with the amount of users having it disabled) by using .catch
.
My problem is, the .catch block performs itself after the rest of the command (The part where it sends a message to the channel)
In the .catch
block I add 1 to a variable, every time it gives me the error.
In the channel message, I send the variable. Obviously, the variable will be 0 since it sends the message before it runs the .catch
block.
How do I send the message with the amount of users that have it turned off, after the .catch
block?
This is my code:
var text = args.join(' ')
message.guild.members.forEach(member => {
member.send(text).catch(() => {
console.log("Can't send DM to this user!")
faultyusers++
});
});
console.log(faultyusers);
message.channel.send("Successfully sent message to all members in the server. (Warning: **" + faultyusers + "** users might have not received the message because of their settings.)")
(faultyusers is always 0 when I run this.)