#Gracefully stop the bot

14 messages · Page 1 of 1 (latest)

odd flicker
#

Is there any way to gracefully stop the bot? I want it to appear as offline as soon as it stops.

client.user!.setPresence({
  status: "invisible"
});
await client.destroy();
```doesn't seem to work…
Also, what's the reason that `setPresence` unlike basically any other similar method isn't async?
copper bronzeBOT
#

• What's your exact discord.js npm list discord.js and node node -v version?
• Post the full error stack trace, not just the top part!
• Show your code!
• Explain what exactly your issue is.
• Not a discord.js issue? Check out #useful-servers.

polar summit
#

I think you need to await the setPresence since destroying the client might stop your request from actually firing

odd flicker
#

setPresence does not return a Promise, it returns the presence synchronically, looking at the type definitions…

#

(discord.js/typings/index.d.ts)

#

I tried to await another API thing before destroying the client (force fetching the client user, to be exact), but the bot still didn't immediately switch to offline after stopping it

polar summit
#

I uses the Ws, thats why it's sync

#

Maybe add an 1-2s cooldown before destroying? idk

odd flicker
polar summit
#

It calls the websocket to the change, not a rest request and when you destroy the client you also destroy the Ws

#

That might be the race condition

odd flicker
#

There's another weird thing: editing a message in the exit handler takes around 15 seconds…

#
    let exitHandled = false;
    async function exitHandler() {
        try {
            if (exitHandled) return;
            exitHandled = true;
            console.log("Handling exit…");
            disableAllButtons = true;
            console.debug("Refreshing volume message…");
            await refresh();
            console.debug("Setting offline presence…");
            client.user!.setPresence({
                status: "invisible"
            });
            await Utils.delay(1000);
            console.debug("Destroying the client…");
            await client.destroy();
            console.log("Handled.");
        } catch(e) {
            console.error(e);
        } finally {
            process.exit(0);
        }
    }
```this is the handler