Ralfotr
Ralfotr

Reputation: 45

Dealing with errors

I want the bot to receive a /dm command and then send a DM to a specific person. Target is targetid and the DM content is letter. The command itself works - it's the error catching that doesn't work. If I make targetid something like 267 or any other impossible targetid, it won't output the error as a new message.

try {
    client.users.fetch(targetid).then((user) => {user.send(letter);})
    message.react("✅")
} catch (err) {
    message.channel.send("❗ Something went wrong! Refer to the error log below.\n\n ``" + err + "``\n(Bot administrator contacted: <@...>)") //Removed my ID
    message.react("❌")
}

Upvotes: 1

Views: 32

Answers (1)

ignis
ignis

Reputation: 442

I would suggest using Promise#catch() instead of normal try/catch, since that's what fetch() returns:

client.users
    .fetch(targetid)
    .then(user => {
        user.send(letter)
        message.react('✅')
    })
    .catch(err => {
        message.channel.send('❗ Something went wrong! Refer to the error log below.\n\n ``' + err + '``\n(Bot administrator contacted: <@...>)') //Removed my ID
        message.react('❌')
    })


You can read more about it here

Upvotes: 1

Related Questions