如何在没有硬编码的情况下使用多个不和谐机器人帐户登录?

Posted

技术标签:

【中文标题】如何在没有硬编码的情况下使用多个不和谐机器人帐户登录?【英文标题】:How do I login with multiple discord bot accounts WITHOUT hardcoding? 【发布时间】:2020-05-29 13:09:42 【问题描述】:

我一直试图让它工作一段时间,但无济于事。

我尝试过的如下,我想要做的是让多个不和谐机器人帐户同时响应相同的提示,而无需对其进行硬编码,到目前为止我最好的示例如下。

编辑:我知道 client.on 错误,但这是迄今为止我所拥有的最好的例子

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:\test> 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:\test\app.js:40
    client.on("ready", () => 
           ^

TypeError: client.on is not a function
    at E:\test\app.js:40:12
    at Array.forEach (<anonymous>)
    at Object.<anonymous> (E:\test\app.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

【问题讨论】:

clients.push(new Discord.Client().login(element)) 附加登录承诺而不是客户端。取而代之的是const c = new Discord.Client(); c.login(element); clients.push(c);,但也以其他方式处理承诺。 @Aivaras 每次迭代 foreach 时不会覆盖那个 const,因此只有最后一个身份验证令牌的值吗? 不,它是为该函数内部的每次迭代创建的,并且无法从外部访问。我的意思是auth["Tokens"].forEach(t =&gt; const c = new Discord.Client(); c.login(t); clients.push(c);); 【参考方案1】:

您可以在开发工作流程中使用 Git。您可以将您的代码(仅使用一个令牌登录)推送到 GitHub 存储库。请注意,在提交到 repo 之前,您需要将 node_modules 文件夹放在 .gitignore 中。推送完成后,使用 git clone,在 Git Bash 中重命名主克隆文件夹:

mv yourClonedRepoFolder nameForTheSameFolderButNewName

然后,您可以将令牌替换为每个克隆的相应令牌。完成了!

这是有利的,因为您可以更改每个机器人的代码。

【讨论】:

我的和@Tanuki 的代码都有帮助。但是,我的方法不同。【参考方案2】:

您可以通过简单的方式完成。首先对你的令牌做一个循环,然后做你的工作。

这看起来像:

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)

【讨论】:

以上是关于如何在没有硬编码的情况下使用多个不和谐机器人帐户登录?的主要内容,如果未能解决你的问题,请参考以下文章

如何在不使用“除外”python的情况下退出不和谐机器人

我如何在不和谐中 ping 某人

如何在不将其注册为机器人的情况下创建不和谐机器人?

如何在 PHP 中没有硬编码密钥的情况下进行对称加密

如何在没有硬编码 NetworkCredentials 的情况下使用 System.Net.Mail?

谷歌驱动API的Android如何登录并从硬编码我的硬盘帐户注销无需用户交互?