forked from d-zone-org/d-zone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
47 lines (42 loc) · 1.85 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
'use strict';
// Server script entry point
var path = require('path');
require('dotenv').config({ path: path.join(__dirname, '.env') });
var discordConfig = require('./discord-config');
var socketConfig = require('./socket-config');
var Inbox = require('./script/inbox.js');
console.log('Initializing server');
var WebSock = require('./script/websock.js');
var inbox = new Inbox(discordConfig);
var webSock;
inbox.on('connected', () => {
webSock = new WebSock(socketConfig,
function onConnect(socket) {
socket.send(JSON.stringify({ type: 'server-list', data: inbox.getServers() }));
},
function onJoinServer(socket, connectRequest) {
var users = inbox.getUsers(connectRequest);
if(users === 'unknown-server') {
socket.send(JSON.stringify({
type: 'error', data: { message: 'Sorry, couldn\'t connect to that Discord server.' }
}));
} else if(users === 'bad-password') {
socket.send(JSON.stringify({
type: 'error', data: { message: 'Sorry, wrong password for that Discord server.' }
}));
console.log('Client used wrong password to join server', connectRequest.server, connectRequest.password);
} else {
socket.discordServer = users.server.discordID;
// Send list of current online users to set up initial client state
console.log('Client joined server', users.server.name);
socket.send(JSON.stringify({
type: 'server-join', data: {
users: users.userList, request: connectRequest
}
}));
}
}
);
inbox.on('message', webSock.sendData.bind(webSock));
inbox.on('presence', webSock.sendData.bind(webSock));
});