-
Notifications
You must be signed in to change notification settings - Fork 23
/
index.js
267 lines (236 loc) · 6.32 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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
const RPC = require("discord-rpc");
const discordDb = require("./discorddb");
const TP = require("touchportal-api");
const express = require("express");
const bodyParser = require("body-parser");
const { open } = require("out-url");
const path = require("path");
let clientId = undefined;
let clientSecret = undefined;
let accessToken = undefined;
let connecting = false;
const scopes = ["identify", "rpc"];
const redirectUri = "http://localhost";
const pluginId = "TPDiscord";
const TPClient = new TP.Client();
let DiscordClient = null;
const app = express();
app.use(bodyParser.urlencoded({ extended: true }));
const port = 9403;
// - START - TP
let muteState = 0;
let deafState = 0;
TPClient.on("Action", (data) => {
if (data.data && data.data.length > 0) {
if (data.data[0].id === "discordDeafenAction") {
if (data.data[0].value === "Toggle") {
deafState = 1 - deafState;
} else if (data.data[0].value === "Off") {
deafState = 0;
} else if (data.data[0].value === "On") {
deafState = 1;
}
DiscordClient.setVoiceSettings({ deaf: 1 === deafState });
}
if (data.data[0].id === "discordMuteAction") {
if (data.data[0].value === "Toggle") {
muteState = 1 - muteState;
} else if (data.data[0].value === "Off") {
muteState = 0;
} else if (data.data[0].value === "On") {
muteState = 1;
}
DiscordClient.setVoiceSettings({ mute: 1 === muteState });
}
} else {
console.log(
pluginId,
": WARN : No data in Action Message",
JSON.stringify(data)
);
}
});
TPClient.on("ListChange", (data) => {
console.log(pluginId, ": DEBUG : ListChange :" + JSON.stringify(data));
});
TPClient.on("Info", (data) => {
console.log(pluginId, ": DEBUG : Info :" + JSON.stringify(data));
});
TPClient.on("Close", (data) => {
console.log(
pluginId,
": WARN : Closing due to TouchPortal sending closePlugin message"
);
});
// - END - TP
// - START - Discord
const connectToDiscord = function () {
DiscordClient = new RPC.Client({ transport: "ipc" });
const voiceActivity = function (data) {
if (data.mute) {
muteState = 1;
} else {
muteState = 0;
}
if (data.deaf) {
deafState = 1;
muteState = 1;
} else {
deafState = 0;
}
let states = [
{ id: "discord_mute", value: muteState ? "On" : "Off" },
{ id: "discord_deafen", value: deafState ? "On" : "Off" },
];
TPClient.stateUpdateMany(states);
};
const subscribe = function (data) {
if (!data || !data.mode || !data.mode.type) {
console.log(
pluginId,
": ERROR : subscribe : event has no data or known mode.type"
);
return;
}
switch (data.mode.type) {
case "VOICE_ACTIVITY":
voiceActivity(data);
break;
default:
console.log(
pluginId,
": DEBUG : Unhandled mode.type " + data.mode.type
);
}
};
DiscordClient.on("ready", () => {
if (!accessToken) {
discordDb.db.insert({
_id: "discordToken",
accessToken: DiscordClient.accessToken,
});
}
DiscordClient.subscribe("VOICE_SETTINGS_UPDATE", subscribe);
});
DiscordClient.on("disconnected", () => {
console.log(pluginId, ": WARN : discord connection closed, will attempt reconnect");
doLogin();
});
DiscordClient.login({
clientId,
clientSecret,
accessToken,
scopes,
redirectUri,
}).catch((error) => {});
};
// - END - Discord
// - START - WebServer
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname,"/html/discordkeys.html"));
});
app.post("/store", async (req, res) => {
const data = req.body;
if (data.clientId && data.clientSecret) {
clientId = data.clientId;
clientSecret = data.clientSecret;
} else {
res.sendFile(path.join(__dirname,"/html/error.html"));
return;
}
let appId = await discordDb.db.findOne({ _id: "appid" });
let id = "";
if (appId) {
id = appId._id;
}
if (id === undefined || id === "") {
discordDb.db.insert({
_id: "appid",
clientId: data.clientId,
clientSecret: data.clientSecret,
});
} else {
discordDb.db.update(
{ _id: id },
{
_id: "appid",
clientId: data.clientId,
clientSecret: data.clientSecret,
},
{},
function (err, numReplaced) {}
);
}
res.sendFile(path.join(__dirname,"/html/success.html"));
});
app.use(express.static(path.join(__dirname,"/public")));
app.listen(port, () =>
console.log(
pluginId,
`: DEBUG : Example app listening at http://localhost:${port}`
)
);
// - END - WebServer
var waitForClientId = (timeoutms) =>
new Promise((r, j) => {
var check = () => {
if (clientId !== undefined) {
r();
} else if ((timeoutms -= 100) < 0) j("timed out, restart Touch Portal!");
else setTimeout(check, 100);
};
setTimeout(check, 100);
});
var waitForLogin = () =>
new Promise((r, j) => {
connecting = true;
var check = () => {
if (DiscordClient && DiscordClient.user != null) {
connecting = false;
r();
} else {
connectToDiscord();
if (DiscordClient && DiscordClient.user != null) {
connectin = false;
r();
} else {
setTimeout(check, 1000);
}
}
};
setTimeout(check,500);
});
async function doLogin() {
if( connecting ) { return; }
if( DiscordClient ) {
DiscordClient.removeAllListeners();
DiscordClient.destroy();
DiscordClient = null;
}
let appDoc = await discordDb.db.findOne({ _id: "appid" });
if (!appDoc) {
open(`https://discord.com/developers/applications`);
open(`http://localhost:${port}`);
}
if (
appDoc &&
appDoc.clientId !== undefined &&
appDoc.clientSecret !== undefined
) {
clientId = appDoc.clientId;
clientSecret = appDoc.clientSecret;
}
if (clientId === undefined) {
await waitForClientId(30 * 60 * 1000); // wait for 30 minutes
}
let aTDoc = await discordDb.db.findOne({ _id: "discordToken" });
if (aTDoc && aTDoc.accessToken !== undefined) {
accessToken = aTDoc.accessToken;
}
// Start Login process
await waitForLogin();
}
// We are going to connect to TP first, then Discord
// That way if TP shuts down the plugin will be shutdown too
TPClient.connect({ pluginId });
doLogin();