Thursday, May 23, 2024
 Popular · Latest · Hot · Upcoming
147
rated 0 times [  148] [ 1]  / answers: 1 / hits: 67486  / 6 Years ago, sat, february 3, 2018, 12:00:00

I'm making a bot in discord.js. How do I make a command that restarts the bot?


More From » node.js

 Answers
240

You can reset a bot by using the client.destroy() method, then calling .login after again. Try something like this:



// set message listener 
client.on('message', message => {
switch(message.content.toUpperCase()) {
case '?RESET':
resetBot(message.channel);
break;

// ... other commands
}
});

// Turn bot off (destroy), then turn it back on
function resetBot(channel) {
// send channel a message that you're resetting bot [optional]
channel.send('Resetting...')
.then(msg => client.destroy())
.then(() => client.login(<your bot token here>));
}


If you set a ready listener in your bot, you will see that the ready event fires twice. I set up a ready listener like this:



client.on('ready', () => {
console.log('I am ready!');
});

[#55272] Wednesday, January 31, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
erick

Total Points: 588
Total Questions: 92
Total Answers: 100

Location: Bangladesh
Member since Sat, Jan 23, 2021
3 Years ago
;