-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
71 lines (60 loc) · 1.78 KB
/
app.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
import express from 'express';
import Rpc from 'discord-rpc';
import logBoxedObject from './utils/logBox.js';
const app = express();
const PORT = 3000;
app.use(express.json());
const rpc = new Rpc.Client({ transport: 'ipc' });
let prevData = {};
const activity = (data) => {
let largeImageKey = data.largeIcon;
if (largeImageKey.endsWith('.ico') || largeImageKey === '') {
largeImageKey = '1brave';
}
const presenceData = {
state: data.state,
details: data.details,
startTimestamp: new Date().getTime(),
largeImageKey: data.largeIconContent
? data.largeIconContent.startsWith('http')
? data.largeIconContent
: 'https://' + data.details + data.largeIconContent
: largeImageKey,
largeImageText: 'Brave Browser',
buttons: [{ label: 'Visit the site', url: data.url }],
instance: true,
};
logBoxedObject(presenceData);
return presenceData;
};
const stateChange = (obj) => {
if (Object.keys(obj).length !== Object.keys(prevData).length) return true;
for (let key in prevData) {
if (!obj.hasOwnProperty(key) || prevData[key] !== obj[key]) return true;
}
return false;
};
rpc.on('ready', () => {
app.post('/', (req, res) => {
const data = req.body;
if (data.action === 'set' && data.rpcEnable) {
if (stateChange(data)) {
prevData = data;
rpc.setActivity(activity(data));
}
} else if (data.action === 'clear') {
rpc.clearActivity();
throw new Error('not a valid link');
} else {
rpc.clearActivity();
}
res.sendStatus(200);
});
});
app.listen(PORT, () => console.log(`Brave RPC is running on PORT: ${PORT}`));
try {
rpc.login({ clientId: '612158030473068545' });
} catch (error) {
console.error(error);
console.log('your discord client is not running');
}