forked from ThatOneCalculator/DiscordRPCMaker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
263 lines (232 loc) · 7.06 KB
/
main.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// libappindicator-gtk3 on Arch as req
const { app, BrowserWindow, Notification, Menu, MenuItem, ipcMain, Tray, dialog, protocol } = require("electron")
const path = require('path')
const fs = require('fs')
const os = require('os')
const RPC = require('discord-rpc')
const EventEmitter = require('events')
require('@electron/remote/main').initialize()
const iconpath = path.join(__dirname, "/assets/icon.png")
const loadingEvents = new EventEmitter()
const slash = os.platform() == 'win32' ? "\\" : "/"
let dir = `${os.userInfo().homedir}/${process.platform === 'win32' ? '/AppData/Roaming/drpcm/' : '/.config/drpcm/'}`
let opendir = dir.replaceAll("/", "\\").replaceAll("\\\\", "\\")
let win = null
const args = process.argv.slice(2)
const id = args[0]
if (id !== undefined) {
const slash = os.platform() == 'win32' ? "\\" : "/"
const dir = `${os.userInfo().homedir}/${process.platform === 'win32' ? '/AppData/Roaming/drpcm/' : '/.config/drpcm/'}`
let client = new RPC.Client({ transport: 'ipc' })
let opendir = dir.replace("/", "\\").replace("\\\\", "\\")
let fullpath = os.platform() == "win32" ? opendir + "\\" + id + ".json" : dir + "/" + id + ".json"
let settingspath = os.platform() == "win32" ? opendir + "\\" + "settings.json" : dir + "/" + "settings.json"
let options = JSON.parse(fs.readFileSync(fullpath, 'utf8'))
let settings = JSON.parse(fs.readFileSync(settingspath, 'utf8'))
let activity = {}
let assets = {}
if (options.largeimage !== '') {
activity.largeImageKey = options.largeimage
// If you change this and some asks about this, please still give me credit :)
activity.largeImageText = "Made with ThatOneCalculator's Discord RPC Maker (v2.1.1 CLI)!"
}
if (options.smallimage !== '') {
activity.smallImageKey = options.smallimage
// Same applies with assets.large_text
activity.smallImageText = 'https://drpcm.t1c.dev/'
}
if (assets !== {}) { activity.assets = assets }
if (options.description !== '') { activity.details = options.description }
if (options.state !== '') { activity.state = options.state }
if (options.buttons.length !== 0) { activity.buttons = options.buttons }
if (settings.showtimestamp == true) {
activity.startTimestamp = Date.now()
}
function assembleClient(timeout = 5000) {
client.destroy()
client = new RPC.Client({ transport: 'ipc' })
client.on('ready', () => {
running = true;
client.setActivity(activity);
client.transport.socket.on("close", (c, s) => {
assembleClient()
})
})
setTimeout(() => client.login({ clientId: options.clientid }), timeout)
}
process.on("unhandledRejection", e => {
if (e.message === "Could not connect") {
console.log("Crashed! Retrying...")
assembleClient()
}
})
assembleClient(1000)
console.log("Started!")
}
else {
function createWindow() {
win = new BrowserWindow({
width: 1200,
height: 700,
minWidth: 900,
minHeight: 600,
webPreferences: {
contextIsolation: true,
nodeIntegration: true,
enableRemoteModule: true,
preload: path.join(__dirname, "preload.js"),
icon: iconpath
}
})
let settings = {}
if (!fs.existsSync(dir)) {
initialdata = {
launchedpresence: false,
language: "english",
theme: "dark",
quitonx: false,
showtimestamp: false
}
if (os.platform() == "win32") {
fs.mkdirSync(opendir, { recursive: true })
}
else {
fs.mkdirSync(dir, { recursive: true })
}
fs.copyFileSync(`${path.join(__dirname, `${slash}themes${slash}dark.css`)}`, `${dir}${slash}custom.css`)
fs.writeFile(`${dir}${slash}settings.json`, JSON.stringify(initialdata, null, 2), 'utf8', (err) => {
if (err) { throw err }
else { console.log("First launch") }
})
//welcome message
const msg = {
type: 'question',
buttons: [],
defaultId: 0,
title: 'Welcome',
message: 'Thank you for choosing Discord RPC Maker!',
detail: 'If you need instructions, click the question (?) icon in the bottom right.',
};
dialog.showMessageBox(null, msg)
}
try {
let settingspath = os.platform() == "win32" ? opendir + "\\" + "settings.json" : dir + "/" + "settings.json"
settings = JSON.parse(fs.readFileSync(settingspath, 'utf8'))
}
catch (e) {
console.log(e)
fs.mkdirSync(dir, { recursive: true })
settings = {
launchedpresence: false,
language: "english",
theme: "dark",
quitonx: false,
showtimestamp: false
}
fs.writeFileSync(`${dir}${slash}settings.json`, JSON.stringify(settings, null, 2), 'utf8', (err) => {
if (err) { throw err }
else { console.log("Wrote base settings") }
})
}
if (settings['quitonx'] == false) {
win.on('minimize', function (event) {
event.preventDefault();
win.hide();
})
win.on('close', function (event) {
if (!app.isQuiting) {
event.preventDefault();
win.hide();
}
return false;
})
}
win.setIcon(iconpath)
//win.setResizable(false);
const menu = Menu()
win.setMenuBarVisibility(false)
if (os.platform() !== 'win32') {
//start loading screen
win.loadFile("loading.html")
loadingEvents.on('finished', () => {
win.loadFile('index.html')
})
//load for one second, then do an internet check
setTimeout(() => {
require('dns').resolve("https://drpcm.t1c.dev", function (err) {
if (err) {
console.log(`\n\nERROR HERE: ${err.message}\n\n`)
noInternet(win)
} else {
setTimeout(() => loadingEvents.emit('finished'), 250)
}
})
}, 150)
function noInternet() {
require('dns').resolve("https://drpcm.t1c.dev", function (err) {
if (err) {
win.webContents.send("no-internet")
console.log("sending no internet")
setTimeout(noInternet, 5000)
} else {
//when we connect just stop loading
loadingEvents.emit('finished')
}
})
}
}
else {
win.loadFile("index.html")
}
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
})
app.on('before-quit', () => {
win.removeAllListeners('close');
win.close();
})
}
const gotTheLock = app.requestSingleInstanceLock()
if (!gotTheLock) {
app.quit()
}
else {
app.on('second-instance', (event, commandLine, workingDirectory) => {
// Someone tried to run a second instance, we should focus our window.
if (win) {
if (win.isMinimized()) win.restore()
win.focus()
win.show()
}
})
// Create myWindow, load the rest of the app, etc...
app.whenReady().then(() => {
createWindow()
win = BrowserWindow.getAllWindows()[0]
if (os.platform() == "darwin") {
appIcon = new Tray(path.join(__dirname, "/assets/iconTemplate.png"))
}
else {
appIcon = new Tray(iconpath)
}
const contextMenu = new Menu()
contextMenu.append(new MenuItem({
label: 'Show Discord RPC Maker',
click: () => { app.isquitting = true; win.show() }
}))
contextMenu.append(new MenuItem({
label: 'Quit Discord RPC Maker',
click: () => { app.quit() }
}))
appIcon.setContextMenu(contextMenu)
appIcon.setToolTip("Discord RPC Maker")
})
}
app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow()
}
})
}