Unable to DM User despite open DMs

code:
try {
let userData = client.users.cache.get(user.id);
console.log(userData)
// Check if user data is available and if the user allows DMs
if (userData && userData.dmChannel) {
userData.send({
content: 'You have been referred in a post by <@!' + interaction.user.id + '>. To report this Referral click the Report button in this message',
embeds: [embed],
});
} else {
// Handle the case where DMs are disabled or the user blocked the bot
console.log(`Failed to send a message to user ${user.id}.`);
errorIds.push({id:user.id, reason: 'Closed DMs'});
}
} catch (e) {
// Handle other potential errors
console.error(`Error while sending a message to user ${user.id}:`, e);
errorIds.push({id:user.id, reason: "User doesn't Exist"});
}
try {
let userData = client.users.cache.get(user.id);
console.log(userData)
// Check if user data is available and if the user allows DMs
if (userData && userData.dmChannel) {
userData.send({
content: 'You have been referred in a post by <@!' + interaction.user.id + '>. To report this Referral click the Report button in this message',
embeds: [embed],
});
} else {
// Handle the case where DMs are disabled or the user blocked the bot
console.log(`Failed to send a message to user ${user.id}.`);
errorIds.push({id:user.id, reason: 'Closed DMs'});
}
} catch (e) {
// Handle other potential errors
console.error(`Error while sending a message to user ${user.id}:`, e);
errorIds.push({id:user.id, reason: "User doesn't Exist"});
}
Output in pics Somehow whenever i do this with my main(this acc) and select myself in the userselectmenu it works.
No description
No description
6 Replies
d.js toolkit
d.js toolkit11mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button! - Marked as resolved by OP
Ateeb Sohail
Ateeb SohailOP11mo ago
console output:
Failed to send a message to user 564513861034835968.
[ { id: '564513861034835968', reason: 'Closed DMs' } ] 1
1
Failed to send a message to user 564513861034835968.
[ { id: '564513861034835968', reason: 'Closed DMs' } ] 1
1
Ive tried a friends account too same thing
space
space11mo ago
You can't check whether the user allows DMs without actually trying to send a dm. You seem to be checking whether a DM channel happen to be cached at the moment, which is unrelated to whether you can actually send a DM to them.
Ateeb Sohail
Ateeb SohailOP11mo ago
Oh How can i check if they have dms available? (if i can)
space
space11mo ago
You can't, you have to just send and handle the eventual error occuring.
Ateeb Sohail
Ateeb SohailOP11mo ago
oh okay thanks ill try it out Thank you so much, it worked!
Want results from more Discord servers?
Add your server