-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
136 lines (123 loc) · 3.18 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
const express = require('express')
const app = express();
var path = require('path');
const server = require('http').createServer(app);
const { Server } = require("socket.io");
const io = new Server(server, {
maxHttpBufferSize: 1e10 // 10 gb
})
const port = process.env.PORT || 8080;
app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*');
next();
});
app.use('/images', express.static('images'))
app.use('/', express.static('public'))
app.get('/tenor', async function(req, res) {
let data = await fetch('https://tenor.googleapis.com/v2/search?key='+process.env['tenor']+'&country=US&locale=US-en&limit=25&media_filter=gif&q='+req.query['q']);
data = await data.json();
res.json(data)
})
app.use(function(req, res) {
res.status(404)
res.sendFile(path.join(__dirname, 'error.html'))
})
io.of("/").adapter.on("join-room", (room, id) => {
io.to(room).emit('data', {
type: 'message',
auth: 'server',
data: {
content: `${id} joined ${room}`,
name: 'Server',
color: '888',
files: [],
time: new Date().getTime()
}
});
});
io.of("/").adapter.on("leave-room", (room, id) => {
io.to(room).emit('data', {
type: 'message',
auth: 'server',
data: {
content: `${id} left ${room}`,
name: 'Server',
color: '888',
files: [],
time: new Date().getTime()
}
});
});
io.on('connection', (socket) => {
socket.leave(socket.id)
socket.join('main')
io.emit('data', {
type: 'stats',
auth: 'server',
data: {
count: io.engine.clientsCount
}
})
function rome() {
return Array.from(io.sockets.adapter.sids.get(socket.id))[0]
}
socket.on('data', async(data) => {
switch (data.type) {
case 'message':
if (!data.data.content.replaceAll(/ |\\n/g, "") && !data.data.files.length) return;
io.to(rome()).emit('data', {
type: 'message',
auth: 'user',
data: {
content: data.data.content,
name: data.data.name || 'Anonymous',
color: data.data.color,
id: socket.id,
files: data.data.files || [],
time: new Date().getTime()
}
})
if (data.data.content.toLowerCase().includes('fsh')) {
io.to(rome()).emit('data', {
type: 'message',
auth: 'bot',
data: {
content: 'fsh',
name: 'Fsh',
color: '888',
files: [],
time: new Date().getTime()
}
})
}
break;
case 'room':
io.sockets.adapter.sids.get(socket.id).forEach(k => socket.leave(k))
socket.join(data.data.room)
break;
}
});
socket.on('disconnect', () => {
io.emit('data', {
type: 'stats',
auth: 'server',
data: {
count: io.engine.clientsCount
}
})
io.emit('data', {
type: 'message',
auth: 'server',
data: {
content: `${socket.id} left`,
name: 'Server',
color: '888',
files: [],
time: new Date().getTime()
}
});
});
});
server.listen(port, function() {
console.log(`Listening on port ${port}`);
});