refactor(configuration): Setup configuration and menu to be reuseable
This commit is contained in:
parent
863ae3fab2
commit
d46bbd84c5
21 changed files with 551 additions and 452 deletions
55
source/Configuration/ConfigurationTransformer.ts
Normal file
55
source/Configuration/ConfigurationTransformer.ts
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
import {ChannelId} from "../types/DiscordTypes";
|
||||
import {Nullable} from "../types/Nullable";
|
||||
import {ArrayUtils} from "../Utilities/ArrayUtils";
|
||||
import {ConfigurationModel} from "../Database/Models/ConfigurationModel";
|
||||
|
||||
export enum TransformerType {
|
||||
Channel,
|
||||
PermissionBoolean,
|
||||
String,
|
||||
Paragraph,
|
||||
}
|
||||
|
||||
type ConfigurationTransformerItem = {
|
||||
path: string[];
|
||||
type: TransformerType,
|
||||
}
|
||||
|
||||
export type TransformerResults =
|
||||
ChannelId | boolean | string | null
|
||||
|
||||
export class ConfigurationTransformer {
|
||||
constructor(
|
||||
private readonly transformers: ConfigurationTransformerItem[]
|
||||
) {
|
||||
}
|
||||
|
||||
public getValue(configValue: ConfigurationModel): TransformerResults {
|
||||
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.Channel:
|
||||
return <ChannelId>configValue.value;
|
||||
case TransformerType.PermissionBoolean:
|
||||
return configValue.value === '1';
|
||||
case TransformerType.Paragraph:
|
||||
case TransformerType.String:
|
||||
return configValue.value;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public getTransformerType(configKey: string): Nullable<TransformerType> {
|
||||
const path = configKey.split('.');
|
||||
return this.transformers.find(
|
||||
transformer => {
|
||||
return ArrayUtils.arraysEqual(transformer.path, path);
|
||||
}
|
||||
)?.type;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue