This repository has been archived by the owner on Oct 25, 2023. It is now read-only.
forked from foxt/rblxRP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
267 lines (243 loc) · 7.89 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
const fetch = require("node-fetch");
const {app, BrowserWindow,Notification,Menu, Tray} = require('electron')
const RPC = require("discord-rpc");
const util = require("./util")
const os = require("os")
const fs = require("fs")
const clientId = '626092891667824688';
var defaultIconKey = "logo_shiny" // logo_old, logo_red, logo_shiny
const rpc = new RPC.Client({ transport: 'ipc' });
var tray,contextMenu
var cache = {}
async function exit() {
try { await rpc.clearActivity() } catch(e) {}
process.kill(process.pid)
}
async function menu(item) {
item.checked = true
defaultIconKey = item.id
for (var i in cache) {
if (cache[i].iconkey == "logo_shiny" ||cache[i].iconkey == "logo_red" ||cache[i].iconkey == "logo_old") {
cache[i].iconkey = defaultIconKey
}
}
lastPresense = false // force presense update
}
function getVersion() {
if (fs.existsSync("./package.json")) {
try {
var file = JSON.parse(fs.readFileSync("./package.json").toString())
if (file.version) {
return file.version + " "
} else {
return ""
}
} catch(e) {return""}
} else {
return ""
}
}
async function procDetect() {
console.log("[Detect:Proc] Detecting Roblox with process method")
var processes = await util.getProcesses({
command: "RobloxPlayer"
})
console.log("[Detect:Proc] Detected",processes.length,"Roblox instances running")
if (processes.length < 1) {
return false
}
var scriptUrl = util.getScriptUrl(processes[0].arguments)
if (scriptUrl == false) {
console.log("[Detect:Proc] Couldn't find the script URL")
} else {
console.log("[Detect:Proc] Script URL",scriptUrl)
}
if (!scriptUrl.includes("placeId=")) {
console.log("[Detect:Proc] Malformed script URL")
return false
}
var split = parseInt(scriptUrl.split("placeId=")[1])
if (isNaN(split)) {
console.log("[Detect:Proc] Malformed script URL")
return false
}
console.log("[Detect:Proc] Found game ID",split)
return split
}
async function detectAPI() {
console.log("[Detect:API] Seeing if user",ruserid,"is playing Roblox")
try {
var ftch = await fetch(`https://api.roblox.com/users/${ruserid}/onlinestatus`)
var j = await ftch.json()
} catch(e) {
console.log("[Detect:API] Failed,",e)
}
return false
}
async function detectGame() {
console.log("[Detect] Trying to find Roblox...")
var detection = await procDetect()
return detection
if (detection == false) {
console.log("[Detect:Proc] Didn't find a running instance.")
if (apiDetect) {
detection = await detectAPI()
if (detection == false) {
console.log("[Detect:API] Didn't find user", ruserid, "playing a game? Do you have privacy settings to 'Everyone can join my game?'")
return false
}
} else {
return false
}
} else {
return false
}
}
async function getGameFromCache(gameid) {
if (cache[gameid]) {return cache[gameid]}
try {
var apiResponse = await fetch("https://api.roblox.com/marketplace/productinfo?assetId=" + gameid)
if (!apiResponse.ok) {throw new Error(apiResponse)}
var j = await apiResponse.json()
var obj = {
name: j.Name,
by: j.Creator.Name,
iconkey: defaultIconKey
}
cache[gameid] = obj
return obj
} catch(e) {
console.error(e)
return {
name:"(unknown)",
by: "(unknown)",
iconkey: defaultIconKey
}
}
}
var timeout = 15000
var lastPresense = false
var enabled = true
async function doTheThing() {
console.log("Updating presense")
var playing = await detectGame()
if (playing == false || !enabled) {
if (lastPresense != false) {
rpc.clearActivity()
} else {
timeout = 1000
}
tray.setTitle("")
if (enabled) {
console.log("Not playing anything. Open Roblox and try it out!")
} else {
console.log("rblxRP disabled.")
}
lastPresense = false
return
}
var game = await getGameFromCache(playing)
console.log("Playing",game.name, "by",game.by)
tray.setTitle(game.name, "by",game.by)
if (lastPresense != game) {
rpc.setActivity({
details:game.name,
state: `by ${game.by}`,
startTimestamp: 0,
largeImageKey: game.iconkey,
largeImageText: 'remind me to put something here',
smallImageKey: 'rblxrp',
smallImageText: 'https://github.com/thelmgn/rblxrp',
instance: false,
});
} else { timeout = 1000 }
lastPresense = game
}
async function interval() {
timeout = 15000
try {
await doTheThing()
} catch(e) {
console.error(e)
}
setTimeout(interval,timeout)
}
rpc.on("ready",async function() {
new Notification({
title: "rblxRP is ready!",
body: "Hi there, " + rpc.user.username + "!",
silent:true
}).show()
console.log("Connected to Discord")
contextMenu = Menu.buildFromTemplate([
{ label: "Enable", type: 'checkbox',checked:enabled,click: function() {
console.log("Toggling enabled")
enabled = !enabled
contextMenu.items[0].checked = enabled
} },
{
label: "Default game icon",
type: "submenu",
submenu: [
{ label: "Shiny", id: "logo_shiny", type: "radio", checked:true,click:menu},
{ label: "Red", id:"logo_red", type: "radio",click:menu},
{ label: "Old 'R' Logo", id:"logo_old", type: "radio",click:menu},
]
},
{ label: 'Quit',click: exit},
{type:"separator"},
{ label: 'rblxRP ' + getVersion() + "by theLMGN",enabled:false},
{ label: 'GitHub',click: function() {
}},
])
tray.setContextMenu(contextMenu)
tray.setToolTip('rblxRP')
tray.setTitle("")
interval()
})
async function go() {
try {
try {
app.dock.hide()
} catch(e) {}
var logo ="ico/logo_white.png"
if (os.platform() == "win32") { logo = "ico/logo.ico" }
try {
tray = new Tray(logo)
} catch(e) {
tray = new Tray(app.getAppPath() + '/' + logo)
}
contextMenu = Menu.buildFromTemplate([
{ label: 'Quit', click: exit }
])
tray.setToolTip('rblxRP')
tray.setContextMenu(contextMenu)
console.log("Downloading configuration...")
tray.setTitle("Downloading config...")
var req = await fetch("https://gist.githubusercontent.com/theLMGN/3799b5cb7b0328be7a13860e46832b0e/raw/9f73d30f4e6369ef976234eb99ed8207363d24ea/rblxrp_config.json")
if (!req.ok) {throw new Error("not ok!")}
var j = await req.json()
cache = j.games
console.log("Downloaded configuration!")
console.log("Connecting to Discord...")
tray.setTitle("Connecting to Discord...")
rpc.login({clientId}).catch(function(e) {
console.error("Failed to connect to Discord... ",e, "Will try again in 15 seconds")
new Notification({
title: "rblxRP failed to connect to Discord",
body: "Will try again in 15 seconds",
silent:true
}).show()
setTimeout(go,15000)
})
} catch(e) {
console.error(e, ". Trying again in 15 seconds")
new Notification({
title: "rblxRP failed to load",
body: "Will try again in 15 seconds",
silent:true
}).show()
setTimeout(go,15000)
}
}
app.on('ready', go)