我可以确认这有效。这是我的代码:
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.
然而,对于更深入的项目,我建议(对于命令等)为两个机器人使用不同的文件,因为我认为即使您使用相同的 index.js 文件,代码也会变得混乱且难以快速阅读。
希望这可以帮助!