如何在没有硬编码的情况下使用多个不一致的bot帐户登录?

2022-02-23 00:00:00 json node.js foreach javascript discord.js

我现在已经尝试了一段时间,但没有成功。

我尝试的内容如下所示,我希望能够让多个不一致机器人帐户同时响应相同的提示,而无需对其进行硬编码,我目前为止最好的示例如下所示。

编辑:我知道客户端。出错时,但这是我遇到的最好的例子

const auth = require("./tokens.json");
const Discord = require("discord.js");
client = new Discord.Client();
var clients = [];
auth["Tokens"].forEach(element => {
    console.log(clients)
    clients.push(new Discord.Client().login(element));
    console.log(clients)
});
console.log(clients[0])
console.log(clients[1])
console.log(clients[2])

clients.forEach(client => {
    client.on("ready", () => {
        console.log("I am ready!");
        console.log(client.user.id)
    });
});

我的tokens.json代码:

{
    "Tokens": ["MjIxtokentoken","MjIxtokentokenc","MjIxtokentoken"]
}

输出:

PS E:	est> node app.js
[]
[ Promise { <pending> } ]
[ Promise { <pending> } ]
[ Promise { <pending> }, Promise { <pending> } ]
[ Promise { <pending> }, Promise { <pending> } ]
[ Promise { <pending> }, Promise { <pending> }, Promise { <pending> } ]
Promise { <pending> }
Promise { <pending> }
Promise { <pending> }
E:	estapp.js:40
    client.on("ready", () => {
           ^

TypeError: client.on is not a function
    at E:	estapp.js:40:12
    at Array.forEach (<anonymous>)
    at Object.<anonymous> (E:	estapp.js:39:9)
    at Module._compile (internal/modules/cjs/loader.js:959:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)
    at Module.load (internal/modules/cjs/loader.js:815:32)
    at Function.Module._load (internal/modules/cjs/loader.js:727:14)
    at Function.Module.runMain (internal/modules/cjs/loader.js:1047:10)
    at internal/main/run_main_module.js:17:11

解决方案

您可以通过简单的方式完成。首先在Your令牌上循环,然后做好您的工作。

如下所示:

const auth = require('./tokens.json')
const Discord = require('discord.js')

for (const token of auth.Tokens) {
    const client = new Discord.Client()
    client.on('ready', () => {
        console.log('I am ready !')
        console.log(client.user.id)
    })
    client.login(token)
}

相关文章