'Bot crashed when trying to DM
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);
Solution 1:[1]
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}`)});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Rovel Stars |
