Skip to content

Commit

Permalink
better embeds and resolve #2
Browse files Browse the repository at this point in the history
  • Loading branch information
ApocalypseCalculator committed May 8, 2023
1 parent 9a3a210 commit f0f9e31
Showing 1 changed file with 29 additions and 16 deletions.
45 changes: 29 additions & 16 deletions lib/push.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,41 @@ const axios = require('axios').default;
const Discord = require('discord.js');
const fs = require('fs');
const arrayLib = require('./array');
const config = require('../data/config');

module.exports.start = (client) => {
// read cached values before refresh
let rawd2 = fs.readFileSync('./data/cache.json');
let parsed2 = JSON.parse(rawd2);
refreshCache().then(() => {
let rawd = fs.readFileSync('./data/cache.json');
let parsed = JSON.parse(rawd);
let prawd = fs.readFileSync('./data/data.json');
let pparsed = JSON.parse(prawd);
//compare
let sendlist = compareAnnouncements(parsed2, parsed);
propogate(pparsed.push, sendlist, client);
}).catch();

staleCache(parseInt(parsed2.lastupdate)).then((stale) => {
if (stale) {
refreshCache().then(() => {
// read cached values after refresh
let rawd = fs.readFileSync('./data/cache.json');
let parsed = JSON.parse(rawd);
let prawd = fs.readFileSync('./data/data.json'); // data such as propagation targets
let pparsed = JSON.parse(prawd);
//compare
let sendlist = compareAnnouncements(parsed2.results, parsed.results);
propagate(pparsed.push, sendlist, client);
}).catch();
}
});
}

async function staleCache(lastupdate) {
let res = await axios.head(`https://maclyonsden.com/api/v3/obj/announcement?limit=${config.queryLimit}`);
return new Date(res.headers['last-modified']).getTime() > lastupdate;
}

module.exports.refreshCache = refreshCache;

function refreshCache() {
return new Promise((resolve, reject) => {
axios.get('https://maclyonsden.com/api/announcements').then(res => {
axios.get(`https://maclyonsden.com/api/v3/obj/announcement?limit=${config.queryLimit}`).then(res => {
try {
let rawd = JSON.stringify(res.data);
let rawd = JSON.stringify({ lastupdate: Date.now(), results: res.data.results });
fs.writeFileSync('./data/cache.json', rawd);
resolve();
}
Expand All @@ -39,7 +52,7 @@ function compareAnnouncements(cached, received) {
received.forEach(e => {
let indx = arrayLib.getElementByProperty(cached, "id", e.id);
if (indx == -1) {
e.recStatus = "new"; //new announcement to propogate
e.recStatus = "new"; //new announcement to propagate
difflist.push(e);
}
else {
Expand All @@ -64,7 +77,7 @@ function getImageURL(src) {
};
}

function propogate(sublist, difflist, client) {
function propagate(sublist, difflist, client) {
let ctr = 0;
let interval = setInterval(function () {
if (!sublist[ctr]) {
Expand Down Expand Up @@ -111,15 +124,15 @@ function propogate(sublist, difflist, client) {
});
}
let desc = `\n**${e.title}**\n\n${e.body}`;
embed.setAuthor(`${e.author.slug}`, '', `https://maclyonsden.com/user/${e.author.slug}`);
embed.setAuthor(`${e.author.first_name} ${e.author.last_name}`, `https://maclyonsden.com${e.organization.icon}`, `https://maclyonsden.com/user/${e.author.username}`);
embed.setTitle(`${e.recStatus == "new" ? "New" : "Edited"} announcement from ${e.organization.slug}`.toUpperCase());
embed.setDescription(`${desc.substring(0, 2000)}${(desc.length > 2000) ? '...' : ''}\n\n\n`);
if ((img = getImageURL(desc)) !== null) embed.setThumbnail(img.url.startsWith("http") ? img.url : `https://maclyonsden.com${img.url}`); // TODO: how to add alt text to image?
embed.setURL(`https://maclyonsden.com/announcement/${e.id}`);
embed.setColor(`${e.tags[0] ? e.tags[0].color.slice(1) : '36314a'}`);
embed.addField(`Club`, `[${e.organization.slug}](https://maclyonsden.com/club/${e.organization.slug})`, true);
embed.addField(`Club`, `[${e.organization.name}](https://maclyonsden.com/club/${e.organization.slug})`, true);
embed.addField(`Tags`, `${tags}`, true);
embed.addField(`Create & Edit Date`, `Creation: ${new Date(`${e.created_date}`).toLocaleString("en-US", { timeZone: "EST" })}\nEdited: ${new Date(`${e.last_modified_date}`).toLocaleString("en-US", { timeZone: "EST" })}`, true);
embed.addField(`Create & Edit Date`, `Creation: <t:${new Date(`${e.created_date}`).getTime() / 1000 | 0}>\nEdited: <t:${new Date(`${e.last_modified_date}`).getTime() / 1000 | 0}>`, true);
embed.setFooter('Metroidpolis Bot by Project Metropolis');
if (!nosend) {
embedlist.push(embed);
Expand Down

0 comments on commit f0f9e31

Please sign in to comment.