-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrpc.js
More file actions
150 lines (140 loc) · 4.93 KB
/
rpc.js
File metadata and controls
150 lines (140 loc) · 4.93 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
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
const RPC = process.env.MOCK_RPC ? require(process.env.MOCK_RPC) : require('discord-rpc');
const config = require('./Settings/config.json');
const logger = require('./Functions/logger');
const errors = require('./Functions/errors');
const rpc = new RPC.Client({
transport: 'ipc'
});
const cfonts = require('cfonts');
const activities = [{
details: 'Developing a Discord RPC',
state: 'In the zone',
startTimestamp: new Date(),
largeImageKey: 'large-image', // Add your large image key
largeImageText: 'Large Image Text',
smallImageKey: 'small-image', // Add your small image key
smallImageText: 'Small Image Text',
instance: false,
buttons: [{
label: 'Join us',
url: 'https://discord.com/invite/your-invite-link'
},
{
label: 'Learn More',
url: 'https://example.com'
}
]
},
{
details: 'Reviewing Code',
state: 'Deep Focus',
startTimestamp: new Date(),
largeImageKey: 'review-image', // Add your large image key
largeImageText: 'Review Image Text',
smallImageKey: 'focus-image', // Add your small image key
smallImageText: 'Focus Image Text',
instance: false,
buttons: [{
label: 'Documentation',
url: 'https://example-docs.com'
},
{
label: 'Support',
url: 'https://support.example.com'
}
]
}
];
function setActivities(client = rpc) {
let index = 0;
let retryCount = 0;
const maxRetries = 3;
const updateActivity = () => {
if (index < activities.length) {
client.setActivity(activities[index])
.then(() => {
if (activities.length > 1) {
logger.log('PRESENCES', `Updated to activity ${index + 1}.`);
} else {
logger.log('PRESENCE', `Updated to the only activity.`);
}
index++;
retryCount = 0;
setTimeout(updateActivity, config.status_timeout);
})
.catch((error) => {
logger.error('RPC Error', `Failed to set activity ${index + 1}.`);
errors(client, error.stack);
if (retryCount < maxRetries) {
retryCount++;
logger.error('RPC Error', `Retrying to set activity ${index + 1} (${retryCount}/${maxRetries}).`);
setTimeout(updateActivity, 5000);
} else {
logger.error('RPC Error', `Max retries reached for activity ${index + 1}. Skipping to the next activity.`);
index++;
retryCount = 0;
setTimeout(updateActivity, config.status_timeout);
}
});
} else {
index = 0;
setTimeout(updateActivity, config.status_timeout);
}
};
updateActivity();
}
function loginRpc(retryCount = 0) {
const maxRetries = 3;
rpc.login({
clientId: config.client_id
})
.catch((error) => {
logger.error('RPC Error', 'Failed to login to RPC.');
errors(rpc, error.stack);
if (retryCount < maxRetries) {
logger.error('RPC Error', `Retrying login (${retryCount + 1}/${maxRetries}).`);
setTimeout(() => loginRpc(retryCount + 1), 5000);
} else {
logger.error('RPC Error', 'Max retries reached for login. Exiting.');
process.exit(1);
}
});
}
rpc.on('ready', () => {
const banner = cfonts.render((`RPC Client\nby ThunderDoesDev`), {
font: 'chrome',
color: 'candy',
align: 'center',
gradient: ["red", "magenta"],
lineHeight: 1
});
console.log(banner.string);
logger.log('WHO AM I', `Logged In As ${rpc.user.username}`);
logger.log("CONNECTED", "Connected To Discord's RPC Gateway");
setActivities();
});
rpc.on('disconnected', () => {
logger.error('RPC Client has been disconnected. Attempting to reconnect...');
setTimeout(loginRpc, config.status_timeout);
});
process.on('SIGINT', () => {
logger.error('RPC Error', `Shutting down gracefully.`);
rpc.destroy()
.then(() => {
logger.error('RPC Error', `RPC client destroyed.`);
process.exit(0);
})
.catch((error) => {
errors(rpc, error.stack);
logger.error('RPC Error', `Failed to destroy RPC client.`);
process.exit(1);
});
});
if (require.main === module) {
loginRpc();
}
module.exports = {
setActivities,
loginRpc,
rpc,
};