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

Contents of /trunk/src/api/routes/misc-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: 3006 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/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 });
41
42 return;
43 }
44
45 const { data: body } = req.body;
46
47 if (
48 !await isChannel(body.starboard.channel, req.params.guild)
49 ) {
50 res.status(422).json({
51 status: 422,
52 message: "Invalid channels"
53 });
54
55 return;
56 }
57
58 for (const roleID of body.autorole.roles) {
59 if (!await isRole(roleID, req.params.guild)) {
60 res.status(422).json({
61 status: 422,
62 message: "Invalid roles"
63 });
64
65 return;
66 }
67 }
68
69 for (const cmd of body.global_commands) {
70 if (!DiscordClient.client.commands.has(cmd)) {
71 res.status(422).json({
72 status: 422,
73 message: "Invalid command"
74 });
75
76 return;
77 }
78 }
79
80 for (const roleID in body.role_commands) {
81 if (!await isRole(roleID, req.params.guild)) {
82 res.status(422).json({
83 status: 422,
84 message: "Invalid roles (2)"
85 });
86
87 return;
88 }
89
90 console.log(roleID, body.role_commands[roleID]);
91 }
92
93 const previous = {...DiscordClient.client.config.props[req.params.guild]};
94
95 const current = {
96 ...DiscordClient.client.config.props[req.params.guild],
97 ...body
98 };
99
100 DiscordClient.client.config.props[req.params.guild] = current;
101 DiscordClient.client.config.write();
102
103 await res.json({
104 previous,
105 current,
106 input: req.body
107 });
108 }
109 };

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26