让不和谐机器人自动加入服务器

时间:2021-03-18 09:32:29

标签: discord.js puppeteer

最近我看到另一个有答案的问题,但它并不完全让我满意。我想在/invite [invitelink]

的用法中使用bot

发现这段代码可以使用 puppeteer 工作,但我想要它而不是在代码中手动设置 url 以使用 var 或其他东西。我尝试将 url 更改为一个名为 url 的 var 但这只是给出了一个错误

>(node:32664) UnhandledPromiseRejectionWarning: Error: Evaluation failed: ReferenceError: invite is not defined
    at __puppeteer_evaluation_script__:3:62
    at ExecutionContext._evaluateInternal (E:\Folders\DiscordBot\v2\node_modules\puppeteer\lib\cjs\puppeteer\common\ExecutionContext.js:218:19)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async ExecutionContext.evaluate (E:\Folders\DiscordBot\v2\node_modules\puppeteer\lib\cjs\puppeteer\common\ExecutionContext.js:107:16)
    at async invite4 (E:\Folders\DiscordBot\v2\app.js:91:5)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:32664) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:32664) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

我使用的代码是:

const invite = async () => {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();
    await page.goto('https://discord.com/');
    await page.evaluate(async () => {
        const x = new XMLHttpRequest();
        x.open('POST', `https://discord.com/api/v6/invites/${invite}`);
        x.setRequestHeader('Authorization', token);
        x.send();
    });
    //Maybe good idea to close browser after executing
};

从这里得到代码:How do I make a selfbot join a server?

有人可以帮我吗?

1 个答案:

答案 0 :(得分:1)

因此在错误中指出,评估失败,因为 invite 未定义。评估是代码的一部分,它被注入到您尝试访问的网站中。

>(node:32664) UnhandledPromiseRejectionWarning: Error: Evaluation failed: ReferenceError: invite is not defined
at __puppeteer_evaluation_script ...

您需要将 inviteCode 传递给 page.evaluate() 函数。在您的情况下,您还需要将 token 传递给函数。

要在 puppeteer 的 page.evaluate() 函数内传递变量,我在这里找到了答案:

How can I pass variable into an evaluate function?

对于您的示例,它看起来像这样:

const invite = async (inviteCode, token) => {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();
    await page.goto('https://discord.com/');
    await page.evaluate(({inviteCode, token}) => {
        const x = new XMLHttpRequest();
        console.log(inviteCode);
        x.open('POST', `https://discord.com/api/v6/invites/${inviteCode}`);
        x.setRequestHeader('Authorization', token);
        x.send();
    },{inviteCode, token});
    //Maybe good idea to close browser after executing
};

这样,您可以像这样直接将参数传递给您的函数:invite(inviteCode, token);