/[sudobot]/trunk/src/api/routes/mod-config.ts
ViewVC logotype

Contents of /trunk/src/api/routes/mod-config.ts

Parent Directory Parent Directory | Revision Log Revision Log


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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26