1 |
rakin |
53 |
import DiscordClient from "../client/Client"; |
2 |
|
|
import express, { Application, Request, Response } from 'express'; |
3 |
|
|
import routes from './routes'; |
4 |
|
|
import router from "./Router"; |
5 |
|
|
import { readFileSync } from "fs"; |
6 |
|
|
import path from "path"; |
7 |
|
|
|
8 |
|
|
export default class Server { |
9 |
|
|
app: Application; |
10 |
|
|
port: number = 4000; |
11 |
|
|
guildData: { |
12 |
|
|
[key: string]: { |
13 |
|
|
token: string; |
14 |
|
|
} |
15 |
|
|
}; |
16 |
|
|
|
17 |
|
|
constructor(protected client: DiscordClient) { |
18 |
|
|
this.app = express(); |
19 |
rakin |
66 |
const data: typeof this.guildData = {}; |
20 |
|
|
|
21 |
|
|
for (const key of Object.keys(process.env)) { |
22 |
|
|
if (key.startsWith('TOKEN_')) { |
23 |
|
|
data[key.replace(/^TOKEN_/g, '')] = { |
24 |
|
|
token: process.env[key]! |
25 |
|
|
}; |
26 |
|
|
} |
27 |
|
|
} |
28 |
|
|
|
29 |
|
|
this.guildData = data; |
30 |
|
|
console.log(data); |
31 |
rakin |
53 |
} |
32 |
|
|
|
33 |
|
|
verifyToken(guild: string, token: string): boolean { |
34 |
|
|
return this.guildData[guild] && this.guildData[guild].token === token; |
35 |
|
|
} |
36 |
|
|
|
37 |
|
|
validToken(token: string): boolean { |
38 |
|
|
for (const key in this.guildData) { |
39 |
|
|
if (this.guildData[key].token === token) |
40 |
|
|
return true; |
41 |
|
|
} |
42 |
|
|
|
43 |
|
|
return false; |
44 |
|
|
} |
45 |
|
|
|
46 |
|
|
registerRoutes() { |
47 |
|
|
this.app.all('/', (req: Request, res: Response) => { |
48 |
|
|
res.send("Server is up."); |
49 |
|
|
}); |
50 |
|
|
|
51 |
|
|
this.app.use('/api', router); |
52 |
|
|
} |
53 |
|
|
|
54 |
|
|
run(port: number = 4000) { |
55 |
|
|
this.port = port; |
56 |
|
|
|
57 |
|
|
this.registerRoutes(); |
58 |
|
|
this.app.listen(port, () => console.log('Server listening at port ' + this.port)); |
59 |
|
|
} |
60 |
|
|
} |