Pruina Tempestatis
Pruina Tempestatis

Reputation: 394

How do you make a command which restarts your bot in discord.js?

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

Upvotes: 6

Views: 71070

Answers (1)

Blundering Philosopher
Blundering Philosopher

Reputation: 6805

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!');
});

Upvotes: 13

Related Questions