Fill out information about a message and then send that message to Discord using a webhook! Upon the first time you run this snippet the code will create a webhook.
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
//Checks to see if a webhook for the snippet has been created already
let check = await lib.utils.kv['@0.1.16'].get({
key: `CREATED_WEBHOOK_FOR_WEBSITE_CHECK`,
defaultValue: false,
});
//If this is the first time the site has been run it will create the webhook
if (check == false) {
//Creates the webhook
let webhook = await lib.discord.webhooks['@0.1.0'].create({
channel_id: `${process.env.CHANNEL_ID}`,
name: `${process.env.WEBHOOK_NAME}`,
});
//Makes the webhook URL from data in the creation
let webhookUrl = `https://discord.com/api/webhooks/${webhook.id}/${webhook.token}`;
//Sets a KV value with the URL for the webhook so that this webhook can be used more than once
await lib.utils.kv['@0.1.16'].set({
key: `WEBSITE_WEBHOOK_URL`,
value: `${webhookUrl}`,
});
//Sets the first KV to true so this webhook wont be created multiple times
await lib.utils.kv['@0.1.16'].set({
key: `CREATED_WEBHOOK_FOR_WEBSITE_CHECK`,
value: true,
});
//Sends responses to the console
console.log(`Created webhook!`);
console.log(`Name: ` + webhook.name);
console.log(`ID: ` + webhook.id);
console.log(`Token: ` + webhook.token);
}
//Gets the webhook URL
let webhookUrl = await lib.utils.kv['@0.1.16'].get({
key: `WEBSITE_WEBHOOK_URL`,
});
//All of the HTML/CSS/JS that makes the site
let body = `
Discord Message Sender 3000
The Discord message sender 3000
Embed
Color
Image
Thumbnail
Author
Footer
`;
console.log(`Website loaded!`);
console.log(`Look in the run tab`);
return {
statusCode: 200,
headers: {'Content-Type': 'text/html'},
body: Buffer.from(body),
};