node.js - 在同一个 NodeJS 项目中运行不同的 discord.js bot 实例

标签 node.js discord.js

我正在尝试创建一个同时为不同机器人(具有不同 token )提供服务的项目。我的猜测是您将不得不记忆两次“client.login(token)”。我正忙着测试这个,还没有完成,但一旦完成就会回来。

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

我也试着想象这会是什么样子:

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

谢谢你,有一个美好的一天。

最佳答案

我可以确认这有效。这是我的代码:

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.

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

事实上,这甚至适用于 3 个机器人,而且它应该适用于任意数量的机器人!
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 文件,代码也会变得困惑且难以快速阅读。

希望这可以帮助!

关于node.js - 在同一个 NodeJS 项目中运行不同的 discord.js bot 实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61074606/

相关文章:

javascript - 尝试将消息发送到特定 channel ,但无法定义我的客户端

javascript - 分割字符串并保留分割器

javascript - 如何从 Google Places 获取一系列地点的坐标,然后在完成所有操作后使用结果?

javascript - 测试失败: ReferenceError: SpeechSynthesisUtterance is not defined

node.js - 如何删除nodejs中对index.html的偏好?

javascript - 加入服务器时会触发discord.js的channelCreate()事件

discord - 如何在旧版本仍在运行时调试 Discord 机器人?

javascript - 在React中播放状态相关的音频文件

node.js - CocoaPods找不到Pod "lottie-ios"的兼容版本

javascript - 使 Discord 机器人命令不区分大小写?