7 |
import ValidatorError from "../middleware/ValidatorError"; |
import ValidatorError from "../middleware/ValidatorError"; |
8 |
import Request from "../Request"; |
import Request from "../Request"; |
9 |
import merge from 'ts-deepmerge'; |
import merge from 'ts-deepmerge'; |
10 |
|
import { config } from "../../client/Config"; |
11 |
|
|
12 |
export default class ConfigController extends Controller { |
export default class ConfigController extends Controller { |
13 |
globalMiddleware(): Function[] { |
globalMiddleware(): Function[] { |
22 |
}; |
}; |
23 |
} |
} |
24 |
|
|
25 |
private zodSchema() { |
private zodSchema(id: string) { |
26 |
const snowflake = zod.string().regex(/\d+/, { message: "The given value is not a Snowflake" }); |
const snowflake = zod.string().regex(/\d+/, { message: "The given value is not a Snowflake" }); |
27 |
|
const config = this.client.config.props[id]; |
28 |
|
|
29 |
const schema = zod.object({ |
const schema = zod.object({ |
30 |
"prefix": zod.string().optional(), |
"prefix": zod.string().optional(), |
41 |
"role_commands": zod.record( |
"role_commands": zod.record( |
42 |
snowflake, |
snowflake, |
43 |
zod.array(zod.string().min(1)) |
zod.array(zod.string().min(1)) |
44 |
).optional(), |
).optional().default({}), |
45 |
"autoclear": zod.object({ |
"autoclear": zod.object({ |
46 |
"enabled": zod.boolean().optional(), |
"enabled": zod.boolean().optional(), |
47 |
"channels": zod.array(snowflake).optional() |
"channels": zod.array(snowflake).optional().default(config.autoclear.channels) |
48 |
}).optional(), |
}).optional(), |
49 |
"verification": zod.object({ |
"verification": zod.object({ |
50 |
"enabled": zod.boolean().optional(), |
"enabled": zod.boolean().optional(), |
68 |
}).optional(), |
}).optional(), |
69 |
"autorole": zod.object({ |
"autorole": zod.object({ |
70 |
"enabled": zod.boolean().optional(), |
"enabled": zod.boolean().optional(), |
71 |
"roles": zod.array(snowflake).optional() |
"roles": zod.array(snowflake).optional().default(config.autorole.roles) |
72 |
}).optional(), |
}).optional(), |
73 |
"spam_filter": zod.object({ |
"spam_filter": zod.object({ |
74 |
"enabled": zod.boolean().optional(), |
"enabled": zod.boolean().optional(), |
75 |
"limit": zod.number().int().optional(), |
"limit": zod.number().int().optional(), |
76 |
"time": zod.number().optional(), |
"time": zod.number().optional(), |
77 |
"diff": zod.number().optional(), |
"diff": zod.number().optional(), |
78 |
"exclude": zod.array(snowflake).optional(), |
"exclude": zod.array(snowflake).optional().default(config.spam_filter.exclude), |
79 |
"samelimit": zod.number().int().optional(), |
"samelimit": zod.number().int().optional(), |
80 |
"unmute_in": zod.number().optional() |
"unmute_in": zod.number().optional() |
81 |
}).optional(), |
}).optional(), |
83 |
"enabled": zod.boolean().optional(), |
"enabled": zod.boolean().optional(), |
84 |
"max_joins": zod.number().int().optional(), |
"max_joins": zod.number().int().optional(), |
85 |
"time": zod.number().optional(), |
"time": zod.number().optional(), |
86 |
"channels": zod.array(snowflake).optional(), |
"channels": zod.array(snowflake).optional().default(config.raid.channels), |
87 |
"exclude": zod.boolean().optional() |
"exclude": zod.boolean().optional() |
88 |
}).optional(), |
}).optional(), |
89 |
"global_commands": zod.array(zod.string()).optional(), |
"global_commands": zod.array(zod.string()).optional().default(config.global_commands), |
90 |
"filters": zod.object({ |
"filters": zod.object({ |
91 |
"ignore_staff": zod.boolean().optional(), |
"ignore_staff": zod.boolean().optional(), |
92 |
"chars_repeated": zod.number().int().optional(), |
"chars_repeated": zod.number().int().optional(), |
93 |
"words_repeated": zod.number().int().optional(), |
"words_repeated": zod.number().int().optional(), |
94 |
"words": zod.array(zod.string()).optional(), |
"words": zod.array(zod.string()).optional().default(config.filters.words), |
95 |
"tokens": zod.array(zod.string()).optional(), |
"tokens": zod.array(zod.string()).optional().default(config.filters.tokens), |
96 |
"invite_message": zod.string().optional(), |
"invite_message": zod.string().optional(), |
97 |
"words_excluded": zod.array(snowflake).optional(), |
"words_excluded": zod.array(snowflake).optional().default(config.filters.words_excluded), |
98 |
"domain_excluded": zod.array(snowflake).optional(), |
"domain_excluded": zod.array(snowflake).optional().default(config.filters.domain_excluded), |
99 |
"invite_excluded": zod.array(snowflake).optional(), |
"invite_excluded": zod.array(snowflake).optional().default(config.filters.invite_excluded), |
100 |
"words_enabled": zod.boolean().optional(), |
"words_enabled": zod.boolean().optional(), |
101 |
"invite_enabled": zod.boolean().optional(), |
"invite_enabled": zod.boolean().optional(), |
102 |
"domain_enabled": zod.boolean().optional(), |
"domain_enabled": zod.boolean().optional(), |
103 |
"regex": zod.boolean().optional(), |
"regex": zod.boolean().optional(), |
104 |
"file_mimes_excluded": zod.array(zod.string()).optional(), |
"file_mimes_excluded": zod.array(zod.string()).optional().default(config.filters.file_mimes_excluded), |
105 |
"file_types_excluded": zod.array(zod.string()).optional(), |
"file_types_excluded": zod.array(zod.string()).optional().default(config.filters.file_types_excluded), |
106 |
"domains": zod.array(zod.string()).optional(), |
"domains": zod.array(zod.string()).optional().default(config.filters.domains), |
107 |
"regex_patterns": zod.array(zod.string()).optional(), |
"regex_patterns": zod.array(zod.string()).optional().default(config.filters.regex_patterns), |
108 |
"rickrolls_enabled": zod.boolean().optional(), |
"rickrolls_enabled": zod.boolean().optional(), |
109 |
"pings": zod.number().int().optional() |
"pings": zod.number().int().optional() |
110 |
}).optional() |
}).optional() |
129 |
|
|
130 |
public async update(request: Request) { |
public async update(request: Request) { |
131 |
const { id } = request.params; |
const { id } = request.params; |
132 |
const { config } = request.body; |
const { config: origconfig } = request.body; |
133 |
|
|
134 |
console.log(config); |
console.log(origconfig); |
135 |
|
|
136 |
|
try { |
137 |
|
const currentConfigDotObject = this.client.config.props[id]; |
138 |
|
|
139 |
|
console.log("Current config: ", currentConfigDotObject); |
140 |
|
|
141 |
|
const result = this.zodSchema(id).safeParse(object(origconfig)); |
142 |
|
|
143 |
|
if (!result?.success) { |
144 |
|
return this.response({ error: "The data schema does not match.", error_type: 'validation', errors: result.error.errors }, 422); |
145 |
|
} |
146 |
|
|
147 |
|
const config = result.data; |
148 |
|
const newConfig = merge.withOptions({ mergeArrays: false }, currentConfigDotObject, config); |
149 |
|
const result2 = this.zodSchema(id).safeParse(newConfig); |
150 |
|
|
151 |
|
if (!result2?.success) { |
152 |
|
console.log(result2.error.errors); |
153 |
|
return this.response({ error: 'Internal Server Error (500)', error_type: 'internal' }, 500); |
154 |
|
} |
155 |
|
|
156 |
|
console.log('Final', newConfig); |
157 |
|
this.client.config.props[id] = newConfig; |
158 |
|
this.client.config.write(); |
159 |
|
return { message: "Configuration updated", previous: dot(currentConfigDotObject), new: dot(this.client.config.props[id]) }; |
160 |
|
} |
161 |
|
catch (e) { |
162 |
|
console.log(e); |
163 |
|
return this.response({ error: 'Internal Server Error', error_type: 'internal' }, 500); |
164 |
|
} |
165 |
|
} |
166 |
|
|
167 |
|
public async update2(request: Request) { |
168 |
|
const { id } = request.params; |
169 |
|
const { config: origconfig } = request.body; |
170 |
|
|
171 |
|
console.log(origconfig); |
172 |
|
|
173 |
try { |
try { |
174 |
const currentConfigDotObject = dot(this.client.config.props[id]); |
const currentConfigDotObject = dot(this.client.config.props[id]); |
175 |
let newConfigDotObject = {...currentConfigDotObject}; |
let newConfigDotObject = {...currentConfigDotObject}; |
176 |
|
|
177 |
console.log("Input: ", config); |
console.log("Current config: ", currentConfigDotObject); |
178 |
|
|
179 |
const result = this.zodSchema().safeParse(object({...config})); |
const result = this.zodSchema(id).safeParse(object({...origconfig})); |
180 |
|
|
181 |
if (!result?.success) { |
if (!result?.success) { |
182 |
return this.response({ error: "The data schema does not match.", error_type: 'validation', errors: result.error.errors }, 422); |
return this.response({ error: "The data schema does not match.", error_type: 'validation', errors: result.error.errors }, 422); |
183 |
} |
} |
184 |
|
|
185 |
for (const configKey in config) { |
const config = result.data; |
186 |
|
|
187 |
|
console.log(config); |
188 |
|
|
189 |
|
for (const key in config) { |
190 |
|
const configKey = key as keyof typeof config; |
191 |
const regexMatched = /(.+)\[\d+\]/g.test(configKey); |
const regexMatched = /(.+)\[\d+\]/g.test(configKey); |
192 |
|
|
193 |
if (typeof currentConfigDotObject[configKey] === 'undefined' && !regexMatched) { |
if (typeof currentConfigDotObject[configKey] === 'undefined' && !regexMatched) { |
194 |
return this.response({ error: `The key '${configKey}' is not allowed` }, 422); |
console.log(configKey, config[configKey]); |
195 |
|
|
196 |
|
if (currentConfigDotObject[configKey] instanceof Array && config[configKey] instanceof Array) { |
197 |
|
console.log('Array'); |
198 |
|
} |
199 |
|
else |
200 |
|
return this.response({ error: `The key '${configKey}' is not allowed` }, 422); |
201 |
} |
} |
202 |
|
|
203 |
if (!regexMatched && config[configKey] !== null && config[configKey] !== null && typeof config[configKey] !== typeof currentConfigDotObject[configKey]) { |
if (!regexMatched && config[configKey] !== null && config[configKey] !== null && typeof config[configKey] !== typeof currentConfigDotObject[configKey]) { |
204 |
console.log(typeof config[configKey], typeof currentConfigDotObject[configKey]); |
console.log(typeof config[configKey], typeof currentConfigDotObject[configKey]); |
205 |
|
|
206 |
if (typeof currentConfigDotObject[configKey] === 'number' && typeof config[configKey] === 'string') { |
if (typeof currentConfigDotObject[configKey] === 'number' && typeof config[configKey] === 'string') { |
207 |
const int = parseInt(config[configKey]); |
const int = parseInt(config[configKey]!.toString()); |
208 |
|
|
209 |
if (int !== NaN) { |
if (int !== NaN) { |
210 |
newConfigDotObject[configKey] = int; |
newConfigDotObject[configKey] = int; |
219 |
console.log("Updating: ", configKey, config[configKey], newConfigDotObject[configKey]); |
console.log("Updating: ", configKey, config[configKey], newConfigDotObject[configKey]); |
220 |
} |
} |
221 |
|
|
222 |
|
const newObj = object({...newConfigDotObject}); |
223 |
|
const configObj = object({...config}); |
224 |
|
|
225 |
|
// console.log("Newobj", newObj); |
226 |
|
// console.log("Configobj", configObj); |
227 |
|
|
228 |
newConfigDotObject = merge.withOptions({ mergeArrays: false }, object({...newConfigDotObject}), object({...config})); |
newConfigDotObject = merge.withOptions({ mergeArrays: false }, newObj, configObj); |
229 |
console.log("Output: ", newConfigDotObject); |
console.log("Output: ", newConfigDotObject); |
230 |
|
|
231 |
this.client.config.props[id] = newConfigDotObject; |
this.client.config.props[id] = newConfigDotObject; |