Advertisement
Finrodi

Untitled

Sep 27th, 2023
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.70 KB | None | 0 0
  1. // The Post url to send the embed to. This is where you should paste your webhook link.
  2. var POST_URL = "https://discord.com/api/webhooks/1156588479912874087/3fO0SkOqkHph1p2Jtu2uvAoUj16ZoHNw2HYrfPn17Q4izpZCXFtkwIWy319jbTTPTNAo";
  3.  
  4. // onSubmit function that should be triggered when a new form is submitted.
  5. function onSubmit(e) {
  6. // Get the form object
  7. var form = FormApp.getActiveForm();
  8. // Get all of the form's responses
  9. var allResponses = form.getResponses();
  10. // Get the latest response submitted to the form
  11. var latestResponse = allResponses[allResponses.length - 1];
  12. // Get an array containing all the responses to each question
  13. var response = latestResponse.getItemResponses();
  14. // Current items array to use in embed
  15. var items = [];
  16. // Current number characters being used in the current embed
  17. var currentEmbedCharacterNum = 0
  18.  
  19. // For loop to iterate through responses
  20. for (var i = 0; i < response.length; i++) {
  21. // Get the question text
  22. var question = response[i].getItem().getTitle();
  23. // Get the answer text
  24. var answer = response[i].getResponse();
  25. // If the answer is over a certain number of characters, break it into multiple parts.
  26. try {
  27. var parts = answer.match(/[\s\S]{1,1024}/g) || [];
  28. } catch (e) {
  29. var parts = answer;
  30. }
  31.  
  32. // If the answer text is blank, skip this iteration.
  33. if (answer == "") {
  34. continue;
  35. }
  36.  
  37. if (question.length > 256){
  38. question = question.substring(0, 220) + "...";
  39. }
  40.  
  41. // For loop to iterate through the parts of an answer
  42. for (var j = 0; j < parts.length; j++) {
  43. // Add the number of characters in a part to the total character count of this embed
  44. currentEmbedCharacterNum += parts[j].length + question.length;
  45. // If the total character count with this part included pushes the total over 5000 (Discord Embeds have a character limit)
  46. if (currentEmbedCharacterNum >= 5000){
  47. // Send away an embed with the current items data
  48. sendEmbed(items);
  49. // Wait a second so Discord doesn't get overwhelmed with requests
  50. Utilities.sleep(50)
  51. // Reset the character count and items array
  52. currentEmbedCharacterNum = 0;
  53. items = [];
  54. }
  55.  
  56. // If there are multiple parts to an answer, add a (cont.) to the names.
  57. if (j == 0) {
  58. items.push({
  59. "name": question,
  60. "value": parts[j],
  61. "inline": false
  62. });
  63. } else {
  64. items.push({
  65. "name": question.concat(" (cont.)"),
  66. "value": parts[j],
  67. "inline": false
  68. });
  69. }
  70. }
  71. }
  72. // Send an embed to the webhook.
  73. sendEmbed(items);
  74.  
  75. };
  76.  
  77. // Function to send an embed to the webhook url.
  78. function sendEmbed(items){
  79. // Package the embed options into a variable
  80. var options = {
  81. "method": "post",
  82. "headers": {
  83. "Content-Type": "application/json",
  84. },
  85. "payload": JSON.stringify({
  86. "embeds": [{
  87. "title": "Some nice embed title here",
  88. "color": 33023, // This is optional, you can look for decimal colour codes at https://convertingcolors.com/
  89. "fields": items,
  90. "footer": {
  91. "text": "Some embed footer here"
  92. }
  93. }]
  94. })
  95. // "payload": JSON.stringify({
  96. // "embeds": [{
  97. // "title": "Some nice embed title here",
  98. // "color": 33023, // This is optional, you can look for decimal colour codes at https://convertingcolors.com/
  99. // "fields": items,
  100. // "footer": {
  101. // "text": "Some embed footer here"
  102. // }
  103. // }]
  104. // })
  105. };
  106.  
  107. // Post the data to the webhook.
  108. UrlFetchApp.fetch(POST_URL, options);
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement