Skip to content

Instantly share code, notes, and snippets.

@squigglezworth
Last active May 12, 2022 19:03
Show Gist options
  • Save squigglezworth/37309b6366948d34f16d15aeac1d703a to your computer and use it in GitHub Desktop.
Save squigglezworth/37309b6366948d34f16d15aeac1d703a to your computer and use it in GitHub Desktop.
Apps Script code for sending game quick tips to a Discord webhook
function sendTip() {
const HOOK = "";
const LOGO = 'https://media.discordapp.net/attachments/525755278172356625/970950652815626280/Hideaway_-_Logo.png?width=486&height=486';
const COLOR = 0x299aff;
// Get our 'Tips' sheet...
var book = SpreadsheetApp.getActiveSpreadsheet();
var sheet = book.getSheetByName("Tips");
// ... and then get all the tips out of it ...
var tips = sheet.getRange(1, 1, sheet.getLastRow(), 3).getValues();
// ... filter the ones we've sent this 'cycle' ...
var ftips = tips.filter(tip => tip[0] == 0);
// ... if we've already cycled through them all, reset the list ...
if (ftips.length == 0) {
sheet.getRange(1,1,tips.length,1).setValue(0);
ftips = tips;
}
// ... and finally choose a tip at random
var i = (ftips.length * Math.random() | 0);
var tip = ftips[i-1];
// add a flag that this tip has been used in this cycle
// ... to do so, we have to get the original index of the tip
var f = tips.indexOf(tips.find(tip => tip[2] === ftips[i-1][2]))+1;
sheet.getRange(f,1,1,1).setValue(1);
// Build the Discord embed
var content = {
embeds: [
{
author: {
name: "EVE Tip #" + i,
// Community logo
icon_url: LOGO
},
thumbnail: {
url: LOGO
},
description: tip[2],
// Community color <3
color: COLOR
}]};
if(tip[1]) {
content.embeds[0]['image'] = {url: tip[1]};
}
// Setup the information for the request
var params = {
contentType: 'application/json',
method: "POST",
payload: JSON.stringify(content),
muteHttpExceptions: true,
};
// Finally send it!
var res = UrlFetchApp.fetch(HOOK, params);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment