-1

Recently I saw another question that had a answer but it doesn't fully satisfy me. I would like to use the bot in the usage of /invite [invitelink]

Found this code that works using puppeteer but I would like it instead of setting the url by hand in code to use a var or something. I tried just changing the url to a var called url but that just gives a error

>(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.

The code I use is:

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
};

Got the code from here: How do I make a selfbot join a server?

Can anyone help me?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
cygnushg
  • 3
  • 1
  • Did you try reading the error message? Did you understand what it said? Did you try copying and pasting any parts of it into a search engine? – Karl Knechtel Mar 18 '21 at 09:35
  • I mean I tested the link and it gives the link fully working, I am not that good with puppeteer so that's why I am asking. I have been trying to fix this issue for more then a week so this is my last resort. I have absolutely no idea why this doesn't work since I saw threads that used it with a var instead of the full link so I really have no idea why this doesn't work. I would appreciate any help anyways. – cygnushg Mar 18 '21 at 09:47

1 Answers1

1

So in the Error is stated, that evaluation failed, because invite is not defined. Evaluation is the part of the code, that gets injected into the website you're trying to access.

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

You need to pass the inviteCode to the page.evaluate() function. In your case, you also need to pass the token to the function.

To pass variables inside the page.evaluate() function in puppeteer, I found an answer here:

How can I pass variable into an evaluate function?

So for your example, it'd look like that:

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
};

Like that, you can pass the arguments directly to your function like that: invite(inviteCode, token);

Sac Lenca
  • 36
  • 3