/[sudobot]/trunk/src/client/Config.ts
ViewVC logotype

Annotation of /trunk/src/client/Config.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 415 - (hide annotations)
Mon Jul 29 17:30:07 2024 UTC (8 months, 1 week ago) by rakin
File MIME type: application/typescript
File size: 8061 byte(s)
fix: update config schema (#82)

* refactor: extract the config schema to a separate method

* style: remove uneeded imports

* fix: update config schema (#79)
1 rakin 393 /**
2     * This file is part of SudoBot.
3     *
4     * Copyright (C) 2021-2022 OSN Inc.
5     *
6     * SudoBot is free software; you can redistribute it and/or modify it
7     * under the terms of the GNU Affero General Public License as published by
8     * the Free Software Foundation, either version 3 of the License, or
9     * (at your option) any later version.
10     *
11     * SudoBot is distributed in the hope that it will be useful, but
12     * WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     * GNU Affero General Public License for more details.
15     *
16     * You should have received a copy of the GNU Affero General Public License
17     * along with SudoBot. If not, see <https://www.gnu.org/licenses/>.
18     */
19    
20 rakin 51 import DiscordClient from "./Client";
21     import path from "path";
22     import fs from "fs";
23 rakin 415 import { z as zod } from 'zod';
24 rakin 51
25     export type config = {
26     [key: string]: any;
27     };
28    
29     export type configContainer = {
30     [guildID: string | number]: config;
31     };
32    
33     export class Config {
34     props: configContainer = {};
35     client: DiscordClient;
36 rakin 360 configPath: string;
37 rakin 51
38     constructor(client: DiscordClient) {
39     this.client = client;
40 rakin 362 console.log(`ENV: ${process.env.SUDO_PREFIX}`);
41 rakin 360 this.configPath = path.resolve(process.env.SUDO_PREFIX ?? this.client.rootdir, "config", "config.json");
42 rakin 51 this.load();
43     }
44    
45     load() {
46 rakin 360 fs.readFile(this.configPath, (err, data) => {
47 rakin 51 if (err) {
48     console.log(err);
49     }
50    
51     this.props = JSON.parse(data.toString());
52     });
53     }
54    
55     write() {
56 rakin 360 fs.writeFile(this.configPath, JSON.stringify(this.props, undefined, ' '), () => null);
57 rakin 51 }
58    
59     get(key: string) {
60     return typeof this.props[this.client.msg!.guild!.id] === 'object' ? this.props[this.client.msg!.guild!.id][key] : null;
61     }
62    
63     set(key: string, value: any) {
64     this.props[this.client.msg!.guild!.id][key] = value;
65     }
66 rakin 415
67     schema(_config: string | config) {
68     const config = typeof _config === 'string' ? this.props[_config] : _config;
69     const snowflake = zod.string().regex(/\d+/, { message: "The given value is not a Snowflake" });
70    
71     return zod.object({
72     "prefix": zod.string().optional(),
73     "debug": zod.boolean().optional(),
74     "mute_role": snowflake.optional(),
75     "gen_role": snowflake.optional(),
76     "logging_channel": snowflake.optional(),
77     "logging_channel_join_leave": snowflake.optional(),
78     "mod_role": snowflake.optional(),
79     "announcement_channel": snowflake.optional(),
80     "admin": snowflake.optional(),
81     "lockall": zod.array(zod.string()).optional(),
82     "warn_notallowed": zod.boolean().optional(),
83     "role_commands": zod.record(
84     snowflake,
85     zod.array(zod.string().min(1))
86     ).optional().default({}),
87     "autoclear": zod.object({
88     "enabled": zod.boolean().optional(),
89     "channels": zod.array(snowflake).optional().default(config.autoclear.channels)
90     }).optional(),
91     "verification": zod.object({
92     "enabled": zod.boolean().optional(),
93     "role": snowflake.optional()
94     }).optional(),
95     "welcomer": zod.object({
96     "enabled": zod.boolean().optional(),
97     "channel": snowflake.optional(),
98     "message": zod.string().min(1).or(zod.null()).optional(),
99     "randomize": zod.boolean().optional()
100     }).optional(),
101     "cooldown": zod.object({
102     "enabled": zod.boolean().optional(),
103     "global": zod.any().optional(),
104     "cmds": zod.object({}).optional()
105     }).optional(),
106     "profile_filter": zod.object({
107     "enabled": zod.boolean().optional(),
108     "inherit_from_words": zod.boolean().optional(),
109     "inherit_from_tokens": zod.boolean().optional(),
110     "inherit_from_regex": zod.boolean().optional(),
111     "blocked_words": zod.array(zod.string()).optional().default(config.profile_filter.blocked_words),
112     "blocked_tokens": zod.array(zod.string()).optional().default(config.profile_filter.blocked_tokens),
113     "blocked_regex_patterns": zod.array(zod.string()).optional().default(config.profile_filter.blocked_regex_patterns),
114     "components": zod.object({
115     "tag": zod.boolean().optional(),
116     "nickname": zod.boolean().optional(),
117     "about": zod.boolean().optional(),
118     "status": zod.boolean().optional()
119     }).optional(),
120     "actions": zod.object({
121     "tag": zod.string().optional(),
122     "nickname": zod.string().optional(),
123     "about": zod.string().optional(),
124     "status": zod.string().optional()
125     }).optional()
126     }).optional(),
127     "starboard": zod.object({
128     "enabled": zod.boolean().optional(),
129     "reactions": zod.number().int().optional(),
130     "channel": snowflake.optional()
131     }).optional(),
132     "autorole": zod.object({
133     "enabled": zod.boolean().optional(),
134     "roles": zod.array(snowflake).optional().default(config.autorole.roles)
135     }).optional(),
136     "spam_filter": zod.object({
137     "enabled": zod.boolean().optional(),
138     "limit": zod.number().int().optional(),
139     "time": zod.number().optional(),
140     "diff": zod.number().optional(),
141     "exclude": zod.array(snowflake).optional().default(config.spam_filter.exclude),
142     "samelimit": zod.number().int().optional(),
143     "unmute_in": zod.number().optional()
144     }).optional(),
145     "raid": zod.object({
146     "enabled": zod.boolean().optional(),
147     "max_joins": zod.number().int().optional(),
148     "time": zod.number().optional(),
149     "channels": zod.array(snowflake).optional().default(config.raid.channels),
150     "exclude": zod.boolean().optional()
151     }).optional(),
152     "global_commands": zod.array(zod.string()).optional().default(config.global_commands),
153     "filters": zod.object({
154     "ignore_staff": zod.boolean().optional(),
155     "chars_repeated": zod.number().int().optional(),
156     "words_repeated": zod.number().int().optional(),
157     "words": zod.array(zod.string()).optional().default(config.filters.words),
158     "tokens": zod.array(zod.string()).optional().default(config.filters.tokens),
159     "invite_message": zod.string().optional(),
160     "words_excluded": zod.array(snowflake).optional().default(config.filters.words_excluded),
161     "domain_excluded": zod.array(snowflake).optional().default(config.filters.domain_excluded),
162     "invite_excluded": zod.array(snowflake).optional().default(config.filters.invite_excluded),
163     "words_enabled": zod.boolean().optional(),
164     "invite_enabled": zod.boolean().optional(),
165     "domain_enabled": zod.boolean().optional(),
166     "regex": zod.boolean().optional(),
167     "file_mimes_excluded": zod.array(zod.string()).optional().default(config.filters.file_mimes_excluded),
168     "file_types_excluded": zod.array(zod.string()).optional().default(config.filters.file_types_excluded),
169     "domains": zod.array(zod.string()).optional().default(config.filters.domains),
170     "regex_patterns": zod.array(zod.string()).optional().default(config.filters.regex_patterns),
171     "rickrolls_enabled": zod.boolean().optional(),
172     "pings": zod.number().int().optional()
173     }).optional()
174     });
175     }
176 rakin 359 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26