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?