This repository has been archived by the owner on Jul 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
624 lines (552 loc) · 17.2 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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
/* roPresence Client
* Created by JiveOff
* Thanks to ddavness for the awesome PRs!
*/
const APP_VERSION = require('./package.json').version
const UPDATE_SERVER = "https://ropresence-hazel.vercel.app"
const AUTO_UPDATE_URL = UPDATE_SERVER + "/update/" + process.platform + "/" + APP_VERSION
const DiscordRPC = require('discord-rpc')
const io = require('socket.io-client')
const Axios = require('axios')
const {
app,
autoUpdater,
Menu,
Tray,
Notification,
} = require('electron')
const Open = require('open')
const Config = require('./config/config.json')
const File = require('fs')
const operateWindows = process.platform === 'win32'
const path = require('path')
if (require('electron-squirrel-startup')) {
process.exit(0)
}
if (handleSquirrelEvent(app)) {
process.exit(0)
}
let tray
function handleSquirrelEvent (application) {
if (process.argv.length === 1) {
return false
}
const ChildProcess = require('child_process')
const path = require('path')
const appFolder = path.resolve(process.execPath, '..')
const rootAtomFolder = path.resolve(appFolder, '..')
const updateDotExe = path.resolve(path.join(rootAtomFolder, 'Update.exe'))
const exeName = path.basename(process.execPath)
const spawn = function (command, args) {
let spawnedProcess
try {
spawnedProcess = ChildProcess.spawn(command, args, { detached: true })
} catch (err) {
console.log(err)
}
return spawnedProcess
}
const spawnUpdate = function (args) {
return spawn(updateDotExe, args)
}
const squirrelEvent = process.argv[1]
switch (squirrelEvent) {
case '--squirrel-install':
case '--squirrel-updated':
spawnUpdate(['--createShortcut', exeName])
setTimeout(application.quit, 1000)
return true
case '--squirrel-uninstall':
spawnUpdate(['--removeShortcut', exeName])
setTimeout(application.quit, 1000)
return true
case '--squirrel-obsolete':
application.quit()
return true
}
}
async function logToFile (text) {
console.log(text)
File.appendFile('roPresence_log.txt', '\n' + text, (err) => {
if (err) throw err
})
}
const clientId = '595172822410592266'
const RPC = new DiscordRPC.Client({
transport: 'ipc'
})
let robloxUser
let elapsed = new Date()
let elapsedLoc = ''
let tipLoc = false
let loaded = false
let tipSuccess = false
let socketPresence = false
let socketPresencePlace
let busyRetrying = false
async function getRobloxPresence () {
if (operateWindows) {
try {
const bloxauth = require('./lib/bloxauth')
const res = await bloxauth.post({ url: 'https://presence.roblox.com/v1/presence/users', data: { userIds: [robloxUser.robloxId] } })
return {
request: {
status: 'ok',
userId: robloxUser.robloxId
},
presence: res.data
}
} catch (e) {
return false
}
} else {
return false
}
}
async function sendTip () {
if (tipLoc === false && Config.showTips === true) {
tipLoc = true
tipSuccess = false
await logToFile('roPresence Tip: To show your game name, head to the README.md in your folder or here: https://github.com/JiveOff/roPresence/blob/master/README.md#making-the-game-name-show')
const notification = new Notification({
title: 'roPresence Tip',
body: 'Click this notification to know how to make your game name appear.',
icon: path.join(__dirname, 'img/game.png'),
timeoutType: 'never'
})
notification.on('click', (event) => {
Open('https://github.com/JiveOff/roPresence/blob/master/README.md#making-the-game-name-show')
})
notification.show()
}
}
async function successTip () {
if (tipLoc === true && tipSuccess === false) {
tipSuccess = true
tipLoc = false
await logToFile('roPresence: Great! Your game names are now shown on your presence.')
const notification = new Notification({
title: 'roPresence',
body: 'Great! Your game names are now shown on your presence.',
timeoutType: 'never',
icon: path.join(__dirname, 'img/game.png')
})
notification.show()
}
}
async function exitRoPresence () {
await logToFile('roPresence: Stopping. Exiting cmd in 5 seconds.')
await RPC.destroy()
setTimeout(() => {
process.exit()
}, 5e3)
}
function updateTrayMenu() {
const contextMenu = Menu.buildFromTemplate([{
label: 'Logged in.',
type: 'normal'
},
{
label: 'Roblox: ' + robloxUser.robloxUsername,
type: 'normal'
},
{
label: 'Discord: ' + RPC.user.username + '#' + RPC.user.discriminator,
type: 'normal'
},
{
label: 'Item2',
type: 'separator'
},
{
label: 'Start on login',
type: 'checkbox',
checked: app.getLoginItemSettings().openAtLogin,
click: () => {
let settings = !app.getLoginItemSettings().openAtLogin;
app.setLoginItemSettings({
openAtLogin: settings,
path: app.getPath("exe")
});
updateTrayMenu()
}
},
{
label: 'Item2',
type: 'separator'
},
{
label: 'Exit roPresence',
type: 'normal',
click: async function () {
exitRoPresence()
tray.destroy()
}
},
{
label: 'View Logs',
type: 'normal',
click: function () {
Open('./roPresence_log.txt')
}
}
])
tray.setContextMenu(contextMenu)
}
async function setActivity () {
if (!RPC) {
return
}
let error = false
const presence = await getRobloxPresence()
if (presence === false || presence.request.status === 'error') {
error = true
}
if (error) {
await logToFile('roPresence API Error: roPresence ran into an error and had to stop. Please make sure that you opened Roblox Studio recently and re-open roPresence.\nPlease open Studio and restart roPresence.')
const notif = new Notification({
title: 'roPresence API Error',
body: 'Please make sure that you opened Roblox Studio recently and re-open roPresence.',
icon: path.join(__dirname, 'img/no.png')
})
notif.show()
await exitRoPresence()
return
}
console.log(presence)
const presenceInfo = presence.presence.userPresences[0]
if(socketPresence === true) {
if(presenceInfo.placeId !== socketPresencePlace) {
await logToFile('Socket Client: Clearing socket presence because not in game anymore.. ')
await setActivity()
socketPresence = false
}
return
}
let rpcInfo = {}
if (presenceInfo.lastLocation !== elapsedLoc) {
elapsed = new Date()
elapsedLoc = presenceInfo.lastLocation
}
switch (presenceInfo.userPresenceType) {
case 0:
if (Config.showOfflinePresence === true) {
rpcInfo.details = 'Not playing'
rpcInfo.state = 'IGN: ' + robloxUser.robloxUsername
elapsedLoc = 'Not playing'
} else {
rpcInfo = null
}
break
case 1:
if (Config.showWebsitePresence === true) {
rpcInfo.startTimestamp = elapsed
rpcInfo.details = 'On the website'
rpcInfo.smallImageKey = 'online'
rpcInfo.smallImageText = 'Browsing the website'
} else {
rpcInfo = null
}
break
case 2:
rpcInfo.startTimestamp = elapsed
if (presenceInfo.lastLocation === '') {
rpcInfo.details = 'Playing a secret game'
rpcInfo.smallImageKey = 'playing'
rpcInfo.smallImageText = 'A secret game'
await sendTip()
} else {
await successTip()
rpcInfo.details = 'Playing a game'
rpcInfo.state = presenceInfo.lastLocation
rpcInfo.smallImageKey = 'playing'
rpcInfo.smallImageText = presenceInfo.lastLocation
}
break
case 3:
rpcInfo.startTimestamp = elapsed
if (presenceInfo.lastLocation === '') {
rpcInfo.details = 'Creating a secret game'
rpcInfo.smallImageKey = 'creating'
rpcInfo.smallImageText = 'A secret game'
await sendTip()
} else {
await successTip()
rpcInfo.details = 'Creating on Studio'
rpcInfo.state = presenceInfo.lastLocation
rpcInfo.smallImageKey = 'creating'
rpcInfo.smallImageText = presenceInfo.lastLocation
}
break
default:
break
}
if (rpcInfo) {
rpcInfo.largeImageKey = 'logo'
if (Config.showUsernameInPresence === true) {
rpcInfo.largeImageText = robloxUser.robloxUsername + " is using roPresence " + APP_VERSION
} else {
rpcInfo.largeImageText = 'Hidden user'
}
rpcInfo.instance = false
await RPC.setActivity(rpcInfo)
} else {
await RPC.clearActivity()
}
if (loaded === false) {
loaded = true
await logToFile('roPresence Loaded: Glad to see you, ' + robloxUser.robloxUsername + '! Your presence will be updated once you interact with ROBLOX.')
const notification = new Notification({
title: 'roPresence Loaded',
body: 'Glad to see you, ' + robloxUser.robloxUsername + '!\nYour presence will be updated once you interact with ROBLOX.',
icon: path.join(__dirname, 'img/yes.png')
})
notification.show()
await logToFile('Presence API: Your Discord presence will now be updated every 15 seconds with the ' + robloxUser.robloxUsername + ' ROBLOX Account.')
updateTrayMenu()
}
}
// C:\Users\antoi\WebstormProjects\roPresence-electron\release-builds\ropresence-win32-x64
async function getRoverUser () {
const res = await Axios.get('https://verify.eryn.io/api/user/' + RPC.user.id)
return res.data
}
async function initSocket () {
await logToFile('Socket Client: Starting.')
const socket = io.connect('http://presences.jiveoff.fr/client/subSocket', {
reconnect: true,
query: {
robloxId: robloxUser.robloxId,
robloxUsername: robloxUser.robloxUsername,
clientVersion: APP_VERSION
}
})
socket.on('connect', () => {
logToFile('Socket Client: Connected to socket, authenticating.')
})
socket.on('disconnect', () => {
logToFile('Socket Client: Disconnected from socket, retrying to connect.')
})
socket.on('instanceReady', () => {
logToFile('Socket Client: Authenticated to socket, ready.')
})
socket.on('serverMessage', (msg) => {
logToFile('Remote Socket Server: ' + msg)
})
socket.on('setPresence', async (presence) => {
let prc = await getRobloxPresence();
if(prc.presence.userPresences[0].placeId === presence.place) {
await logToFile('Socket Client: Updating socket presence.. ')
await RPC.setActivity(presence.presence)
socketPresence = true
socketPresencePlace = presence.place
}
})
socket.on('clearPresence', async (presence) => {
let prc = await getRobloxPresence();
if(prc.presence.userPresences[0].placeId === presence.place) {
await logToFile('Socket Client: Clearing socket presence.. ')
await setActivity()
socketPresence = false
}
})
}
async function robloxVerify () {
if(robloxUser) {
await setActivity()
return
}
const result = await getRoverUser()
if (result.status === 'ok') {
robloxUser = result
await initSocket()
await setActivity()
} else {
if (busyRetrying) {
return
}
busyRetrying = true
await RPC.clearActivity()
await logToFile('roPresence Error: To use roPresence, please link your Discord account with verify.eryn.io')
const notification = new Notification({
title: 'roPresence Error',
body: 'To use roPresence, please link your Discord account with verify.eryn.io\n\nClick this bubble to get there.',
icon: path.join(__dirname, 'img/no.png'),
timeoutType: 'never'
})
notification.on('click', (event) => {
Open('https://verify.eryn.io/')
})
notification.show()
await logToFile('RoVer: API returned an error: ' + result.error)
let count = 0
const retry = setInterval(async () => {
const result = await getRoverUser()
await logToFile('RoVer: Retrying..')
if (result.status === 'ok') {
loaded = false
await init()
busyRetrying = false
clearInterval(retry)
} else {
if (count === 25) {
await logToFile('roPresence Error: We couldn\'t find your ROBLOX account in time, roPresence has been stopped. Relaunch it to retry.')
const notification = new Notification({
title: 'roPresence Error',
body: "We couldn't find your ROBLOX account in time, roPresence has been stopped.\nRelaunch it to retry!",
icon: path.join(__dirname, 'img/no.png'),
timeoutType: 'never'
})
notification.show()
await exitRoPresence()
}
count = count + 1
}
}, 3e3)
}
}
async function initUpdater() {
autoUpdater.on(
'error',
async (err) => await logToFile("roPresence Error: Autoupdater - " + err.message + "."))
autoUpdater.on(
'checking-for-update',
async () => {
await logToFile('roPresence Info: Autoupdater - Checking for updates...')
tray.setToolTip("roPresence - Checking for updates...")
})
autoUpdater.on(
'update-available',
async () => {
await logToFile('roPresence Info: Autoupdater - Update available.')
tray.setToolTip("roPresence - Update available.")
const notification = new Notification({
title: 'roPresence Update Available',
body: "A new roPresence update is available. Starting the download.",
timeoutType: 'never',
icon: path.join(__dirname, 'img/roPresence-logo.png')
})
notification.show()
})
autoUpdater.on(
'update-not-available',
async () => {
await logToFile('roPresence Info: Autoupdater - No update available.')
tray.setToolTip("roPresence " + APP_VERSION)
})
autoUpdater.on(
'download-progress',
(obj) => {
tray.setToolTip("roPresence - Downloading update: " + Math.round(obj.percent) + "%")
})
autoUpdater.on(
'update-downloaded',
(event, releaseNotes, releaseName) => {
const notification = new Notification({
title: 'roPresence Update Downloaded',
body: "Version " + releaseName + " was downloaded. Restart roPresence to install it.",
timeoutType: 'never',
icon: path.join(__dirname, 'img/roPresence-logo.png')
})
notification.on('click', (event) => {
autoUpdater.quitAndInstall()
})
notification.show()
tray.setToolTip("roPresence - Update downloaded.")
}
)
autoUpdater.setFeedURL(AUTO_UPDATE_URL)
autoUpdater.checkForUpdates()
setInterval(() => {
autoUpdater.checkForUpdates()
}, 10 * 60 * 1000)
}
async function init () {
if (process.platform === 'linux') {
await logToFile('roPresence Information: Autoupdater is not available on Linux.')
} else {
initUpdater()
}
await robloxVerify()
const busy = setInterval(async () => {
if (busyRetrying) {
clearInterval(busy)
} else {
await robloxVerify()
}
}, 15e3)
}
app.whenReady().then(async () => {
tray = new Tray('./resources/app/img/roPresence-logo.png')
const contextMenu = Menu.buildFromTemplate([{
label: 'Logging in.',
type: 'normal'
},
{
label: 'Item2',
type: 'separator'
},
{
label: 'Exit roPresence',
type: 'normal',
click: async function () {
await exitRoPresence()
tray.destroy()
}
},
{
label: 'View Logs',
type: 'normal',
click: function () {
Open('./roPresence_log.txt')
}
}
])
tray.setToolTip('roPresence - Loading...')
tray.setContextMenu(contextMenu)
/*const notification = new Notification({
title: 'roPresence',
body: 'Now loading ' + require('./package.json').version + ', this may take some seconds.',
timeoutType: 'never',
icon: path.join(__dirname, 'img/roPresence-logo.png')
})
notification.show()*/
await logToFile('RPC: Attempting to login through IPC.')
RPC.on('ready', async () => {
await logToFile('RPC: Logged in as ' + RPC.user.username + ' (' + RPC.user.id + ').')
await init()
})
RPC.login({
clientId
}).catch(async (str) => {
await logToFile(str)
await logToFile('Failed to connect to Discord!')
if (Config.attemptToOpenDiscordOnConnectionFailure === true && process.env.terminal !== '1') {
await logToFile('Attempting to forcefully open Discord...')
Open('Discord.exe', {
wait: 'true'
})
setInterval(async () => {
// Restart process and pass in a flag to give up after first attempt
await logToFile('Restarting process with terminal flag...')
process.exit(1) // Failure, ask master process to launch terminal process.
}, Config.discordOpenedTimeout * 1000)
} else {
const notification = new Notification({
title: 'roPresence Discord Error',
body: "Failed to connect to Discord! Make sure that Discord has been launched and that you're logged in, then launch roPresence again.",
timeoutType: 'never',
icon: path.join(__dirname, 'img/no.png')
})
notification.on('click', (event) => {
Open('Discord.exe')
})
notification.show()
await logToFile("Make sure that Discord has been launched and that you're logged in, then launch roPresence again.")
await logToFile('Exiting in 5 seconds...')
setInterval(() => {
process.exit()
}, 5000)
}
})
})