From 631afe7ed4dcf765d7aa90be62cc4e4651f9d52b Mon Sep 17 00:00:00 2001 From: Oliver Akins Date: Tue, 9 Aug 2022 19:10:49 -0600 Subject: [PATCH] Add methods to the database handler --- src/utils/database/json.ts | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/utils/database/json.ts b/src/utils/database/json.ts index 03a23b8..9354dd7 100644 --- a/src/utils/database/json.ts +++ b/src/utils/database/json.ts @@ -2,7 +2,7 @@ import { databaseOptions } from "$/types/config"; import fs from "fs"; export class JSONDatabase { - private data = {}; + private data: any = {}; private conf: databaseOptions; constructor(conf: databaseOptions) { @@ -22,4 +22,29 @@ export class JSONDatabase { public shutdown() { fs.writeFileSync(this.conf.uri, JSON.stringify(this.data)); }; + + public channelExists(channel: string) { + return this.data[channel] != null; + }; + + public counterExists(channel: string, counter: string) { + return this.channelExists(channel) && this.data[channel].counters[counter] != null; + }; + + public addChannel(channel: string) { + if (this.channelExists(channel)) { + throw new Error("Channel already exists"); + }; + this.data[channel] = { counters: {} }; + }; + + public changeCount(channel: string, counter: string, delta: number): number { + if (!this.channelExists(channel)) { + throw new Error("Channel not found"); + }; + if (!this.counterExists(channel, counter)) { + throw new Error("Counter doesn't exist on that channel"); + }; + return this.data[channel].counters[counter] += delta; + }; }; \ No newline at end of file