Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Discord.js 使不协调机器人自动加入服务器_Discord.js_Puppeteer - Fatal编程技术网

Discord.js 使不协调机器人自动加入服务器

Discord.js 使不协调机器人自动加入服务器,discord.js,puppeteer,Discord.js,Puppeteer,最近我看到另一个问题有了答案,但我并不完全满意。我想在/invite[invitelink]中使用bot 我发现这段代码可以使用Puppeter,但我希望它能代替手动设置url来使用var或其他东西。我尝试将url更改为一个名为url的变量,但这只会产生一个错误 >(node:32664) UnhandledPromiseRejectionWarning: Error: Evaluation failed: ReferenceError: invite is not defined

最近我看到另一个问题有了答案,但我并不完全满意。我想在/invite[invitelink]中使用bot

我发现这段代码可以使用Puppeter,但我希望它能代替手动设置url来使用var或其他东西。我尝试将url更改为一个名为url的变量,但这只会产生一个错误

>(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.
从这里获取代码:


有人能帮我吗?

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

>(node:32664) UnhandledPromiseRejectionWarning: Error: Evaluation failed: ReferenceError: invite is not defined
at __puppeteer_evaluation_script ...
您需要将
inviteCode
传递到
页面。evaluate()
函数。在您的情况下,还需要将
令牌
传递给函数

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

因此,对于您的示例,它看起来是这样的:

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)

您是否尝试读取错误消息?你明白上面说的吗?你有没有试过把它的任何部分复制粘贴到搜索引擎里?我的意思是我测试了链接,它让链接完全正常工作,我不太擅长木偶演员,所以这就是我问的原因。我已经尝试解决这个问题一个多星期了,所以这是我最后的选择。我完全不知道为什么这不起作用,因为我看到线程将它与var而不是完整链接一起使用,所以我真的不知道为什么这不起作用。不管怎样,我都会感激你的帮助。
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
};