-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactivityHandler.js
More file actions
62 lines (49 loc) · 1.53 KB
/
activityHandler.js
File metadata and controls
62 lines (49 loc) · 1.53 KB
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
const net = require('net');
const struct = require('struct');
class RPCSocketConnection {
constructor(clientId) {
this.clientId = clientId;
this.connection = net.createConnection({ path: '/run/user/1000/discord-ipc-0' });
this.isConnected = false
}
connect() {
const authorizePayload = {
cmd: "AUTHORIZE",
args: {
client_id: this.clientId,
scopes: ["rpc", "identify"]
},
nonce: (new Date().getTime() / 1000).toFixed(20)
};
// this.connection.on('connect', () => {
this.sendMessage({ v: 1, client_id: this.clientId }, 0);
this.sendMessage(authorizePayload, 1);
// });
this.connection.setKeepAlive(true, 60000)
this.isConnected = true
}
close() {
this.connection.destroy()
}
sendMessage(payload, op) {
const message = JSON.stringify(payload);
const dataLen = Buffer.byteLength(message);
const packet = Buffer.alloc(8 + dataLen);
packet.writeInt32LE(op, 0);
packet.writeInt32LE(dataLen, 4);
packet.write(message, 8, dataLen);
this.connection.write(packet);
}
setActivity(data) {
const payload = {
cmd: "SET_ACTIVITY",
args: {
pid: process.pid,
activity: data
},
nonce: (new Date().getTime() / 1000).toFixed(20)
};
this.sendMessage(payload, 1);
}
}
module.exports = RPCSocketConnection;