-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrpc.js
More file actions
76 lines (63 loc) · 1.97 KB
/
rpc.js
File metadata and controls
76 lines (63 loc) · 1.97 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
const RPC = require('discord-rpc');
const clientId = '1331184169753120778';
// registir client id
RPC.register(clientId);
// create da client
const rpc = new RPC.Client({ transport: 'ipc' });
// track role (innocent by default)
let currentRole = 'Innocent';
let dotCount = 0; // dot tracker (definitely a better way to do this im just dumb)
// set up Rich Presence
function setRichPresence(role = 'Innocent', dots = '') {
rpc.setActivity({
details: `mm bot running${dots}`,
state: `Role: ${role}`,
startTimestamp: Date.now(),
instance: false,
}).catch(err => {
console.warn('[Discord RPC] Failed to set activity:', err.message);
});
}
// function to update the role
function updateRole(newRole) {
if (currentRole !== newRole) {
currentRole = newRole;
setRichPresence(currentRole, '.'.repeat(dotCount)); // updates presence with current dots
}
}
// function to cycle dots
function cycleDots() {
setInterval(() => {
dotCount = (dotCount + 1) % 4; // cycles between 0 and 3 dots
setRichPresence(currentRole, '.'.repeat(dotCount));
}, 500); // update every half second
}
// event listeners for the RPC client
rpc.on('ready', () => {
console.log('[Discord RPC] Connected!');
setRichPresence(currentRole); // set initial presence
cycleDots(); // begin dot animation
});
rpc.on('disconnected', () => {
console.log('[Discord RPC] Disconnected. Attempting to reconnect...');
reconnectRPC();
});
rpc.on('error', (err) => {
console.error('[Discord RPC] Error occurred:', err);
});
// function to handle reconnection
function reconnectRPC() {
setTimeout(() => {
rpc.login({ clientId }).catch(console.error);
}, 5000); // attempt to reconnect every 5 secs
}
// start rpc
rpc.login({ clientId }).catch((err) => {
console.error('[Discord RPC] Failed to connect:', err);
reconnectRPC();
});
// export role updater for other files
module.exports = {
rpc,
updateRole,
};