Skip to content

Commit

Permalink
feat: add countdown command
Browse files Browse the repository at this point in the history
  • Loading branch information
sunny-zuo committed Jun 16, 2021
1 parent feee536 commit 32c8bfd
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
7 changes: 6 additions & 1 deletion .env.example
Expand Up @@ -3,4 +3,9 @@ DB_URL=
ADMIN_ID=
AES_PASSPHRASE=
SERVER_URI=
PORT=
PORT=
SERVER_URI=
CLIENT_ID=
TENANT_ID=
CLIENT_SECRET=
UW_API_KEY=
41 changes: 41 additions & 0 deletions commands/countdown.js
@@ -0,0 +1,41 @@
const Discord = require("discord.js");
const fetch = require("node-fetch");
const { DateTime } = require("luxon");

let examsEndDate;
let termEndDate;

module.exports = {
name: "countdown",
description: "Count the number of days until the current academic term is over",
aliases: ['fml', 'count'],
args: false,
guildOnly: false,
displayHelp: true,
async execute(message) {
if (!examsEndDate || DateTime.local() > termEndDate) {
const currentTermResponse = await fetch('https://openapi.data.uwaterloo.ca/v3/Terms/current', {
method: 'GET',
headers: {
'X-API-KEY': process.env.UW_API_KEY
}
});
const currentTerm = await currentTermResponse.json();
termEndDate = DateTime.fromISO(currentTerm.termEndDate, { zone: 'America/Toronto' });

const importantDateResponse = await fetch('https://openapi.data.uwaterloo.ca/v3/ImportantDates', {
method: 'GET',
headers: {
'X-API-KEY': process.env.UW_API_KEY
}
});
const importantDates = await importantDateResponse.json();

const examEndEvent = importantDates.find(date => date?.name === "Final examinations end" && date?.details?.some(detail => detail?.termName === currentTerm.name));
examsEndDate = DateTime.fromISO(examEndEvent.details.find(detail => detail?.termName === currentTerm.name ).startDate);
}

let timeDiff = examsEndDate.diff(DateTime.local(), 'days');
message.channel.send(`There are about ${Math.floor(timeDiff.values.days)} more days until this term is over!`);
}
};

0 comments on commit 32c8bfd

Please sign in to comment.