-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
78 lines (70 loc) · 2.15 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
const express = require('express');
const RPC = require("discord-rpc");
const open = require("open");
console.clear()
const client = new RPC.Client({
transport: 'ipc'
});
var connected = false;
function connect_discord() {
client.login({
clientId: '785150339308716082'
}).catch(() => {
console.log('Error when trying to connnect to Discord Client Application.\nRetrying...')
});
}
connect_discord();
client.on('disconnected', () => {
connected = false;
console.log('Client disconnected!');
});
var last_request = null;
client.on('ready', () => {
connected = true;
console.log('Authed for user', client.user.username);
client.subscribe('ACTIVITY_JOIN', (data) => {
var data = Buffer.from(data.secret, 'base64');
data = JSON.parse(data.toString('ascii'));
if (last_request == null || new Date() - last_request > 3000) {
last_request = new Date();
open(data.url);
}
});
});
setInterval(() => {
if (!connected)
connect_discord();
}, 10000);
const app = express();
app.use(express.json());
app.get("/iot-presence", (request, response) => {
if (!connected) {
response.sendStatus(500);
return;
}
var body = request.query;
var action = body.action;
switch (action) {
case "update":
var presence = body.presence;
if (presence.startTimestamp)
presence.startTimestamp = parseInt(presence.startTimestamp);
if (presence.partySize)
presence.partySize = parseInt(presence.partySize);
if (presence.partyMax)
presence.partyMax = parseInt(presence.partyMax);
if (presence.endTimestamp)
presence.endTimestamp = parseInt(presence.endTimestamp);
presence.instance = true;
client.setActivity(presence);
break;
case "clear":
client.clearActivity();
break;
}
response.send(`${body.callback}("discord-rpc: success");`);
response.end();
});
app.listen(3000, () => {
console.log('IOT - Discord Rich Presence is listening on port 3000!')
});