-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
147 lines (112 loc) · 3.87 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
const express = require("express");
const parser = require("body-parser");
const cors = require("cors");
const multer = require("multer");
const cookieParser = require("cookie-parser");
const moment = require("moment");
const fs = require("fs");
const fetch = require("node-fetch");
const config = require("./config.json");
const app = express();
const port = 3000;
const storage = multer.memoryStorage();
const upload = multer({ storage: storage });
app.set("views", "views");
app.set("view engine", "ejs");
app.set("x-powered-by", false);
app.set("case sensitive routing", false);
app.use(parser.json());
app.use(parser.urlencoded( { extended: true } ));
app.use(cors());
app.use(cookieParser());
app.use(express.static(`${__dirname}`));
async function messageHandling(){
const messages = await require("./bot.cjs").fetchMessages();
let content = [];
let author_ids = [];
let timestamps = [];
let parsedContent = ``;
for (let message of messages){
message = message[1];
if(message.content) content.push(message.content);
author_ids.push(message.author.id);
timestamps.push(message.createdTimestamp);
for(let attachment of message.attachments){
const response = await fetch(attachment[1].url);
const arrayBuffer = await response.arrayBuffer();
const buffer = Buffer.from(arrayBuffer);
fs.createWriteStream(`./images/${attachment[1].name}`).write(buffer);
if(!message.content) content.push(`<embed type="${attachment[1].contentType}" src="../images/${attachment[1].name}">`);
if(message.content) content[content.length - 1] += `<br><br><embed type="${attachment[1].contentType}" src="../images/${attachment[1].name}">`;
}
};
content.reverse();
author_ids.reverse();
timestamps.reverse();
let i = 0;
let class_name_suff;
for (let c of content) {
if(author_ids[i] == config.client){
class_name_suff = "client";
} else {
class_name_suff = "other"
}
let date = new Date(timestamps[i]);
let time = moment(date, 'MM-DD-YYYY hh:mm:ss a');
time.utcOffset(-7);
parsedContent += `<div class="message ${class_name_suff}"><div class="content">${content[i]}</div><div class="tooltip">${time}</div></div><br>`;
i++;
};
return parsedContent;
}
app.get("/", async function(req, res){
if(!req.cookies.loggedIn) return res.redirect("/login");
res.render("index", {
content: await messageHandling()
});
});
app.get("/timedout", async function(req, res){
if(!req.cookies.loggedIn) return res.redirect("/login");
res.render("timedout", {
time: "5 AM"
});
});
app.post('/api/image-upload', upload.single('file'), async (req, res) => {
if (req.file) {
const arrayBuffer = req.file.buffer;
//fs.writeFileSync(`uploads/${req.file.originalname}`, Buffer.from(arrayBuffer));
await require("./bot.cjs").sendAttachment(Buffer.from(arrayBuffer), req.file.originalname);
res.redirect("/");
}
});
app.get("/login", async (req, res) => {
res.render("login");
});
app.post("/api/send", async (req, res) => {
const cont = req.body.message;
await require("./bot.cjs").sendMessage(cont);
res.redirect("/");
});
app.post("/api/typing", async (req, res) => {
require("./bot.cjs").sendTyping();
});
app.get("/api/updates", async (req, res) => {
res.send(await messageHandling());
});
app.post("/api/loginReq", async (req, res) => {
const username = req.body.username;
const password = req.body.password;
if(username == config.username && password == config.password){
res.cookie("loggedIn", "true", "expires", 0);
res.redirect("/");
}
});
app.listen(port, () => {
console.log(`Logged in on port ${port}`)
});
process.on("uncaughtException", (err, origin) => {
console.error(`Caught exception ${err} at ${origin}`);
});
process.on("unhandledRejection", (reason, promise) => {
console.error(`Promise ${promise} was rejected because ${reason}`);
});