Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running different discord.js bot instances in the same NodeJS project

I am trying to create a project that serves different bots (with different tokens) at the same time. My guess is that you would have to recall "client.login(token)" twice. I am busy testing this and did not yet finish it, but will come back once completed.

Does anyone have some advice on running multiple NodeJS bot instances in the same file, in the same project? Is this even possible? Help is greatly appreciated.

I have also tried to imagine what this would be like:

const {Client} = require('discord.js');
const bot1 = new Client();
const bot2 = new Client();
//bot1 does something
//bot2 does something
bot1.login('token1');
bot2.login('token2');

Thank you and have a good day.

like image 569
ßrilliant Avatar asked Oct 25 '25 02:10

ßrilliant


1 Answers

I can confirm that this works. Here is my code:

const Discord = require('discord.js');
const client1 = new Discord.Client();
const client2 = new Discord.Client();
const CONFIG = require('./config.json');

client1.once('ready', () => {
    console.log('Bot 1 ready.');
});

client2.once('ready', () => {
    console.log('Bot 2 ready.');
});

client1.on('message', message => {
    if (message.content === 'Hello!') {
        message.channel.send('Hello');
        console.log('Bot 1 said hello.');
    }
});

client2.on('message', message => {
    if (message.content === 'Hello!') {
        message.channel.send('world!');
        console.log('Bot 2 said hello.');
    }
});


client1.login(CONFIG.token1);
client2.login(CONFIG.token2);

Here is the Console log:

Bot 2 ready.
Bot 1 ready.
Bot 1 said hello.
Bot 2 said hello.

Interestingly, whether Bot 1 or Bot 2 responds first varies, so you might want to take that into consideration.

In fact, this even works with 3 bots, and it should work with any number of bots!

const Discord = require('discord.js');
const client1 = new Discord.Client();
const client2 = new Discord.Client();
const client3 = new Discord.Client();
const CONFIG = require('./config.json');

client1.once('ready', () => {
    console.log('Bot 1 ready.');
});

client2.once('ready', () => {
    console.log('Bot 2 ready.');
});

client3.once('ready', () => {
    console.log('Bot 3 ready.');
});

client1.on('message', message => {
    if (message.content === 'Hello!') {
        message.channel.send('Hello1');
        console.log('Bot 1 said hello.');
    }
});

client2.on('message', message => {
    if (message.content === 'Hello!') {
        message.channel.send('Hello2');
        console.log('Bot 2 said hello.');
    }
});

client3.on('message', message => {
    if (message.content === 'Hello!') {
        message.channel.send('Hello3');
        console.log('Bot 3 said hello.');
    }
});

client1.login(CONFIG.token1);
client2.login(CONFIG.token2);
client3.login(CONFIG.token3);

And here is the console log for that:

Bot 1 ready.
Bot 3 ready.
Bot 2 ready.
Bot 2 said hello.
Bot 3 said hello.
Bot 1 said hello.

However with a more in depth project, I would advise (for commands and such) using different files for the 2 bots as I think the code would get messy and hard to read quickly, even if you use the same index.js file.

Hope this helps!

like image 126
cs09 Avatar answered Oct 26 '25 18:10

cs09