Gracefully stop the bot

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();
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?
10 Replies
d.js docs
d.js docs3y ago
• 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.
조아오
조아오3y ago
I think you need to await the setPresence since destroying the client might stop your request from actually firing
❄ Christmas Ralsei
setPresence does not return a Promise, it returns the presence synchronically, looking at the type definitions…
❄ Christmas Ralsei
(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
조아오
조아오3y ago
I uses the Ws, thats why it's sync Maybe add an 1-2s cooldown before destroying? idk
❄ Christmas Ralsei
Ws? As in websockets?
조아오
조아오3y ago
It calls the websocket to the change, not a rest request and when you destroy the client you also destroy the Ws
조아오
조아오3y ago
That might be the race condition
❄ Christmas Ralsei
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);
}
}
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 Approximately 15 seconds between Refreshing volume message… and Setting offline presence…, and the only await inside refresh() is editing a message saved in a variable
Want results from more Discord servers?
Add your server