Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 274
bw55555 Avatar asked Jan 18 '26 05:01

bw55555


1 Answers

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

like image 114
koolkats99 Avatar answered Jan 19 '26 18:01

koolkats99



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!