|
| 1 | +import { Message } from "../../types/message"; |
| 2 | +import log from "../components/utils/log"; |
| 3 | +import axios from "axios"; |
| 4 | + |
| 5 | +export const info = { |
| 6 | + command: "wakatime", |
| 7 | + description: "Shows current WakaTime tasks for today.", |
| 8 | + usage: "wakatime", |
| 9 | + example: "wakatime", |
| 10 | + role: "user", |
| 11 | + cooldown: 5000, |
| 12 | +}; |
| 13 | + |
| 14 | +export default async function (msg: Message) { |
| 15 | + if (!/^wakatime/i.test(msg.body)) return; |
| 16 | + |
| 17 | + const apiKey = process.env.WAKATIME_API_KEY; |
| 18 | + if (!apiKey) { |
| 19 | + await msg.reply("WakaTime API key is not configured."); |
| 20 | + return; |
| 21 | + } |
| 22 | + |
| 23 | + const today = new Date().toISOString().split("T")[0]; // YYYY-MM-DD |
| 24 | + |
| 25 | + const response = await axios.get( |
| 26 | + `https://wakatime.com/api/v1/users/current/heartbeats`, |
| 27 | + { |
| 28 | + params: { date: today }, |
| 29 | + headers: { |
| 30 | + Authorization: `Basic ${Buffer.from(apiKey + ":").toString("base64")}`, |
| 31 | + }, |
| 32 | + }, |
| 33 | + ); |
| 34 | + |
| 35 | + const heartbeats = response.data.data; |
| 36 | + if (!heartbeats || heartbeats.length === 0) { |
| 37 | + await msg.reply("The author didn't do any job today unfortunately 😢"); |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + // Sort by time |
| 42 | + heartbeats.sort((a: any, b: any) => a.time - b.time); |
| 43 | + |
| 44 | + const projectDurations: Record<string, number> = {}; |
| 45 | + |
| 46 | + for (let i = 0; i < heartbeats.length; i++) { |
| 47 | + const hb = heartbeats[i]; |
| 48 | + const nextHb = heartbeats[i + 1]; |
| 49 | + |
| 50 | + if (!hb.project) continue; |
| 51 | + |
| 52 | + let duration = 0; |
| 53 | + if (nextHb) { |
| 54 | + duration = Math.min(120, nextHb.time - hb.time); // max 2 minutes per heartbeat |
| 55 | + } else { |
| 56 | + duration = 60; // assume 1 minute for last heartbeat |
| 57 | + } |
| 58 | + |
| 59 | + projectDurations[hb.project] = |
| 60 | + (projectDurations[hb.project] || 0) + duration; |
| 61 | + } |
| 62 | + |
| 63 | + const projects = Object.keys(projectDurations); |
| 64 | + if (projects.length === 0) { |
| 65 | + await msg.reply("The author didn't do any job today unfortunately 😢"); |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + const projectList = projects.map((proj) => { |
| 70 | + const seconds = projectDurations[proj]; |
| 71 | + const hours = Math.floor(seconds / 3600); |
| 72 | + const minutes = Math.floor((seconds % 3600) / 60); |
| 73 | + return `- ${proj}: ${hours}h ${minutes}m`; |
| 74 | + }); |
| 75 | + |
| 76 | + const replyText = `\`Today's WakaTime projects\`\n${projectList.join("\n")}`; |
| 77 | + await msg.reply(replyText); |
| 78 | +} |
0 commit comments