-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
104 lines (88 loc) · 3.04 KB
/
server.js
File metadata and controls
104 lines (88 loc) · 3.04 KB
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
const express = require("express");
const bodyParser = require("body-parser");
const botMain = require("./bot.js");
const userHandler = require("./commands/register.js");
const app = express();
const fs = require("fs");
const serverhttp = require('http').createServer(app).listen(process.env.PORT, () => {
console.log(`server is listening on port ${process.env.PORT}`);
});
const io = require('socket.io')(serverhttp);
const { v4: uuidv4 } = require('uuid');
module.exports.usedChns = {};
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.set('view engine', 'pug')
app.get("/", (request, response) => {
response.render('sendToDiscord');
console.log("pinged :)");
response.end("OK");
});
io.on('connection', (socket) => {
socket.emit('id', uuidv4());
socket.on('loadMsgs', (data) => {
module.exports.updateMsgs(data.server, data.channel, data.mode);
//console.log([data.channel, botMain.getMembers(data.server, data.channel)]);
});
socket.on('loadMor', (data) => {
module.exports.updateMsgs(data.server, data.channel, data.mode, data.before);
//console.log(data.before);
});
socket.on('updateArr', (data) => {
const channel = data.channel;
const server = data.server;
const id = data.ID;
delete module.exports.usedChns[id];
if (channel !== "") module.exports.usedChns[id] = `${server},${channel}`;
});
socket.on('sendMessage', async (data) => {
const content = data.content;
const server = data.server;
const channel = data.channel;
const userID = data.userID;
if (content !== '') {
const response = await botMain.sendMsg(server, channel, content, userID);
if (response === "") {socket.emit("senderr", undefined);}
}
});
socket.on("verifyUser", (data) => {
const username = data[1];
const server = data[0];
const password = data[2];
const id = botMain.getID(username, server);
//console.log(data);
if (userHandler.getUser(id, password)) socket.emit("verifySuccess", true);
else socket.emit("verifyFail", undefined);
});
});
module.exports.updateMsgs = async (server, channel, mode, before) => {
const data = {
mode: mode,
channel: channel,
messages: await botMain.getMsgs(server, channel, mode, before)
}
io.emit('messagePackage', data);
}
app.post('/connect', async (req, res) => {
try {
const server = req.body.serv;
const user = req.body.user;
const pass = req.body.pass;
const channels = botMain.getChns(server).filter(c => botMain.canSee(server, c, user));
const id = botMain.getID(user, server);
if (userHandler.getUser(id, pass)) {
res.render('sconnected', {
welcome: `Welcome, ${user}!`,
ser: server,
chns: channels,
id: id,
});
}
else {
res.send("Invalid credentials. Please check if you are in the server that you are attempting to join.");
}
}
catch(e) {
console.log(e.stack);
}
});