'Send message to discord via google apps script

I'd like to send a bot message in a discord channel via google apps script when a certain event is triggered, but I don't know where to start. Is this even possible? If not, is there a way to do it via github?

EDIT: I have figured out how to get the OAuth tokens, now how do I make the bot send a message?



Solution 1:[1]

I know that there's pretty much no chance that the OP still needs this answer, but I'm putting it up here so that others who google this question will be able to find the answer

var webhooks = {
  test: "Obtain a webhook for the channel you'd like and put it here."
};

function sendMessage(message, channel)
{
  if(webhooks.hasOwnProperty(channel))
    var url = webhooks[channel];
  else {
    Logger.log("Error Sending Message to Channel " + channel);
    return "NoStoredWebhookException";
  }
  
  var payload = JSON.stringify({content: message});
  
  var params = {
    headers: {"Content-Type": "application/x-www-form-urlencoded"},
    method: "POST",
    payload: payload,
    muteHttpExceptions: true
  };
  
  var res = UrlFetchApp.fetch(url, params);
  Logger.log(res.getContentText());
}

// to call and send a message
sendMessage("Hi!", "test");

This is what I typically use for sending messages. Unfortunately, there's no way to receive triggers from the webhooks to my knowledge.

Note: The above answer references discord.js, which is not compatible with Google Apps Script

Solution 2:[2]

To start with, here is a documentation from discord.js.

To let your apps script communicate with discord, you can check the External APIs

Google Apps Script can interact with APIs from all over the web. This guide shows how to work with different types of APIs in your scripts.

Examples are available in the provided documentations.

See these useful links for further details.

Solution 3:[3]

Super easy. Just go to your Discord channel, choose "Edit Channel" > "Webhooks". You name the bot and set its profile picture. It will give you a webhook URL that already contains the authorization token. Then you just POST to that public URL. TADA, a message will appear in the given channel, sent by the bot.

function postMessageToDiscord(message) {

  message = message || "Hello World!";

  var discordUrl = 'https://discordapp.com/api/webhooks/labnol/123';
  var payload = JSON.stringify({content: message});

  var params = {
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    method: "POST",
    payload: payload,
    muteHttpExceptions: true
  };

  var response = UrlFetchApp.fetch(discordUrl, params);

  Logger.log(response.getContentText());

}

Source: https://ctrlq.org/code/20563-post-message-to-discord-webhooks

Solution 4:[4]

For anyone still looking and not having luck with previous answers, like me:

After making my message_string, this snippet successfully sent to my desired channel's webhook:

    // Send the message to the Discord channel webhook.
  let options = {
          "method": "post",
          "headers": {
              "Content-Type": "application/json",
          },
          "payload": JSON.stringify({
              "content": message_string
          })
      };
      Logger.log(options, null, 2);
      UrlFetchApp.fetch("your_webhook_url_here", options);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 koolkats99
Solution 2 MαπμQμαπkγVπ.0
Solution 3 andras
Solution 4 Garance