Skip to content

Commit

Permalink
Merge pull request #9 from kcoderhtml/slack-status-age-count
Browse files Browse the repository at this point in the history
slack-status-age-count
  • Loading branch information
taciturnaxolotl authored Feb 18, 2024
2 parents f0215af + 51fa7c4 commit 139c79b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
4 changes: 2 additions & 2 deletions content/message.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type Person = {
let usr: Person = {
name: "Kieran",
nomeDeGuerra: "Kieran",
age: 15.834,
age: {age},
roles: [
"student",
"developer",
Expand All @@ -28,7 +28,7 @@ let usr: Person = {
],
isAlive: true,
};

console.log(usr);

console.log("{greeting} This is my plain text site!");
console.log("{slack_status}");
15 changes: 15 additions & 0 deletions utils/files.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { getSlackStatus } from "./slack";

// get the greeting from a file and return a random greeting
async function getGreeting() {
const greetingFile = await Bun.file("content/greeting.txt").text();
Expand All @@ -9,5 +11,18 @@ async function getGreeting() {
export async function getMessage() {
let messageFile = await Bun.file("content/message.txt").text();
messageFile = messageFile.replace("{greeting}", await getGreeting());
// get a decimal value of the time since 2008-04-27
messageFile = messageFile.replace(
"{age}",
(
(Date.now() - new Date("2008-04-27").getTime()) /
1000 /
60 /
60 /
24 /
365
).toFixed(10),
);
messageFile = messageFile.replace("{slack_status}", await getSlackStatus());
return messageFile;
}
20 changes: 20 additions & 0 deletions utils/slack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,23 @@ export async function get10DaysLeaderboard(start: Date, end: Date) {

return leaderboardFormatted;
}

export async function getSlackStatus() {
// get slack status from the slack API
const response = await fetch(
"https://slack.com/api/users.getPresence?user=U062UG485EE&pretty=1",
{
headers: {
Authorization: `Bearer ${process.env.SLACK_OAUTH_TOKEN}`,
},
},
);

const status = await response.json();

return status.presence === "active"
? new Date().getHours() > 20 && new Date().getHours() < 8
? "I'm currently listed as active on slack but i probably forgot to set myself not active :)"
: "I'm currently listed as active on slack :)"
: "I'm currently listed as not active on slack :( catch you later?";
}

0 comments on commit 139c79b

Please sign in to comment.