-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.js
110 lines (87 loc) · 5.56 KB
/
code.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Please enter your webhooks below, inside the quotations, you can have multiple, as long as you format like this: "LINK_1", "LINK_2", "LINK_3"
const webhooks = ["Webhook"];
// This defines the variables, of which can be filled out; these are completely optional.
// Title can either be what you enter below, but if you leave blank, the script will fill it with the form title.
// Avatar Image is the tiny thumbnail you see on embeds, this is great for a logo.
// Short Description is great if you wish to add a tiny bit of information.
// Color allows you to enter a custom hex color, if left blank, a colour will be randomly chosen each time.
// Mention is great if you want to be alerted, or you want a certain role alerted. Mention either the user/role in Discord with a \ at the beginning to the formatting.
// Type is a new required variabl e, state either "EMBED" or "TEXT" - the default is embed.
const title = "", avatarImage = "", shortDescription = "", colour = "", mention = "", type = "";
// Bonus features 😃
// You can turn these ON or OFF, defaults to the values entered below.
const bonusFeatures = {
convert2Link: 'ON', // Links in embeds will be clickable | Text links won't embed.
convert2Mention: 'OFF' // Checks if there's a Discord ID, and converts it into a mention.
}
// This defines and fetches the current form, all the responses from the form, the latest response received, and the items from the Q&A's from the latest res.
// Items is set up to store the Q&A's.
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); };
}