forked from Iku/Google-Forms-to-Discord
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathscript.gs
105 lines (94 loc) · 3.33 KB
/
script.gs
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
// The Post url to send the embed to. This is where you should paste your webhook link.
var POST_URL = "Discord Webhook link here";
// onSubmit function that should be triggered when a new form is submitted.
function onSubmit(e) {
// Get the form object
var form = FormApp.getActiveForm();
// Get all of the form's responses
var allResponses = form.getResponses();
// Get the latest response submitted to the form
var latestResponse = allResponses[allResponses.length - 1];
// Get an array containing all the responses to each question
var response = latestResponse.getItemResponses();
// Current items array to use in embed
var items = [];
// Current number characters being used in the current embed
var currentEmbedCharacterNum = 0
// For loop to iterate through responses
for (var i = 0; i < response.length; i++) {
// Get the question text
var question = response[i].getItem().getTitle();
// Get the answer text
var answer = response[i].getResponse();
// When checkbox questions are used, we need to compact the multiple choices into one answer string
if (answer instanceof Array){
answer = answer.join(", ");
}
// If the answer is over a certain number of characters, break it into multiple parts.
try {
var parts = answer.match(/[\s\S]{1,1024}/g) || [];
} catch (e) {
var parts = answer;
}
// If the answer text is blank, skip this iteration.
if (answer == "") {
continue;
}
if (question.length > 256){
question = question.substring(0, 220) + "...";
}
// For loop to iterate through the parts of an answer
for (var j = 0; j < parts.length; j++) {
// Add the number of characters in a part to the total character count of this embed
currentEmbedCharacterNum += parts[j].length + question.length;
// If the total character count with this part included pushes the total over 5000 (Discord Embeds have a character limit)
if (currentEmbedCharacterNum >= 5000){
// Send away an embed with the current items data
sendEmbed(items);
// Wait a second so Discord doesn't get overwhelmed with requests
Utilities.sleep(50)
// Reset the character count and items array
currentEmbedCharacterNum = 0;
items = [];
}
// If there are multiple parts to an answer, add a (cont.) to the names.
if (j == 0) {
items.push({
"name": question,
"value": parts[j],
"inline": false
});
} else {
items.push({
"name": question.concat(" (cont.)"),
"value": parts[j],
"inline": false
});
}
}
}
// Send an embed to the webhook.
sendEmbed(items);
};
// Function to send an embed to the webhook url.
function sendEmbed(items){
// Package the embed options into a variable
var options = {
"method": "post",
"headers": {
"Content-Type": "application/json",
},
"payload": JSON.stringify({
"embeds": [{
"title": "Some nice embed title here",
"color": 33023, // This is optional, you can look for decimal colour codes at https://convertingcolors.com/
"fields": items,
"footer": {
"text": "Some embed footer here"
}
}]
})
};
// Post the data to the webhook.
UrlFetchApp.fetch(POST_URL, options);
}