本文介绍了在同一个 NodeJS 项目中运行不同的 discord.js 机器人实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个同时服务于不同机器人(具有不同令牌)的项目.我的猜测是您必须两次回忆client.login(token)".我正在忙于测试,尚未完成,但完成后会回来.

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.

有人对在同一个文件、同一个项目中运行多个 NodeJS 机器人实例有什么建议吗?这甚至可能吗?非常感谢您的帮助.

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.

推荐答案

我可以确认这是可行的.这是我的代码:

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);

这是控制台日志:

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

有趣的是,Bot 1 还是 Bot 2 首先响应各不相同,因此您可能需要考虑这一点.

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

事实上,这甚至适用于 3 个机器人,而且它应该适用于任意数量的机器人!

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);

这里是控制台日志:

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

但是,对于更深入的项目,我建议(对于命令等)为 2 个机器人使用不同的文件,因为我认为即使您使用相同的 index.js,代码也会变得混乱且难以快速阅读文件.

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.

希望这会有所帮助!

这篇关于在同一个 NodeJS 项目中运行不同的 discord.js 机器人实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 19:24