Skip to content

Instantly share code, notes, and snippets.

@ChaseC99
Last active January 30, 2022 08:57
Show Gist options
  • Save ChaseC99/95f9e0ab0267f13d6edd5c0372f134e1 to your computer and use it in GitHub Desktop.
Save ChaseC99/95f9e0ab0267f13d6edd5c0372f134e1 to your computer and use it in GitHub Desktop.
How to connect Google Form Responses with Discord

Connecting Google Form Responses with Discord

This Gist describes how to use Google App Scripts to post a message on Discord whenever a Google Form is submitted.

  1. Create a Discord Webhook.
  2. Create a Google Form.
  3. Open the Google App Scripts Editor
    In the top right corner of the Google Form, click the vertical menu bar (⋮), and select "Script editor". image
  4. Create a function that does the following:
    • Loads the last response in the form.
    • Constructs an embeded message to send to Discord.
    • Sends the message to the Discord Webhook.
      You can refer to this example for how to do this.
  5. Test your code
    Save your file and then press the "▷ Run" button to test your code.
    Make sure your form has at least 1 response. This test will always pull the last submitted response.
    If the request to the webhook returns a response that says {"embeds": ["0"]}, then that means the payload was not formatted correctly. Unforunately this can be difficult to debug. Please refer this guide or the official Discord documentation.
  6. Add a Trigger Lastly, we need to add a trigger so this code is excecuted when a new form is submitted.
    On the left pane, click the "Triggers" icon that looks like an alarm clock, then press "Add Trigger".
    Make sure "Select event type" is set to "On form submit". image

Your Google Form should now be connected to your Discord channel 🎉
The message should look something like this: image

// This is an example script for how to connect a Google Form to a Discord channel
// It connects antalmanac.com's Feedback Form to the AntAlmanac #user-feedback channel
function postFeedbackToDiscord() {
// Load the form and it's responses
var form = FormApp.getActiveForm();
var formResponses = form.getResponses();
var newResponse = formResponses[formResponses.length-1]; // Get the last (newest) response
var itemResponses = newResponse.getItemResponses();
// Get the question responses that you want to include in the Discord message
// In this case, I want the first 3 question responses
var feedbackType = itemResponses[0].getResponse();
var feedbackResponse = itemResponses[1].getResponse();
var contactInfo = itemResponses[2].getResponse();
var fields = [
{
name: "Feedback Type",
value: feedbackType.toString()
},
{
name: "Feedback",
value: feedbackResponse.toString()
}
]
// Add Contact info to fields if contactInfo was provided
if (contactInfo) {
fields.push({
name: "Contact Info",
value: contactInfo.toString()
})
}
// Set the color to Red if the feedback is reporting an Issue / Bug
// Otherwise, set it to green
var statusColor = (feedbackType.toString() == "Issue / Bug") ? 15605837 : 1879160
// Construct the embeded message
var embededMessage = {
color: statusColor,
fields: fields
};
// Construct the post request
var url = "https://discord.com/api/webhooks/<secret>/<secret>";
var payload = JSON.stringify({embeds: [embededMessage]});
var params = {
headers: {"Content-Type": "application/json"},
method: "POST",
payload: payload,
muteHttpExceptions: true
};
// Send the post request to the Discord webhook
var res = UrlFetchApp.fetch(url, params);
// Log the response
Logger.log(res.getContentText());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment