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/mod/:guild', |
11 |
middleware: [guildAuth], |
12 |
async post(req: Request, res: Response) { |
13 |
const dataArray = await z.array(z.string()); |
14 |
|
15 |
const Config = await z.object({ |
16 |
mute_role: z.string(), |
17 |
gen_role: z.string(), |
18 |
logging_channel: z.string(), |
19 |
logging_channel_join_leave: z.string(), |
20 |
mod_role: z.string(), |
21 |
announcement_channel: z.string(), |
22 |
autoclear: z.object({ |
23 |
enabled: z.boolean(), |
24 |
channels: dataArray |
25 |
}), |
26 |
spam_filter: z.object({ |
27 |
enabled: z.boolean(), |
28 |
limit: z.number().int(), |
29 |
time: z.number(), |
30 |
diff: z.number(), |
31 |
exclude: dataArray, |
32 |
samelimit: z.number().int(), |
33 |
unmute_in: z.number() |
34 |
}), |
35 |
raid: z.object({ |
36 |
enabled: z.boolean(), |
37 |
max_joins: z.number().int(), |
38 |
time: z.number(), |
39 |
channels: dataArray, |
40 |
exclude: z.boolean() |
41 |
}), |
42 |
lockall: dataArray, |
43 |
// warn_notallowed: z.boolean(), |
44 |
// global_commands: dataArray, |
45 |
// role_commands: z.any(), |
46 |
filters: z.object({ |
47 |
ignore_staff: z.boolean(), |
48 |
words_enabled: z.boolean(), |
49 |
invite_enabled: z.boolean(), |
50 |
regex: z.boolean(), |
51 |
chars_repeated: z.number().int(), |
52 |
words_repeated: z.number().int(), |
53 |
words: dataArray, |
54 |
invite_message: z.string(), |
55 |
words_excluded: dataArray, |
56 |
invite_excluded: dataArray, |
57 |
file_mimes_excluded: dataArray, |
58 |
file_types_excluded: dataArray |
59 |
}) |
60 |
}); |
61 |
|
62 |
console.log(req.body.data); |
63 |
const parsed = Config.safeParse(req.body.data); |
64 |
|
65 |
if (!req.body.data || !parsed.success) { |
66 |
console.log(parsed); |
67 |
|
68 |
res.status(422).json({ |
69 |
status: 422, |
70 |
message: "Unprocessable entity" |
71 |
}); |
72 |
|
73 |
return; |
74 |
} |
75 |
|
76 |
const { data: body } = req.body; |
77 |
|
78 |
if ( |
79 |
!(await isRole(body.mod_role, req.params.guild)) || |
80 |
!(await isRole(body.gen_role, req.params.guild)) || |
81 |
!(await isRole(body.mute_role, req.params.guild)) |
82 |
) { |
83 |
res.status(422).json({ |
84 |
status: 422, |
85 |
message: "Invalid roles" |
86 |
}); |
87 |
|
88 |
return; |
89 |
} |
90 |
|
91 |
// for (const roleID in body.role_commands) { |
92 |
// if (!await isRole(roleID, req.params.guild)) { |
93 |
// res.status(422).json({ |
94 |
// status: 422, |
95 |
// message: "Invalid roles (2)" |
96 |
// }); |
97 |
|
98 |
// return; |
99 |
// } |
100 |
// } |
101 |
|
102 |
await console.log(body.logging_channel, await isChannel(body.logging_channel, req.params.guild)); |
103 |
|
104 |
|
105 |
if ( |
106 |
!await isChannel(body.logging_channel, req.params.guild) || |
107 |
!await isChannel(body.logging_channel_join_leave, req.params.guild) || |
108 |
!await isChannel(body.announcement_channel, req.params.guild) |
109 |
) { |
110 |
res.status(422).json({ |
111 |
status: 422, |
112 |
message: "Invalid channels" |
113 |
}); |
114 |
|
115 |
return; |
116 |
} |
117 |
|
118 |
const { spam_filter, raid, filters, autoclear } = body; |
119 |
|
120 |
console.log(autoclear); |
121 |
|
122 |
for (const id of body.spam_filter.exclude) { |
123 |
if (!await isChannel(id, req.params.guild)) { |
124 |
res.status(422).json({ |
125 |
status: 422, |
126 |
message: "Invalid channels (2)" |
127 |
}); |
128 |
|
129 |
return; |
130 |
} |
131 |
} |
132 |
|
133 |
for (const id of raid.channels) { |
134 |
if (!await isChannel(id, req.params.guild)) { |
135 |
res.status(422).json({ |
136 |
status: 422, |
137 |
message: "Invalid channels (3)" |
138 |
}); |
139 |
|
140 |
return; |
141 |
} |
142 |
} |
143 |
|
144 |
for (const id of body.lockall) { |
145 |
if (!await isChannel(id, req.params.guild)) { |
146 |
res.status(422).json({ |
147 |
status: 422, |
148 |
message: "Invalid channels (4)" |
149 |
}); |
150 |
|
151 |
return; |
152 |
} |
153 |
} |
154 |
|
155 |
for (const id of filters.words_excluded) { |
156 |
if (!await isChannel(id, req.params.guild)) { |
157 |
res.status(422).json({ |
158 |
status: 422, |
159 |
message: "Invalid channels (5)" |
160 |
}); |
161 |
|
162 |
return; |
163 |
} |
164 |
} |
165 |
|
166 |
for (const id of filters.invite_excluded) { |
167 |
if (!await isChannel(id, req.params.guild)) { |
168 |
res.status(422).json({ |
169 |
status: 422, |
170 |
message: "Invalid channels (6)" |
171 |
}); |
172 |
|
173 |
return; |
174 |
} |
175 |
} |
176 |
|
177 |
for (const id of autoclear.channels) { |
178 |
if (!await isChannel(id, req.params.guild)) { |
179 |
res.status(422).json({ |
180 |
status: 422, |
181 |
message: "Invalid channels (7)" |
182 |
}); |
183 |
|
184 |
return; |
185 |
} |
186 |
} |
187 |
|
188 |
// for (const cmd of body.global_commands) { |
189 |
// if (!DiscordClient.client.commands.has(cmd)) { |
190 |
// res.status(422).json({ |
191 |
// status: 422, |
192 |
// message: "Invalid command" |
193 |
// }); |
194 |
|
195 |
// return; |
196 |
// } |
197 |
// } |
198 |
|
199 |
const previous = {...DiscordClient.client.config.props[req.params.guild]}; |
200 |
|
201 |
const current = { |
202 |
...DiscordClient.client.config.props[req.params.guild], |
203 |
mute_role: body.mute_role, |
204 |
gen_role: body.gen_role, |
205 |
logging_channel: body.logging_channel, |
206 |
logging_channel_join_leave: body.logging_channel_join_leave, |
207 |
mod_role: body.mod_role, |
208 |
announcement_channel: body.announcement_channel, |
209 |
autoclear: { |
210 |
enabled: autoclear.enabled, |
211 |
channels: autoclear.channels |
212 |
}, |
213 |
spam_filter: { |
214 |
enabled: spam_filter.enabled, |
215 |
limit: parseInt(spam_filter.limit), |
216 |
time: parseInt(spam_filter.time), |
217 |
diff: parseInt(spam_filter.diff), |
218 |
exclude: spam_filter.exclude, |
219 |
samelimit: parseInt(spam_filter.samelimit), |
220 |
unmute_in: spam_filter.unmute_in |
221 |
}, |
222 |
raid: { |
223 |
enabled: raid.enabled, |
224 |
max_joins: parseInt(raid.max_joins), |
225 |
time: raid.time, |
226 |
channels: raid.channels, |
227 |
exclude: raid.exclude |
228 |
}, |
229 |
lockall: body.lockall, |
230 |
// warn_notallowed: body.warn_notallowed, |
231 |
// global_commands: body.global_commands, |
232 |
// role_commands: body.role_commands, |
233 |
filters: { |
234 |
...filters, |
235 |
chars_repeated: parseInt(filters.chars_repeated), |
236 |
words_repeated: parseInt(filters.words_repeated), |
237 |
} |
238 |
}; |
239 |
|
240 |
DiscordClient.client.config.props[req.params.guild] = current; |
241 |
DiscordClient.client.config.write(); |
242 |
|
243 |
await res.json({ |
244 |
previous, |
245 |
current, |
246 |
input: req.body |
247 |
}); |
248 |
} |
249 |
}; |