Search code examples
node.jsdiscorddiscord.js

How to ignore msg.delete() if not found discord.js


execute(client, message) {
        message.channel.send(`Last heartbeat calculated ${ms(Date.now() - client.ws.shards.first().lastPingTimestamp, { long: true })} ago **${client.ws.ping}ms** 🛰️`).then(msg => {
           setTimeout(() => msg.delete(), client.config.app.dtime) 
        });

Hi there, I was working on my discord bot and ran into a scenario like this, the code causing the problem is shown above. When this function runs, it sends the current ping of the bot to the message channel. and automatically deletes the msg after some time, but in case some person with admin privileges deletes the message manually. My bot will crash when it tries to delete so to avoid that how should I fix my code. Thanks in advance.


Solution

  • You can handle the promise rejection and void it as such:

    msg.delete().catch(() => null);
    

    Even when the promise rejects, no errors will appear nor will the process crash