Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 615
Pruina Tempestatis Avatar asked Feb 03 '18 20:02

Pruina Tempestatis


People also ask

How to create a discord bot?

1. Make sure you’re logged on to the Discord website. 2. Navigate to the application page. 3. Click on the “New Application” button. 4. Give the application a name and click “Create”. 5. Go to the “Bot” tab and then click “Add Bot”. You will have to confirm by clicking "Yes, do it!"

How to restart a bot from command prompt?

You can even make a command to restart your bot with this script. Go into your bot folder with : cd BotFolderLocation Use nano start.sh to create a file called start.sh and start editing. Press CTRL+C then Y and ENTER to save the start.sh file.

What is an event in discord?

An event is something you listen to and then respond to. For example, when a message happens, you will receive an event about it that you can respond to. Let’s make a bot that replies to a specific message. This simple bot code is taken directly from the discord.js documentation. We will be adding more features to the bot later.

How do I create a Discord server in REPL?

Start by going to Repl.it. Create a new Repl and choose "Node.js" as the language. This means the programming language will be JavaScript. To use the discord.js library, just add const Discord = require ("discord.js"); at the top of main.js. Repl.it will automatically install this dependency when you press the "run" button.


1 Answers

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!');
});
like image 55
Blundering Philosopher Avatar answered Nov 10 '22 22:11

Blundering Philosopher