/[sudobot]/branches/4.x/src/client/Config.ts
ViewVC logotype

Annotation of /branches/4.x/src/client/Config.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 577 - (hide annotations)
Mon Jul 29 18:52:37 2024 UTC (8 months ago) by rakinar2
File MIME type: application/typescript
File size: 8340 byte(s)
chore: add old version archive branches (2.x to 9.x-dev)
1 rakinar2 577 /**
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     import DiscordClient from "./Client";
21     import path from "path";
22     import fs from "fs";
23     import { z as zod } from 'zod';
24    
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     configPath: string;
37    
38     constructor(client: DiscordClient) {
39     this.client = client;
40     console.log(`ENV: ${process.env.SUDO_PREFIX}`);
41     this.configPath = path.resolve(process.env.SUDO_PREFIX ?? this.client.rootdir, "config", "config.json");
42     this.load();
43     }
44    
45     load() {
46     fs.readFile(this.configPath, (err, data) => {
47     if (err) {
48     console.log(err);
49     }
50    
51     this.props = JSON.parse(data.toString());
52     });
53     }
54    
55     write() {
56     fs.writeFile(this.configPath, JSON.stringify(this.props, undefined, ' '), () => null);
57     }
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    
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     "ai_mod": zod.object({
84     "enabled": zod.boolean().optional(),
85     "toxicity": zod.number().optional(),
86     "severe_toxicity": zod.number().optional(),
87     "threat": zod.number().optional(),
88     }).optional(),
89     "role_commands": zod.record(
90     snowflake,
91     zod.array(zod.string().min(1))
92     ).optional().default({}),
93     "autoclear": zod.object({
94     "enabled": zod.boolean().optional(),
95     "channels": zod.array(snowflake).optional().default(config.autoclear.channels)
96     }).optional(),
97     "verification": zod.object({
98     "enabled": zod.boolean().optional(),
99     "role": snowflake.optional()
100     }).optional(),
101     "welcomer": zod.object({
102     "enabled": zod.boolean().optional(),
103     "channel": snowflake.optional(),
104     "message": zod.string().min(1).or(zod.null()).optional(),
105     "randomize": zod.boolean().optional()
106     }).optional(),
107     "cooldown": zod.object({
108     "enabled": zod.boolean().optional(),
109     "global": zod.any().optional(),
110     "cmds": zod.object({}).optional()
111     }).optional(),
112     "profile_filter": zod.object({
113     "enabled": zod.boolean().optional(),
114     "inherit_from_words": zod.boolean().optional(),
115     "inherit_from_tokens": zod.boolean().optional(),
116     "inherit_from_regex": zod.boolean().optional(),
117     "blocked_words": zod.array(zod.string()).optional().default(config.profile_filter.blocked_words),
118     "blocked_tokens": zod.array(zod.string()).optional().default(config.profile_filter.blocked_tokens),
119     "blocked_regex_patterns": zod.array(zod.string()).optional().default(config.profile_filter.blocked_regex_patterns),
120     "components": zod.object({
121     "tag": zod.boolean().optional(),
122     "nickname": zod.boolean().optional(),
123     "about": zod.boolean().optional(),
124     "status": zod.boolean().optional()
125     }).optional(),
126     "actions": zod.object({
127     "tag": zod.string().optional(),
128     "nickname": zod.string().optional(),
129     "about": zod.string().optional(),
130     "status": zod.string().optional()
131     }).optional()
132     }).optional(),
133     "starboard": zod.object({
134     "enabled": zod.boolean().optional(),
135     "reactions": zod.number().int().optional(),
136     "channel": snowflake.optional()
137     }).optional(),
138     "autorole": zod.object({
139     "enabled": zod.boolean().optional(),
140     "roles": zod.array(snowflake).optional().default(config.autorole.roles)
141     }).optional(),
142     "spam_filter": zod.object({
143     "enabled": zod.boolean().optional(),
144     "limit": zod.number().int().optional(),
145     "time": zod.number().optional(),
146     "diff": zod.number().optional(),
147     "exclude": zod.array(snowflake).optional().default(config.spam_filter.exclude),
148     "samelimit": zod.number().int().optional(),
149     "unmute_in": zod.number().optional()
150     }).optional(),
151     "raid": zod.object({
152     "enabled": zod.boolean().optional(),
153     "max_joins": zod.number().int().optional(),
154     "time": zod.number().optional(),
155     "channels": zod.array(snowflake).optional().default(config.raid.channels),
156     "exclude": zod.boolean().optional()
157     }).optional(),
158     "global_commands": zod.array(zod.string()).optional().default(config.global_commands),
159     "filters": zod.object({
160     "ignore_staff": zod.boolean().optional(),
161     "chars_repeated": zod.number().int().optional(),
162     "words_repeated": zod.number().int().optional(),
163     "words": zod.array(zod.string()).optional().default(config.filters.words),
164     "tokens": zod.array(zod.string()).optional().default(config.filters.tokens),
165     "invite_message": zod.string().optional(),
166     "words_excluded": zod.array(snowflake).optional().default(config.filters.words_excluded),
167     "domain_excluded": zod.array(snowflake).optional().default(config.filters.domain_excluded),
168     "invite_excluded": zod.array(snowflake).optional().default(config.filters.invite_excluded),
169     "words_enabled": zod.boolean().optional(),
170     "invite_enabled": zod.boolean().optional(),
171     "domain_enabled": zod.boolean().optional(),
172     "regex": zod.boolean().optional(),
173     "file_mimes_excluded": zod.array(zod.string()).optional().default(config.filters.file_mimes_excluded),
174     "file_types_excluded": zod.array(zod.string()).optional().default(config.filters.file_types_excluded),
175     "domains": zod.array(zod.string()).optional().default(config.filters.domains),
176     "regex_patterns": zod.array(zod.string()).optional().default(config.filters.regex_patterns),
177     "rickrolls_enabled": zod.boolean().optional(),
178     "pings": zod.number().int().optional()
179     }).optional()
180     });
181     }
182     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26