66 lines
No EOL
1.8 KiB
TypeScript
66 lines
No EOL
1.8 KiB
TypeScript
import {ChannelId} from "../types/DiscordTypes";
|
|
import {GroupConfigurationModel} from "../Models/GroupConfigurationModel";
|
|
import {Nullable} from "../types/Nullable";
|
|
import {ArrayUtils} from "../Utilities/ArrayUtils";
|
|
|
|
export enum TransformerType {
|
|
Locale,
|
|
Channel,
|
|
PermissionBoolean,
|
|
}
|
|
|
|
type GroupConfigurationTransformer = {
|
|
path: string[];
|
|
type: TransformerType,
|
|
}
|
|
|
|
export type GroupConfigurationResult =
|
|
ChannelId | Intl.Locale | boolean
|
|
|
|
export class GroupConfigurationTransformers {
|
|
static TRANSFORMERS: GroupConfigurationTransformer[] = [
|
|
{
|
|
path: ['channels', 'newPlaydates'],
|
|
type: TransformerType.Channel,
|
|
},
|
|
{
|
|
path: ['channels', 'playdateReminders'],
|
|
type: TransformerType.Channel,
|
|
},
|
|
{
|
|
path: ['locale'],
|
|
type: TransformerType.Locale,
|
|
},
|
|
{
|
|
path: ['permissions', 'allowMemberManagingPlaydates'],
|
|
type: TransformerType.PermissionBoolean
|
|
}
|
|
];
|
|
|
|
public getValue(configValue: GroupConfigurationModel): GroupConfigurationResult {
|
|
const transformerType = this.getTransformerType(configValue.key);
|
|
if (transformerType === undefined || transformerType === null) {
|
|
throw new Error(`Can't find transformer for ${configValue.key}`);
|
|
}
|
|
|
|
switch (transformerType) {
|
|
case TransformerType.Locale:
|
|
return new Intl.Locale(configValue.value)
|
|
case TransformerType.Channel:
|
|
return <ChannelId>configValue.value;
|
|
case TransformerType.PermissionBoolean:
|
|
return configValue.value === '1';
|
|
}
|
|
}
|
|
|
|
public getTransformerType(configKey: string): Nullable<TransformerType> {
|
|
const path = configKey.split('.');
|
|
return GroupConfigurationTransformers.TRANSFORMERS.find(
|
|
transformer => {
|
|
return ArrayUtils.arraysEqual(transformer.path, path);
|
|
}
|
|
)?.type;
|
|
}
|
|
|
|
|
|
} |