Skip to content

Commit

Permalink
Add a check for changes in study session message before reposting
Browse files Browse the repository at this point in the history
If people want to have notifications on for the events channel
it might get annoying having the ping go off even if there are no
changes
  • Loading branch information
Fox-Islam committed Jun 4, 2021
1 parent 1741c82 commit 5adc510
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions src/tasks/studySession/channelReminder.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,16 @@ export default function sendChannelReminder(client) {
const mostRecentStudySessionMessage = studySessionMessages.first();

if (isMessageOlderThan5HoursAgo(mostRecentStudySessionMessage)) {
mostRecentStudySessionMessage.delete().then(() => {
makeStudySessionMessage().then((studyMessage) => {
studySessionChannel.send(studyMessage).catch((error) => {
makeStudySessionMessage().then((studyMessage) => {
if (upcomingSessionsAreDifferent(mostRecentStudySessionMessage, studyMessage)) {
mostRecentStudySessionMessage.delete().then(() => {
studySessionChannel.send(studyMessage).catch((error) => {
console.log(error);
});
}).catch((error) => {
console.log(error);
});
});
}
}).catch((error) => {
console.log(error);
});
Expand Down Expand Up @@ -98,6 +102,21 @@ function getUpcomingStudySessionSummary(message) {
return truncatedString;
}

function upcomingSessionsAreDifferent(oldMessage, newMessage) {
const oldMessageEmbed = oldMessage.embeds[0];
const newMessageEmbed = newMessage.embed;
if (oldMessageEmbed.title !== newMessageEmbed.title) {
return true;
}
if (oldMessageEmbed.fields.length !== newMessageEmbed.fields.length) {
return true;
}
return oldMessageEmbed.fields.some((oldMessageEmbedField, index) => {
return newMessageEmbed.fields[index].name !== oldMessageEmbedField.name ||
newMessageEmbed.fields[index].value !== oldMessageEmbedField.value
});
}

function createNotFoundMessage() {
return {
content: upcomingStudySessionMessageContent,
Expand Down

0 comments on commit 5adc510

Please sign in to comment.