Channel stuff

This commit is contained in:
Hri7566 2023-09-09 01:37:19 -04:00
parent bcd812cdf4
commit 7cdcdcfa5a
2 changed files with 76 additions and 0 deletions

11
src/channel/Channel.ts Normal file
View File

@ -0,0 +1,11 @@
// TODO Load channel config file
export class Channel {
constructor(private _id: string) {}
getID() {
return this._id;
}
isLobby() {}
}

65
src/channel/settings.ts Normal file
View File

@ -0,0 +1,65 @@
import { ChannelSettings } from "../util/types";
type Validator = "boolean" | "string" | "number" | ((val: any) => boolean);
const validationRecord: Record<keyof ChannelSettings, Validator> = {
// Brandon
lobby: "boolean",
visible: "boolean",
chat: "boolean",
crownsolo: "boolean",
"no cussing": "boolean",
"lyrical notes": "boolean",
color: val => {
return typeof val === "string" && !!val.match(/^#[0-9a-f]{6}$/i);
},
color2: val => {
return typeof val === "string" && !!val.match(/^#[0-9a-f]{6}$/i);
},
owner_id: "string",
// MPPClone (why?)
limit: "number",
noindex: "boolean"
};
/**
* Check the validity of channel settings
* @param set Unknown data
* @returns Record of which settings are correct
*/
export function validateChannelSettings(set: Partial<ChannelSettings>) {
// Create record
let keys = Object.keys(validationRecord);
let record: Partial<Record<keyof ChannelSettings, boolean>> = {};
for (const key of Object.keys(set)) {
let val = (set as Record<string, any>)[key];
let validator = (
validationRecord as Record<string, Validator | undefined>
)[key];
// Do we have a validator?
if (!validator) {
// Skip setting
continue;
}
// Set valid status
record[key as keyof ChannelSettings] = validate(val, validator);
}
return record;
}
function validate(value: any, validator: Validator) {
// What type of validator?
if (typeof validator == "function") {
// We are copying Zod's functionality
return validator(value) === true;
} else if (typeof value === validator) {
return true;
}
return false;
}