-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
215 lines (188 loc) · 5.63 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
require("dotenv").config();
const nbx = require("noblox.js");
const rpc = require("@xhayper/discord-rpc");
const Config = require("./config.js");
const Relative = require("@yaireo/relative-time");
let RelativeTime = new Relative();
const ROBLOSECURITY_PAD =
"_|WARNING:-DO-NOT-SHARE-THIS.--Sharing-this-will-allow-someone-to-log-in-as-you-and-to-steal-your-ROBUX-and-items.|_";
const PRESENCE_DETAILS = { NONE: 0, BASIC: 1, ALL: 2 };
/**
* Updates your Discord rich presence.
* @param {UserPresence} Presence
*
* Type definitions:
* ```js
* UserPresence {
* state: UserPresenceType;
* // 0 = Offline, 1 = Online, 2 = InGame, 3 = Studio
* userName: string;
* userId: string;
* location: string;
* lastOnline: string;
* gameState: GamePresence;
* thumbnails: PresenceThumbnails;
* }
* GamePresence {
* gameId: string;
* placeId: number;
* universeId: number;
* }
* PresenceThumbnails {
* userHeadshotThumbnail: string;
* userBodyThumbnail: string;
* gameThumbnail: string;
* }
* ```
*/
function updatePresence(Presence) {
let details = Presence.userName;
let state = "Offline";
// Presence detection
if (Presence.state === 1) state = "Online (" + Presence.location + ")";
if (Presence.state === 2) state = 'Playing "' + Presence.location + '"';
if (Presence.state === 3) state = "Editing in Studio";
if (Config.presenceDetails === 0) {
// Offline and Online only
if (Presence.state >= 0 && Presence.state <= 3) {
state = "Online";
}
} else if (Config.presenceDetails === 1) {
// Offline, Online and InGame only
if (Presence.state == 3) {
// Studio not allowed
state = "Online";
}
}
if (Presence.state < 2) {
state += " (last seen in " + Presence.location + ")";
}
// Thumbnail setting
let bigThumbnail = "dummy_game_image";
let bigThumbnailText = "Game title";
let smallThumbnail = "dummy_user_image";
let smallThumbnailText = "Username";
if (Presence.state === 2) {
if (Config.presenceDetails === 2) {
// In-game, show game and user in big and small thumbnails
// bigThumbnail = Presence.thumbnails.gameThumbnail;
bigThumbnail = Presence.thumbnails.userHeadshotThumbnail;
bigThumbnailText = Presence.userName + " is playing " + Presence.location;
smallThumbnail = "logo_new_small";
smallThumbnailText = "Roblox-RPC v1.0";
} else {
// Only show user thumbnail
bigThumbnail = Presence.thumbnails.userBodyThumbnail;
smallThumbnail = "logo_tilt";
bigThumbnailText = Presence.userName;
smallThumbnailText = "Roblox";
}
} else {
// Only show user thumbnail
bigThumbnail = Presence.thumbnails.userBodyThumbnail;
smallThumbnail = "logo_new_small";
bigThumbnailText = Presence.userName + ` (${state})`;
smallThumbnailText = "Roblox";
}
console.log({
details,
state,
bigThumbnail,
bigThumbnailText,
smallThumbnail,
smallThumbnailText,
Presence,
});
let buttons = [
{
label: "View Profile",
url: "https://www.roblox.com/users/" + Presence.userId + "/profile",
},
];
if (Presence.state === 2) {
buttons.push({
label: "View Game",
url:
"https://www.roblox.com/games/" + Presence.gameState.universeId + "/",
});
}
client.user?.setActivity({
details,
state,
buttons,
// {
// label: "Test Button 01",
// url: "https://thats-the.name/",
// },
// {
// label: "Test Button 02",
// url: "https://thats-the.name/",
// },
// ],
largeImageKey: bigThumbnail,
largeImageText: bigThumbnailText,
smallImageKey: smallThumbnail,
smallImageText: smallThumbnailText,
// partySize: 3,
// partyMax: 16,
});
}
const client = new rpc.Client({
clientId: "1055739306670563349",
});
function getRobloxPresence() {
nbx.setCookie(process.env.ROBLOX_COOKIE).then((d) => startRobloxLoop(d));
}
function handleError(e) {
if (e.code !== "ETIMEDOUT") { // Common API error
console.error("Fatal error", e);
process.exit(1);
}
}
async function startRobloxLoop(data) {
console.log(`Logged in as ${data.UserName} [${data.UserID}]`);
async function poll() {
let headThumbnails = await nbx
.getPlayerThumbnail([data.UserID], "420x420", "png", false, "headshot")
.catch(handleError);
let bodyThumbnails = await nbx
.getPlayerThumbnail([data.UserID], "420x420", "png", false, "body")
.catch(handleError);
let presences = await nbx.getPresences([data.UserID]).catch((e) => {
console.error("Fatal error", e);
process.exit(1);
});
const myPresence = presences.userPresences[0];
updatePresence({
state: myPresence.userPresenceType,
userName: data.UserName,
userId: data.UserID,
location: myPresence.lastLocation,
lastOnline: myPresence.lastOnline,
gameState: {
gameId: myPresence.gameId,
placeId: myPresence.placeId,
universeId: myPresence.placeId,
},
thumbnails: {
gameThumbnail: "",
userHeadshotThumbnail: headThumbnails[0].imageUrl,
userBodyThumbnail: bodyThumbnails[0].imageUrl,
},
});
}
await poll();
let loop = setInterval(poll, Config.pollingInterval);
}
client.on("ready", () => {
if (typeof process.env.ROBLOX_COOKIE !== "string") {
console.log("Invalid roblox cookie, see README.md for more details.");
return process.exit(1);
}
if (process.env.ROBLOX_COOKIE.startsWith(ROBLOSECURITY_PAD)) {
getRobloxPresence();
} else {
console.log("Invalid roblox cookie, see README.md for more details.");
}
});
client.login();