1 |
import { Request, Response } from "express"; |
2 |
import DiscordClient from "../../client/Client"; |
3 |
import auth from "../Auth"; |
4 |
import guildAuth from "../GuildAuth"; |
5 |
import { Route } from "../Router"; |
6 |
import { z } from 'zod'; |
7 |
import { isChannel, isRole } from "../Validator"; |
8 |
|
9 |
export default <Route> { |
10 |
path: '/config/misc/:guild', |
11 |
middleware: [guildAuth], |
12 |
async post(req: Request, res: Response) { |
13 |
console.log('log'); |
14 |
|
15 |
const dataArray = await z.array(z.string()); |
16 |
|
17 |
const Config = await z.object({ |
18 |
prefix: z.string(), |
19 |
debug: z.boolean(), |
20 |
starboard: z.object({ |
21 |
enabled: z.boolean(), |
22 |
reactions: z.number().int(), |
23 |
channel: z.string() |
24 |
}), |
25 |
autorole: z.object({ |
26 |
enabled: z.boolean(), |
27 |
roles: dataArray |
28 |
}), |
29 |
warn_notallowed: z.boolean(), |
30 |
global_commands: dataArray, |
31 |
role_commands: z.any(), |
32 |
}); |
33 |
|
34 |
console.log(req.body.data); |
35 |
|
36 |
if (!req.body.data || !Config.safeParse(req.body.data).success) { |
37 |
res.status(422).json({ |
38 |
status: 422, |
39 |
message: "Unprocessable entity", |
40 |
trace: Config.safeParse(req.body.data) |
41 |
}); |
42 |
|
43 |
return; |
44 |
} |
45 |
|
46 |
const { data: body } = req.body; |
47 |
|
48 |
if ( |
49 |
!await isChannel(body.starboard.channel, req.params.guild) |
50 |
) { |
51 |
res.status(422).json({ |
52 |
status: 422, |
53 |
message: "Invalid channels" |
54 |
}); |
55 |
|
56 |
return; |
57 |
} |
58 |
|
59 |
for (const roleID of body.autorole.roles) { |
60 |
if (!await isRole(roleID, req.params.guild)) { |
61 |
res.status(422).json({ |
62 |
status: 422, |
63 |
message: "Invalid roles" |
64 |
}); |
65 |
|
66 |
return; |
67 |
} |
68 |
} |
69 |
|
70 |
for (const cmd of body.global_commands) { |
71 |
if (!DiscordClient.client.commands.has(cmd)) { |
72 |
res.status(422).json({ |
73 |
status: 422, |
74 |
message: "Invalid command" |
75 |
}); |
76 |
|
77 |
return; |
78 |
} |
79 |
} |
80 |
|
81 |
for (const roleID in body.role_commands) { |
82 |
if (!await isRole(roleID, req.params.guild)) { |
83 |
res.status(422).json({ |
84 |
status: 422, |
85 |
message: "Invalid roles (2)" |
86 |
}); |
87 |
|
88 |
return; |
89 |
} |
90 |
|
91 |
console.log(roleID, body.role_commands[roleID]); |
92 |
} |
93 |
|
94 |
const previous = {...DiscordClient.client.config.props[req.params.guild]}; |
95 |
|
96 |
const current = { |
97 |
...DiscordClient.client.config.props[req.params.guild], |
98 |
...body |
99 |
}; |
100 |
|
101 |
DiscordClient.client.config.props[req.params.guild] = current; |
102 |
DiscordClient.client.config.write(); |
103 |
|
104 |
await res.json({ |
105 |
previous, |
106 |
current, |
107 |
input: req.body |
108 |
}); |
109 |
} |
110 |
}; |