This script will go through all snippets in every day and see if there is any new snippet is added and if there is any new snippet then it sends it to discord.
/* ALERT CHANNEL: channel where you want to send alerts */
const axios = require('axios');
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
const sleep = async (ms) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve();
}, ms || 0);
});
};
let savedURL = await lib.utils.kv['@0.1.16'].get({
key: `snippet_check`,
});
if (!savedURL) {
savedURL = await axios
.post(
'https://www.toptal.com/developers/hastebin/documents',
JSON.stringify([])
)
.then(
(res) => `https://www.toptal.com/developers/hastebin/raw/` + res.data?.key
);
}
let database = await axios(savedURL).then((res) => res.data);
const firstPage = await axios('https://autocode.com/snippet?prefetch').then(
(res) => res.data
);
let newSnippet = 0;
let snippets = [];
await Promise.all(
[...Array(firstPage.data.pages).keys()].map(async (i) => {
const page = await axios(
`https://autocode.com/snippet?p=${i}&prefetch`
).then((res) => res.data);
snippets = snippets.concat(
page.data.snippets.filter((x) => !database.includes(x.sid))
);
})
);
await lib.utils.kv['@0.1.16'].set({
key: `snippet_check`,
value: await axios
.post(
'https://www.toptal.com/developers/hastebin/documents',
JSON.stringify(database.concat(snippets.map((x) => x.sid)))
)
.then(
(res) => `https://www.toptal.com/developers/hastebin/raw/` + res.data?.key
),
});
if (snippets.length < 5) {
for (let snippet of snippets) {
await lib.discord.channels['@0.3.0'].messages.create({
content: ``,
channel_id: process.env.ALERT_CHANNEL,
embeds: [
{
author: {name: 'New snippet is out 🥳'},
title: snippet.title,
url: `https://autocode.com/snippet/${snippet.user.username}/${snippet.sid}`,
description: snippet.description,
thumbnail: {url: snippet.user.full_image_url},
color: 0xffffff,
},
],
});
sleep(100);
}
}