-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
80 lines (70 loc) · 2.03 KB
/
script.js
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
const userID = "906037896295878706"; // Đặt Discord user ID của bạn vào đây
const statusImage = document.getElementById("status-image");
// Kết nối WebSocket để nhận trạng thái Discord
function startWebSocket() {
const ws = new WebSocket("wss://api.lanyard.rest/socket");
ws.onopen = () => {
// Gửi yêu cầu để subscribe đến user ID
ws.send(
JSON.stringify({
op: 2, // Subscribe operation
d: {
subscribe_to_id: userID,
},
})
);
};
ws.onmessage = (event) => {
const { t, d } = JSON.parse(event.data);
// Kiểm tra xem có sự kiện "INIT_STATE" hoặc "PRESENCE_UPDATE" hay không
if (t === "INIT_STATE" || t === "PRESENCE_UPDATE") {
updateStatus(d);
}
};
ws.onerror = (error) => {
console.error("Lỗi WebSocket:", error);
ws.close();
};
ws.onclose = () => {
console.log("WebSocket đóng, thử kết nối lại...");
setTimeout(startWebSocket, 1000); // Tự động kết nối lại sau 1 giây nếu WebSocket đóng
};
}
// Hàm cập nhật hình ảnh trạng thái Discord
function updateStatus(data) {
const { discord_status, activities } = data;
// Lấy đường dẫn hình ảnh tương ứng với trạng thái
let imagePath;
switch (discord_status) {
case "online":
imagePath = "./status/online.svg";
break;
case "idle":
imagePath = "./status/idle.svg";
break;
case "dnd":
imagePath = "./status/dnd.svg";
break;
case "offline":
imagePath = "./status/offline.svg";
break;
default:
imagePath = "";
break;
}
// Kiểm tra xem người dùng đang stream không
if (
activities.find(
(activity) =>
activity.type === 1 &&
(activity.url.includes("twitch.tv") || activity.url.includes("youtube.com"))
)
) {
imagePath = "/public/status/streaming.svg";
}
// Cập nhật ảnh trạng thái
statusImage.src = imagePath;
statusImage.alt = `Discord Status: ${discord_status}`;
}
// Bắt đầu WebSocket
startWebSocket();