1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
const CHANNEL_POST_URL = "這邊放 Discord 的 channel url"; const CALENDAR_ID = "這邊放 google 日曆的 ID"; const NO_VALUE_FOUND = "N/A"; const minsInAdvance = 60;
eval(UrlFetchApp.fetch('https://cdn.jsdelivr.net/npm/luxon@2.0.2/build/global/luxon.min.js').getContentText()); let DateTime = luxon.DateTime; const DTnow = DateTime.now().startOf('minute');
function postEventsToChannel() { let optionalArgs = { timeMin: DTnow.toISO(), timeMax: DTnow.plus({minutes: minsInAdvance}).toISO(), showDeleted: false, singleEvents: true, orderBy: 'startTime' }; let response = Calendar.Events.list(CALENDAR_ID, optionalArgs); let events = response.items; if (events.length > 0) { for (i = 0; i < events.length; i++) { let event = events[i]; let ISOStartDate = event.start.dateTime || event.start.date; let ISOEndDate = event.end.dateTime || event.end.date;
let OptionsPayload = { "content": "<@&1094230208527208539>", "embeds": [{ "author": { "name": `${event.summary}`, "icon_url": "https://cdn.discordapp.com/attachments/696400605908041794/888874282950750238/1200px-Google_Calendar_icon_28202029.png" }, "timestamp": DTnow.toISO(), "description":`[Google Event Link](${event.htmlLink})`, "color": 1425196, "fields":[] }] };
let FieldsStartTime = { "name":"Start Time", "value": ISOToDiscordUnix(ISOStartDate) ?? NO_VALUE_FOUND, "inline":false }; let FieldsEndTime = { "name":"End Time", "value":ISOToDiscordUnix(ISOEndDate) ?? NO_VALUE_FOUND, "inline":false }; let FieldsLocation = { "name":"Location", "value":event.location ?? NO_VALUE_FOUND, "inline":false }; let FieldsDescription = { "name":"Description", "value":event.description ?? NO_VALUE_FOUND, "inline":false };
if(ISOStartDate) { OptionsPayload.embeds[0].fields.push(FieldsStartTime); }
if(ISOEndDate) { OptionsPayload.embeds[0].fields.push(FieldsEndTime); }
if(event.location) { OptionsPayload.embeds[0].fields.push(FieldsLocation); }
if(event.description) { OptionsPayload.embeds[0].fields.push(FieldsDescription); }
let options = { "method": "post", "headers": { "Content-Type": "application/json", }, "payload": JSON.stringify(OptionsPayload) };
Logger.log(options, null, 2); UrlFetchApp.fetch(CHANNEL_POST_URL, options); } } else { Logger.log(`No events starting within ${minsInAdvance} minute(s) found.`); } }
function ISOToDiscordUnix(isoString) { return `<t:${Math.floor(DateTime.fromISO(isoString).toSeconds())}:F>` }
|