'How do I logout my Discordbot in JS savely with a command
I built a basic Discord bot which I want to go offline when I type the corresponding command on the Discord server "!logout". I searched for answers, but those who used discord.js weren't responsed...
If I use client.logout() it throws an error and crashs with
TypeError: client.logout is not a function
and it won't even call my error function. Maybe the answer is simple but I can't find it anywhere.
Here's my basic code:
require('dotenv').config()
const Discord = require('discord.js')
const client = new Discord.Client({
intents: ["GUILDS", "GUILD_MESSAGES"],
})
client.on('ready', () => {
console.log('Bot is ready');
});
client.on("messageCreate", msg => {
if (msg.content === '!logout'){
client.logout(() => {
msg.reply('couldn^t go offline')
})
}
})
client.login(process.env.BOT_TOKEN)
Thank you! :)
Solution 1:[1]
Try using the function <Client>#destroy()
If you want to exit Node.js, you can then use process.exit(0)
Solution 2:[2]
process.exit(0)
From the docs:
process.exit([exitcode])Ends the process with the specified code. If omitted, exit uses the 'success' code 0.To exit with a 'failure' code:
process.exit(1); The shell that executed node should see the exit code as 1.
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 | unfestive chicken |
| Solution 2 | Dreamy Player |
