This repository has been archived by the owner on Jun 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.ts
60 lines (54 loc) · 1.74 KB
/
http.ts
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
import * as Discord from 'discord.js';
import express, { NextFunction, Request, Response } from 'express';
import moment from "moment";
import { Logger } from "./logger";
function logErrors(err: any, req: Request, res: Response, next: NextFunction) {
Logger.error('[Express error]', err);
next(err);
}
function errorHandler(err: any, req: Request, res: Response, next: NextFunction) {
res.status(500).json({ error: err.toString() });
}
export function initHttp(client: Discord.Client) {
const app = express();
app.set('view engine', 'ejs');
app.use(express.urlencoded({
extended: true,
}));
app.use(express.json());
app.use(logErrors);
app.use(errorHandler);
app.get('/', (req, res) => {
res.render('index', {
readyAt: client.readyAt ? moment(client.readyAt).format('YYYY-MM-DD HH:mm:ss [UTC]ZZ') : '',
uptime: client.uptime,
ping: client.ws.ping,
date: moment().format('YYYY-MM-DD HH:mm:ss [UTC]ZZ'),
});
});
app.get('/channels/:guildId/:channelId/:messageId', async (req, res) => {
try {
const guild = client.guilds.resolve(req.params.guildId);
if (guild) {
const channel = guild.channels.resolve(req.params.channelId);
if (channel && channel.type === 'text') {
const textChannel = channel as Discord.TextChannel;
let message = textChannel.messages.resolve(req.params.messageId);
if (!message) {
message = await textChannel.messages.fetch(req.params.messageId);
}
if (message) {
const json: any = message.toJSON();
json['author'] = message.author.toJSON();
res.json(json);
return;
}
}
}
res.status(404).json({ error: 'Not Found' });
} catch (err: any) {
res.status(500).json({ error: err.toString() });
}
});
app.listen(process.env.PORT || 8080);
}