a fun bot for the hc slack

chore: move time functions to their own lib

dunkirk.sh a2984d2a 0f4982bb

verified
Changed files
+19 -18
src
features
libs
+5 -18
src/features/takes.ts
···
import { takes as takesTable } from "../libs/schema";
import { eq, and, desc } from "drizzle-orm";
import TakesConfig from "../libs/config";
+
import { generateSlackDate, prettyPrintTime } from "../libs/time";
type MessageResponse = {
blocks?: AnyMessageBlock[];
···
};
const takes = async () => {
-
// Helper function for pretty-printing time
-
const prettyPrintTime = (ms: number): string => {
-
const minutes = Math.round(ms / 60000);
-
if (minutes < 2) {
-
const seconds = Math.max(0, Math.round(ms / 1000));
-
return `${seconds} seconds`;
-
}
-
return `${minutes} minutes`;
-
};
-
// Helper functions for command actions
const getActiveTake = async (userId: string) => {
return db
···
const endTime = new Date(
newTake.startedAt.getTime() + newTake.durationMinutes * 60000,
);
-
const endTimeStr = `<!date^${Math.floor(endTime.getTime() / 1000)}^{time}|${endTime.toLocaleTimeString()}>`;
const descriptionText = description
? `\n\n*Working on:* ${description}`
: "";
return {
-
text: `🎬 Takes session started! You have ${prettyPrintTime(newTake.durationMinutes * 60000)} until ${endTimeStr}.${descriptionText}`,
+
text: `🎬 Takes session started! You have ${prettyPrintTime(newTake.durationMinutes * 60000)} until ${generateSlackDate(endTime)}.${descriptionText}`,
response_type: "ephemeral",
blocks: [
{
···
elements: [
{
type: "mrkdwn",
-
text: `You have ${prettyPrintTime(newTake.durationMinutes * 60000)} left until ${endTimeStr}.`,
+
text: `You have ${prettyPrintTime(newTake.durationMinutes * 60000)} left until ${generateSlackDate(endTime)}.`,
},
],
},
···
pausedSession.durationMinutes * 60000 +
(pausedSession.pausedTimeMs || 0),
);
-
const endTimeStr = `<!date^${Math.floor(endTime.getTime() / 1000)}^{time}|${endTime.toLocaleTimeString()}>`;
return {
text: `▶️ Takes session resumed! You have ${prettyPrintTime(pausedSession.durationMinutes * 60000)} remaining in your session.`,
···
elements: [
{
type: "mrkdwn",
-
text: `You have ${prettyPrintTime(pausedSession.durationMinutes * 60000)} remaining until ${endTimeStr}.`,
+
text: `You have ${prettyPrintTime(pausedSession.durationMinutes * 60000)} remaining until ${generateSlackDate(endTime)}.`,
},
],
},
···
endTime.setTime(endTime.getTime() + take.pausedTimeMs);
}
-
const endTimeStr = `<!date^${Math.floor(endTime.getTime() / 1000)}^{time}|${endTime.toLocaleTimeString()}>`;
-
const now = new Date();
const remainingMs = endTime.getTime() - now.getTime();
···
elements: [
{
type: "mrkdwn",
-
text: `You have ${prettyPrintTime(remainingMs)} remaining until ${endTimeStr}.`,
+
text: `You have ${prettyPrintTime(remainingMs)} remaining until ${generateSlackDate(endTime)}.`,
},
],
},
+14
src/libs/time.ts
···
+
// Helper function for pretty-printing time
+
export const prettyPrintTime = (ms: number): string => {
+
const minutes = Math.round(ms / 60000);
+
if (minutes < 2) {
+
const seconds = Math.max(0, Math.round(ms / 1000));
+
return `${seconds} seconds`;
+
}
+
return `${minutes} minutes`;
+
};
+
+
// Helper function that generates the slack date format
+
export const generateSlackDate = (endTime: Date): string => {
+
return `<!date^${Math.floor(endTime.getTime() / 1000)}^{time}|${endTime.toLocaleTimeString()}>`;
+
};