feat(timezone): Adds timezone as option
This commit is contained in:
parent
0b9089ffae
commit
b852c06f83
11 changed files with 278 additions and 22 deletions
93
source/Configuration/TimezoneHandler.ts
Normal file
93
source/Configuration/TimezoneHandler.ts
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
import {GroupModel} from "../Database/Models/GroupModel";
|
||||
import {ConfigurationHandler, PathConfigurationFrom} from "./ConfigurationHandler";
|
||||
import {GroupConfigurationProvider} from "./Groups/GroupConfigurationProvider";
|
||||
import {Container} from "../Container/Container";
|
||||
import {GroupConfigurationRepository} from "../Database/Repositories/GroupConfigurationRepository";
|
||||
import {ServerConfigurationProvider} from "./Server/ServerConfigurationProvider";
|
||||
import {Snowflake, time} from "discord.js";
|
||||
import {ServerConfigurationRepository} from "../Database/Repositories/ServerConfigurationRepository";
|
||||
import tzdata from 'tzdata';
|
||||
import {Nullable} from "../types/Nullable";
|
||||
|
||||
export type Timezone = {
|
||||
zone: string,
|
||||
gmt: string,
|
||||
name: string,
|
||||
}
|
||||
|
||||
export enum TimezoneSaveTarget {
|
||||
Server,
|
||||
Group
|
||||
}
|
||||
|
||||
export class TimezoneHandler {
|
||||
public static ALL_TIMEZONES: string[] = Object.keys(tzdata.zones)
|
||||
|
||||
constructor(
|
||||
private readonly serverid: Snowflake,
|
||||
private readonly group: GroupModel|null = null
|
||||
) {
|
||||
}
|
||||
|
||||
public use<TReturn>(callback: () => TReturn): TReturn {
|
||||
const previousTZ = process.env.TZ;
|
||||
|
||||
process.env.TZ = this.getCurrentTimezone();
|
||||
const result = callback();
|
||||
process.env.TZ = previousTZ;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public getCurrentTimezone(): string {
|
||||
const configs = [
|
||||
this.getGroupConfiguration(),
|
||||
this.getServerConfiguration()
|
||||
];
|
||||
|
||||
for (const config of configs) {
|
||||
if (!config) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const timezone = config.getConfigurationByPath('timezone');
|
||||
if (timezone.from === PathConfigurationFrom.Default) {
|
||||
continue;
|
||||
}
|
||||
|
||||
return <string>timezone.value;
|
||||
}
|
||||
|
||||
return process.env.TZ ?? "Europe/London";
|
||||
}
|
||||
|
||||
public save(timezone: string, target: TimezoneSaveTarget) {
|
||||
const config = target === TimezoneSaveTarget.Server ? this.getServerConfiguration() : this.getGroupConfiguration();
|
||||
if (!config) {
|
||||
return;
|
||||
}
|
||||
|
||||
config.save('timezone', timezone);
|
||||
}
|
||||
|
||||
private getGroupConfiguration(): Nullable<ConfigurationHandler> {
|
||||
if (!this.group) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return new ConfigurationHandler(
|
||||
new GroupConfigurationProvider(
|
||||
Container.get<GroupConfigurationRepository>(GroupConfigurationRepository.name),
|
||||
this.group
|
||||
)
|
||||
);
|
||||
}
|
||||
private getServerConfiguration(): ConfigurationHandler {
|
||||
return new ConfigurationHandler(
|
||||
new ServerConfigurationProvider(
|
||||
Container.get<ServerConfigurationRepository>(ServerConfigurationRepository.name),
|
||||
this.serverid
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue