My Discord bot sometimes sends a message directly to the user, but if user turned off the messages from strangers it crashes.
Is there some If function that will check if you can send a message to this user? Or maybe some command, that will try to send a message to the user, but if it won't work, bot will ignore it and go on? I tried "try" but it doesn't work DiscordAPIError: Cannot send messages to this user
try{
await client.users.cache.get(`${id}`).send('hello',{
embed:embed1,
});
let messageEmbed1 = await client.users.cache.get(`${id}`).send({embeds: [embed1]})
} catch (error) {
console.error(error);
send function is a async function, therefore you need to chain catch function instead of try catch scoping them.
await client.users.cache.get(`${id}`).send('hello',{
embed:embed1,
});
let messageEmbed1 = await client.users.cache.get(`${id}`).send({embeds: [embed1]}).catch(e=>{console.log(`Saved bot from crash, error:\n${e}`)});