0
votes

I'm working on a small project to learn puppeteer and discord bots, thought it would be interesting to combine the two and make a weird cyborg bot thing that is essentially a bot but a normal user. I've set up the command handlers, etc, and I've created an 'invite' command that takes one of the arguments that the person provides (the code of the server invite) and uses it to auto-invite itself.

for example typing in discord: <prefix>invite 78uHjkaf

This semi-works. Not really. It'll work the first time, but never again and I'll have to restart the thing. I keep trying to add a page.close(); hoping that it will solve the problem but then it won't work at all. How can I make this work consistently and reusably? - Note that my command handler works fine. The entire issue is located within the following code. (I hope)

Semi reliable.

    if (command === "invite") {
        const invite = async () => {
            const browser = await puppeteer.launch();
            const page = await browser.newPage();
            await page.goto('https://discordapp.com/');
            let [link] = args;
            await page.evaluate(async ({link}) => {
                const x = new XMLHttpRequest();
                x.open(`POST`, `https://discordapp.com/api/v6/invites/` + `${link}`);
                x.setRequestHeader('Authorization', 'my token');
                x.send();
            },{link});
         };
        invite();
    }

Doesn't work at all.

    if (command === "invite") {
        const invite = async () => {
            const browser = await puppeteer.launch();
            const page = await browser.newPage();
            await page.goto('https://discordapp.com/');
            let [link] = args;
            await page.evaluate(async ({link}) => {
                const x = new XMLHttpRequest();
                x.open(`POST`, `https://discordapp.com/api/v6/invites/` + `${link}`);
                x.setRequestHeader('Authorization', 'my token');
                x.send();
            },{link});
          await page.close();
         };
        invite();
    }

I am REALLY stumped.

Update. For some reason shifting

x.open(`POST`, `https://discordapp.com/api/v6/invites/` + `${link}`);

to

x.open(`POST`, `https://discordapp.com/api/v6/invites/${link}`);

fixed the problem of unreliability. Adding a close page still kills it though. I feel like that should be added however?

2

2 Answers

1
votes

Why are you using Puppeteer? It's easier to use the Discord private API instead.

const fetch = require('node-fetch'); // npm install node-fetch

if (command === "invite") {
    const [code] = args; // should only be the code, not full URL (8KqbkST, not https://discord.gg/8KqbkST)
    fetch(`https://discord.com/api/v8/invites/${code}`, {
        method: 'POST',
        headers: { 'authorization': 'YOUR USER TOKEN' },
        body: JSON.stringify({
            code,
            new_member: true
        })
   });
}

By the way this is totally forbidden by the Discord T.O.S and you should not use it for massive use such as joining a lot of servers at once (because it is forbidden, and because this type of behavior is very painful on Discord).

0
votes

Use this 100% const fetch = require('node-fetch'); // npm install node-fetch

if (command === "invite") { const [code] = args; // should only be the code, not full URL (8KqbkST, not https://discord.gg/8KqbkST) fetch(https://discord.com/api/v8/invites/${code}, { method: 'POST', headers: { 'authorization': 'YOUR USER TOKEN' }, body: JSON.stringify({ code, new_member: true }) }); }