-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.tsx
More file actions
87 lines (76 loc) · 2.38 KB
/
index.tsx
File metadata and controls
87 lines (76 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
* Vencord, a Discord client mod
* Copyright (c) 2024 Vendicated and contributors
* SPDX-License-Identifier: GPL-3.0-or-later
*/
import definePlugin, { OptionType } from "@utils/types";
import { definePluginSettings } from "@api/Settings";
const settings = definePluginSettings({
apiKey: {
type: OptionType.STRING,
description: "API Key for Wakatime",
default: "",
isValid: (value: string) => {
if (!value?.startsWith("waka_")) return "Invalid Key: Please obtain your API Key from Wakatime";
return true;
},
},
debug: {
type: OptionType.BOOLEAN,
description: "Enable debug mode",
default: false,
},
machineName: {
type: OptionType.STRING,
description: "Machine name",
default: "Vencord User",
},
projectName: {
type: OptionType.STRING,
description: "Project Name",
default: "Discord",
},
});
async function sendHeartbeat() {
const time = Date.now();
const { debug, apiKey, machineName, projectName } = settings.store
if (!apiKey) return;
if (debug) console.log("Sending heartbeat to WakaTime API.");
const url = "https://api.wakatime.com/api/v1/users/current/heartbeats";
const body = JSON.stringify({
time: time / 1000,
entity: "Discord",
type: "app",
project: projectName ?? "Discord",
plugin: "vencord/version discord-wakatime/v0.0.1",
});
const headers = {
Authorization: `Basic ${apiKey}`,
"Content-Type": "application/json",
"Content-Length": new TextEncoder().encode(body).length.toString(),
...(machineName ? { "X-Machine-Name": machineName } : {})
};
const res = await fetch(url, {
method: "POST",
body: body,
headers: headers,
});
const data = await res.text();
if (res.status !== 200) console.warn(`WakaTime API Error ${res.status}: ${data}`);
}
export default definePlugin({
name: "Wakatime",
description: "Fully automatic code stats via Wakatime",
authors: [
{ name: "Neon", id: 566766267046821888n },
{ name: "thororen", id: 848339671629299742n }
],
settings,
sendHeartbeat,
start() {
this.updateInterval = setInterval(() => { this.sendHeartbeat(); }, 120000);
},
stop() {
clearInterval(this.updateInterval);
}
});