-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidators.ts
74 lines (57 loc) · 2.84 KB
/
validators.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { Plan } from "./definitions/cc_bot";
import { CloudCordModule, ModuleConfig } from "./definitions/cc_module";
// Command Prefix Validation
type PrefixAllowedResponse = {
allowed: boolean,
error?: string
}
export const BASIC_ALLOWED_PREFIXES = ['.', '!', '?', '-', '+', '*', '/'];
export const isValidPlan = (plan: Plan): boolean => (
typeof plan === 'string' && Object.values(Plan).includes(plan)
)
/**
* Checks if the prefix a user wants a bot to take matches with our guards
* @param plan Plan of user
* @param prefix Prefix user wants the bot to take
*/
export const prefixAllowed = (plan: Plan, prefix: string): PrefixAllowedResponse => {
if (!isValidPlan(plan)) return { allowed: false, error: "Plan is not valid" };
if (prefix.length > 12) return { allowed: false, error: "Prefix too long" };
if (plan !== Plan.BASIC) return { allowed: true };
if (BASIC_ALLOWED_PREFIXES.includes(prefix)) return { allowed: true };
return { allowed: false, error: `Basic accounts may only use a small list of prefixes` }
}
// Module Config Validation
export const MODULE_ROOT_KEYS = ['disabled_commands', 'command_permissions', 'command_options'];
export const PERMISSION_TYPES = ['everyone', 'named_role'];
type ModuleConfigValidatorResponse = {
valid: boolean,
error?: string
}
const validateModuleKeys = (omf: CloudCordModule, o: ModuleConfig, m: ModuleConfig): boolean => {
const mRoot = Object.keys(m),
oCmds = Object.keys(omf.commands)
// Makes sure there are no additional keys added to the root of the module object
if(mRoot.filter((i) => {return MODULE_ROOT_KEYS.indexOf(i) < 0;}).length > 0) return false;
// Makes sure the user hasn't disabled any commands which aren't part of the module
if(m.disabled_commands) if(m.disabled_commands.filter(i => {return !oCmds.includes(i)}).length > 0) return false;
// Makes sure user hasn't tried to set permissions of any commands that aren't part of the module
if(m.command_permissions) if(Object.keys(m.command_permissions).filter(i => {return !oCmds.includes(i)}).length > 0) return false;
// Makes sure command permissions conform to correct structure
if(m.command_permissions) if(!Object.keys(m.command_permissions).every(i => {
const permission = m.command_permissions[i];
if(!permission) return false;
if(!PERMISSION_TYPES.includes(permission.type)) return false;
return true;
})) return false;
// Checks passed!
return true;
}
/**
* Checks if the config a user wants a module to take matches with our guards
* @param config Customized config
*/
export const validModuleConfig = (module: CloudCordModule, originalModuleConfig: ModuleConfig, newConfig: ModuleConfig): ModuleConfigValidatorResponse => {
if(!validateModuleKeys(module, originalModuleConfig, newConfig)) return {"valid": false, "error": "Invalid module structure"};
return {"valid": true};
}