this snippet uses puppeteer to visit the website which generates AI memes and send it to your Discord channel:) In order for it to function properly, you have to set your Project Timeout to 30000ms
// authenticates you with the API standard library
// type `await lib.` to display API autocomplete
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
//require autocode-puppeteer as puppeteer is a large dependency to add.
const puppeteer = require('autocode-puppeteer');
//defines sleep function
let sleep = async (ms) => {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
};
//defines browser as nothing:)
let browser;
//try this code first
try {
//define browser as launching a browser:)
browser = await puppeteer.launch();
let msg = await lib.discord.channels['@0.3.2'].messages.create({
channel_id: `${context.params.event.channel_id}`,
content: `Browser Launched`,
});
//opens a new page/tab
const page = await browser.newPage();
np = await lib.discord.channels['@0.3.2'].messages.update({
message_id: `${msg.id}`,
channel_id: `${msg.channel_id}`,
content: `${msg.content}\n New Page Created`,
});
//goes to the AI meme generator website
await page.goto('https://imgflip.com/ai-meme');
to = await lib.discord.channels['@0.3.2'].messages.update({
message_id: `${msg.id}`,
channel_id: `${msg.channel_id}`,
content: `${np.content}\n Tabs Opened`,
});
await sleep(3000);
//scroll till it sees the button
await scrollToElement(page, '.aim-generate-btn');
//click on the Save button to save the image
await page.click('.aim-generate-btn');
sbc = await lib.discord.channels['@0.3.2'].messages.update({
message_id: `${msg.id}`,
channel_id: `${msg.channel_id}`,
content: `${to.content}\n Save Button Clicked`,
});
//waits for 2 seconds so that the popup element can show properly
await sleep(2000);
//makes sure that isVisible Function is able to find a text called Copy Link from the page
let visible = await isVisible(page, 'input.img-code.link');
cltiv = await lib.discord.channels['@0.3.2'].messages.update({
message_id: `${msg.id}`,
channel_id: `${msg.channel_id}`,
content: `${sbc.content}\n Copy Link text is visible`,
});
//defines generatedLink as getText function which gets the text from the selector of `input.img-code.link`
let generatedLink = await getText(page, 'input.img-code.link');
await lib.discord.channels['@0.3.2'].messages.update({
message_id: `${msg.id}`,
channel_id: `${msg.channel_id}`,
content: `${cltiv.content}\n Please wait while the meme is generating...`,
});
//launch another new page
const page2 = await browser.newPage();
//open up the generated link page
await page.goto(generatedLink);
//evaluate the page for image source so it can be used as the image link for the embed.
let img = await page.evaluate((selector) => {
const element = document.querySelector('#im');
return element.src;
});
//sends the AI generated meme to the channel
await lib.discord.channels['@0.2.2'].messages.update({
channel_id: msg.channel_id,
message_id: msg.id,
content: '',
embeds: [
{
title: `Drake Meme? lmao`,
image: {
url: `${img}`,
},
},
],
});
} catch (e) {
//catch errors if any
console.log('error occurred: ', e);
} finally {
//finally if browser is opened, close it.
if (browser) {
await browser.close();
}
}
//Essential Functions
async function isVisible(page, selector) {
try {
const elementHandle = await page.$(selector);
if (!elementHandle) {
return false;
}
const elementBox = await elementHandle.boundingBox();
return Boolean(elementBox);
} catch (err) {
return false;
}
}
async function scrollToElement(page, selector) {
await page.evaluate((selector) => {
const element = document.querySelector(selector);
element.scrollIntoView({
block: 'center',
inline: 'nearest',
behavior: 'instant',
});
}, selector);
}
async function getText(page, selector) {
return page.evaluate((selector) => {
const element = document.querySelector(selector);
return element.value;
}, selector);
}