forked from theADAMJR/discord-chat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
websocket.js
45 lines (35 loc) · 1.21 KB
/
websocket.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
const { messages, server } = require('./server');
const socket = require('socket.io');
const got = require('got');
const { toHTML } = require('discord-markdown');
const { textEmoji } = require('markdown-to-text-emoji');
const metascraper = require('metascraper')([
require('metascraper-description')(),
require('metascraper-image')(),
require('metascraper-title')(),
require('metascraper-url')()
]);
const io = socket(server);
io.on('connection', (clientSocket) => {
console.log('Made socket connection', clientSocket.id);
clientSocket.on('chat', async (data) => {
await setEmbed(data);
data.html = toHTML(textEmoji(data.message));
const newIndex = messages.push(data) - 1;
setTimeout(() => messages.splice(newIndex, 1), 5 * 60 * 1000);
io.sockets.emit('chat', data);
});
clientSocket.on('typing', (data) => {
clientSocket.broadcast.emit('typing', data);
});
});
async function setEmbed(data) {
const containsURL = /([https://].*)/.test(data.message);
if (containsURL) {
try {
const targetUrl = /([https://].*)/.exec(data.message)[0];
const { body: html, url } = await got(targetUrl);
data.embed = await metascraper({ html, url });
} catch {}
}
}