From 278abb3c02c03f1371d4f3f3a94a74b787e36f1b Mon Sep 17 00:00:00 2001 From: Oliver-Akins Date: Fri, 23 Jul 2021 13:27:12 -0600 Subject: [PATCH] Add DB operations for used quotes and history. --- src/utils/data.ts | 50 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/utils/data.ts diff --git a/src/utils/data.ts b/src/utils/data.ts new file mode 100644 index 0000000..380f058 --- /dev/null +++ b/src/utils/data.ts @@ -0,0 +1,50 @@ +import fs from "fs/promises"; + +/** + * Retrieves the historical data for each bracket that was ran for a channel. + * + * @param guild_id The guild ID that we are loading the history of brackets for + */ +export async function loadHistory(guild_id: string): Promise { + try { + return JSON.parse(await fs.readFile(`data/history/${guild_id}.json`, `utf-8`)) + } catch (err) { + return []; + }; +}; + +/** + * Loads all of the quotes used by the system in previous quote brackets to + * prevent duplicates. + * + * @param guild_id The guild ID that we are loading quotes for + * @returns The quotes which have been used + */ +export async function loadUsedQuotes(guild_id: string): Promise { + try { + return JSON.parse(await fs.readFile(`data/used_quotes/${guild_id}.json`, `utf-8`)) + } catch (err) { + return []; + }; +}; + +/** + * Saves the guild's quote bracket history. + * + * @param guild_id The ID of the guild which is being saved. + * @param data The data that we are saving to disk. + */ +export async function saveHistory(guild_id: string, data: any) { + fs.writeFile(`data/history/${guild_id}.json`, JSON.stringify(data)); +}; + +/** + * Saves the data to the guild's Used Quotes file to prevent duplicate quotes from + * being used. + * + * @param guild_id The guild's ID + * @param data The data that we are saving + */ +export async function saveUsedQuotes(guild_id: string, data: any) { + fs.writeFile(`data/used_quotes/${guild_id}.json`, JSON.stringify(data)); +}; \ No newline at end of file