-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
121 lines (99 loc) · 3.57 KB
/
index.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// index.js
const express = require("express");
const bodyParser = require("body-parser");
const cors = require("cors");
const { clientId } = require("./config.json");
const DiscordRPC = require("discord-rpc");
const rpc = new DiscordRPC.Client({ transport: "ipc" });
const WebSocket = require("ws");
const app = express();
app.use(bodyParser.json());
app.use(cors());
const PORT = 3000;
let songInfo = {};
let wsClients = [];
app.post("/updateRichPresence", (req, res) => {
const receivedSongInfo = req.body;
// console.log("Received song info:", receivedSongInfo);
songInfo = receivedSongInfo;
updateRichPresence(songInfo);
wsClients.forEach((client) => {
client.send(JSON.stringify(songInfo));
});
res.sendStatus(200);
});
// Initialize WebSocket server
const wss = new WebSocket.Server({ port: PORT + 1 });
wss.on("connection", function connection(ws) {
wsClients.push(ws);
ws.send(JSON.stringify(songInfo));
ws.on("close", function close() {
wsClients = wsClients.filter((client) => client !== ws);
});
});
rpc.on("ready", () => {
console.log("Discord RPC connected");
});
function updateRichPresence(songInfo) {
const elapsedSeconds = songInfo.elapsed || 0;
const totalSeconds = songInfo.total || 0;
const elapsedMinutes = Math.floor(elapsedSeconds / 60);
const elapsedSecondsRemainder = elapsedSeconds % 60;
const totalMinutes = Math.floor(totalSeconds / 60);
const totalSecondsRemainder = totalSeconds % 60;
const elapsedFormatted = `${elapsedMinutes}:${elapsedSecondsRemainder
.toString()
.padStart(2, "0")}`;
const totalFormatted = `${totalMinutes}:${totalSecondsRemainder
.toString()
.padStart(2, "0")}`;
const progressBarLength = 5;
const elapsedPercentage = (elapsedSeconds / totalSeconds) * 100;
const elapsedProgressBarLength = Math.floor(
(elapsedPercentage / 100) * progressBarLength
);
const remainingProgressBarLength =
progressBarLength - elapsedProgressBarLength;
const progressBar =
"•".repeat(elapsedProgressBarLength) +
"—".repeat(remainingProgressBarLength);
const currentTime = new Date().getTime();
const startTime = currentTime - elapsedSeconds * 1000;
const endTime = startTime + totalSeconds * 1000;
const progressBarAndTimeline = `${progressBar} | ${elapsedFormatted} / ${totalFormatted}`;
const tinyimage = songInfo.isPlaying
? "https://raw.githubusercontent.com/SaolGhra/richpresence/main/assets/pause60.png"
: "https://raw.githubusercontent.com/SaolGhra/richpresence/main/assets/play60.png";
// Set Rich Presence
rpc.setActivity({
ActivityType: "Listening to YouTube Music",
details: `${songInfo.title || ""}\n-\n${songInfo.album || ""}`,
state: `${songInfo.artist || ""}\n${progressBarAndTimeline}`,
largeImageKey: songInfo.thumbnail,
largeImageText: songInfo.title || "",
smallImageKey: tinyimage,
smallImageText: `${songInfo.isPlaying ? "Playing" : "Paused"}`,
buttons: [{ label: "YouTube Music", url: songInfo.url }],
instance: false,
party: { id: "party_id" },
startTimestamp: startTime,
endTimestamp: endTime,
assets: {
largeImage: songInfo.thumbnail,
largeText: songInfo.title || "",
smallImageKey: tinyimage,
smallText: songInfo.isPlaying ? "Playing" : "Paused",
},
secrets: {
match: "match_secret",
join: "join_secret",
spectate: "spectate_secret",
},
buttons: [{ label: "Listen on YouTube Music", url: songInfo.url }],
});
}
rpc.login({ clientId }).catch(console.error);
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
// — •