You are on page 1of 3

const webhooks =

['https://discord.com/api/webhooks/1009393465211240478/uPAguAHflMrWlByAXGXcfnXCUrxZ
TpWZSctjenRcmlmpdSz3dBIL8UwMPp3QhYDuihFV'];

const title = "Title", avatarImage = "", shortDescription = "Description", colour =


"#338ccc", mention = "<@&1004491960649797754>", type = "";

const bonusFeatures = {
convert2Link: 'ON', // Links in embeds will be clickable | Text links won't
embed.
convert2Mention: 'ON' // Checks if there's a Discord ID, and converts it into a
mention.
}

const form = FormApp.getActiveForm(), allResponses = form.getResponses(),


latestResponse = allResponses[allResponses.length - 1];
let response;
var items = [];

// Checks if there's a response, if not - throw error.


try {
response = latestResponse.getItemResponses()
} catch (error) {
throw "No Responses found in your form."
}

// Just a safe check to make sure you've entered a webhook.


for (const hook of webhooks) {
if (!/^(?:https?:\/\/)?(?:www\.)?(?:(?:canary|ptb)\.)?discord(?:app)?\.com\/
api\/webhooks\/\d+\/[\w-+]+$/i.test(hook)) throw `Webhook ${i + 1 || 1} is not
valid.`;
}

// An extra check as people have been having issues.


if (avatarImage && !/\.(jpeg|jpg|gif|png)$/.test(avatarImage)) throw "Image URL is
not a direct link";

// This loops through our latest response and fetches the Question titles/answers;
then stores them in the items array above.
for (var i = 0; i < response.length; i++) {
const question = response[i].getItem().getTitle(), answer =
response[i].getResponse();
if (answer == "") continue;
items.push({ "name": question, "value": answer });

function data(item) {
const linkValidate = /(?:(?:https?|http?):\/\/)(?:\([-A-Z0-9+&@#\/%=~_|
$?!:,.]*\)|[-A-Z0-9+&@#\/%=~_|$?!:,.])*(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[A-Z0-
9+&@#\/%=~_|$])/i;

// Checks if there's an ID, if there is, convert into a mention.


if (bonusFeatures.convert2Mention == 'ON' && !isNaN(item.value) &&
item.value.length == 18) item.value = `<@!${item.value}>`;

// Checks if type is text, or default to embed.


if (bonusFeatures.convert2Link == 'ON' && type.toLowerCase() !== 'text') {
// Validates the link, if it's true, make it a hyperlink for embed.
if (linkValidate.test(item.value)) item.value = `[${item.value}]($
{item.value})`;
} else {
// Validates the link, if it's true, make it not-embed for text.
if (bonusFeatures.convert2Link == 'ON' &&
linkValidate.test(item.value)) item.value = `<${item.value}>`;
}

return [`**${item.name}**`, `${item.value}`].join("\n");


}
}

if (items.map(data).toString().length + shortDescription.length > 1999) throw


"Discord limit reached. Please add limits to your questions!";

function plainText(e) {

// A webhook construct, which sets up the correct formatting for sending to


Discord.
const text = {
"method": "post",
"headers": { "Content-Type": "application/json" },
"muteHttpExceptions": true,
"payload": JSON.stringify({
"content": `${mention ? mention : ''}${title ? `**${title}**` : `**$
{form.getTitle()}**`}\n\n${shortDescription ? `${shortDescription}\n\n$
{items.map(data).join('\n\n')}` : items.map(data).join('\n\n')}`
}),
};

// We now loop through our webhooks and send them one by one to the respectful
channels.
for (var i = 0; i < webhooks.length; i++) { UrlFetchApp.fetch(webhooks[i],
text); };
}

function embedText(e) {

// A webhook embed construct, which sets up the correct formatting for sending
to Discord.
const embed = {
"method": "post",
"headers": { "Content-Type": "application/json" },
"muteHttpExceptions": true,
"payload": JSON.stringify({
"content": mention ? mention : '',
"embeds": [{
"title": title ? title : form.getTitle(), // Either the set title
or the forms title.
"description": shortDescription ? `${shortDescription}\n\n$
{items.map(data).join('\n\n')}` : items.map(data).join('\n\n'), // Either the desc
or just the res.
"thumbnail": { url: avatarImage ? encodeURI(avatarImage) :
null }, // The tiny image in the right of the embed
"color": colour ? parseInt(colour.substr(1), 16) :
Math.floor(Math.random() * 16777215), // Either the set colour or random.
"timestamp": new Date().toISOString() // Today's date.
}]
}),
};
// We now loop through our webhooks and send them one by one to the respectful
channels.
for (var i = 0; i < webhooks.length; i++) { UrlFetchApp.fetch(webhooks[i],
embed); };
}

You might also like