bw55555
bw55555

Reputation: 31

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?

Upvotes: 3

Views: 10870

Answers (6)

Dustin Yang
Dustin Yang

Reputation: 1

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/json"},
    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");

Upvotes: 0

lampski
lampski

Reputation: 1

I can't make a comment yet, but if you are having issues with one of the above answers - try changing the content type from:'Content-Type':"application/x-www-form-urlencoded" to 'Content-Type':"application/json".

Upvotes: 0

Garance
Garance

Reputation: 21

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);

Upvotes: 2

andras
andras

Reputation: 6729

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

Upvotes: 1

koolkats99
koolkats99

Reputation: 41

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

Upvotes: 4

MαπμQμαπkγVπ.0
MαπμQμαπkγVπ.0

Reputation: 6737

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.

Upvotes: 1

Related Questions