-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
119 lines (107 loc) · 2.46 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
const { app, BrowserWindow, ipcMain, Menu } = require("electron");
const DiscordRPC = require("discord-rpc");
const path = require("path");
const clientId = "900755240532471888";
DiscordRPC.register(clientId);
const rpc = new DiscordRPC.Client({ transport: "ipc" });
rpc.login({ clientId });
app.rpc = true;
rpc.on("ready", () => {
console.log("RPC Connected");
});
const menu = Menu.buildFromTemplate([
{
label: "App",
submenu: [
{
label: "Open dev tools",
click() {
BrowserWindow.getFocusedWindow().webContents.openDevTools();
},
},
{
label: "Restart",
click() {
app.relaunch();
app.exit();
},
},
{
label: "Exit",
click() {
app.quit();
},
},
],
},
{
label: "Discord",
submenu: [
{
label: "Connect / Disconnect",
click() {
app.rpc = !app.rpc;
},
},
],
},
]);
const createWindow = async () => {
const win = new BrowserWindow({
title: "Stopify",
icon: "build/icon.ico",
minWidth: 1200,
minHeight: 800,
webPreferences: {
nodeIntegration: process.env.ELECTRON_NODE_INTEGRATION,
contextIsolation: !process.env.ELECTRON_NODE_INTEGRATION,
preload: path.join(__dirname, "preload.js"),
},
});
win.setMenu(menu);
await win.loadURL(
process.env.NODE_ENV === "development"
? "http://localhost:3000"
: "https://stopify.silentjungle.me/"
);
win.webContents.setWindowOpenHandler(({ url }) => {
require("electron").shell.openExternal(url);
return { action: "deny" };
});
};
ipcMain.on("playerState", (event, data) => {
if (rpc.user && data.song && data.playing && data.knownUsers && app.rpc) {
const listenWith =
data.knownUsers.length > 1
? data.knownUsers.length + " users"
: data.knownUsers?.[0]?.username;
rpc.setActivity({
details: data.song.title + " - " + data.song.artist,
largeImageKey: data.song.thumbnail,
largeImageText: data.song.title,
smallImageKey: "https://i.imgur.com/YFoY7BO.png",
smallImageText: "Playing on stopify",
startTimestamp: Date.now() - data.currentTime * 1000,
buttons: [
{
label: `Listen with ` + (listenWith || "me"),
url: "https://stopify.silentjungle.me/",
},
],
});
} else {
rpc.clearActivity();
}
});
app.on("window-all-closed", () => {
rpc.clearActivity();
if (process.platform !== "darwin") {
app.quit();
}
});
app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) createWindow();
});
app.whenReady().then(() => {
createWindow();
});