-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
122 lines (109 loc) · 3.54 KB
/
main.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
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
const MODULE = '[SillyTavern-DiscordRichPresence-Server]';
const states = [
'Constructing a waifu army',
'Spreading the silliness',
'Mass producing catgirls',
'Roleplaying with a toaster',
'Caffeinated and chaotic',
'Existential AI therapy',
'Seraphina my beloved',
// Add more states here
];
let client;
async function exit() {
if (client) {
try {
const chalk = require('chalk');
console.log(chalk.green(MODULE), 'Shutting down Discord connection');
await Promise.race([
client.destroy(),
new Promise(resolve => setTimeout(resolve, 1000)),
]);
} catch {
// Ignore
}
}
}
/**
* Initializes the plugin
* @param {import('express').Router} router Express router
*/
async function init(router) {
const rpc = require('discord-rpc');
const fs = require('fs');
const path = require('path');
const chalk = require('chalk');
const config = JSON.parse(fs.readFileSync(path.join(__dirname, 'config.json'), 'utf8'));
let connected = false;
console.log(chalk.green(MODULE), 'Plugin loaded!');
await connectToDiscord();
router && router.post('/update', (req, res) => {
const name = req.query.name;
if (name) {
console.log(chalk.green(MODULE), 'Updating Discord Rich Presence to', chalk.blue(name));
const state = `Chatting with ${name}`;
setActivity(state);
res.status(200).send('OK');
} else {
console.log(chalk.green(MODULE), 'Resetting Discord Rich Presence');
setActivity();
res.status(204).send();
}
});
async function connectToDiscord() {
client = new rpc.Client({ transport: 'ipc' });
client.once('ready', () => {
console.log(chalk.green(MODULE), 'Discord Rich Presence is ready!');
setActivity();
});
client.on('connected', () => {
connected = true;
});
client.on('disconnected', () => {
console.log(chalk.yellow(MODULE), 'Client disconnected!');
connected = false;
});
await client.login({ clientId: config.ClientID }).catch(console.error);
}
async function setActivity(state) {
try {
// Try to connect if not connected.
if (!connected) {
await connectToDiscord();
}
// Still nothing? Avast!
if (!connected) {
console.error(chalk.yellow(MODULE), 'Not connected to a client');
return;
}
state = state || states[Math.floor(Math.random() * states.length)];
await client.setActivity({
details: config.Details,
state: state,
buttons: [
{
label: config.Button1,
url: config.Url1,
},
{
label: config.Button2,
url: config.Url2,
},
],
largeImageKey: config.LargeImage,
largeImageText: config.LargeImageText,
}, process.pid);
} catch {
console.error(chalk.yellow(MODULE), 'Failed to set activity');
}
}
}
module.exports = {
init,
exit,
info: {
id: 'discord',
name: 'Discord Rich Presence',
description: 'Set your rich presence in Discord to the "SillyTavern" app.',
},
};