diff --git a/database/0.2/1699814500650-createConfig/Up/01-table/Config.sql b/database/0.2/1699814500650-createConfig/Up/01-table/Config.sql new file mode 100644 index 0000000..dbc4ec7 --- /dev/null +++ b/database/0.2/1699814500650-createConfig/Up/01-table/Config.sql @@ -0,0 +1,8 @@ +CREATE TABLE `config` ( + `Id` VARCHAR(255) NOT NULL, + `WhenCreated` DATETIME NOT NULL, + `WhenUpdated` DATETIME NOT NULL, + `Key` VARCHAR(255) NOT NULL, + `Value` VARCHAR(255) NOT NULL, + PRIMARY KEY (`Id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; \ No newline at end of file diff --git a/src/Functions/CardSetupFunction.ts b/src/Functions/CardSetupFunction.ts index 93892cf..037b87b 100644 --- a/src/Functions/CardSetupFunction.ts +++ b/src/Functions/CardSetupFunction.ts @@ -4,12 +4,19 @@ import Card from "../database/entities/card/Card"; import Series from "../database/entities/card/Series"; import path from "path"; import { CardRarity, CardRarityToString } from "../constants/CardRarity"; +import Config from "../database/entities/app/Config"; export default class CardSetupFunction { public static async Execute() { - await this.ClearDatabase(); - await this.ReadSeries(); - await this.ReadCards(); + if (await Config.GetValue('safemode') == "true") return; + + try { + await this.ClearDatabase(); + await this.ReadSeries(); + await this.ReadCards(); + } catch { + await Config.SetValue('safemode', 'true'); + } } private static async ClearDatabase() { diff --git a/src/buttonEvents/Reroll.ts b/src/buttonEvents/Reroll.ts index 7dd6d19..38cae08 100644 --- a/src/buttonEvents/Reroll.ts +++ b/src/buttonEvents/Reroll.ts @@ -4,8 +4,8 @@ import CardDropHelper from "../helpers/CardDropHelper"; import { readFileSync } from "fs"; import { v4 } from "uuid"; import { CoreClient } from "../client/client"; -import Card from "../database/entities/card/Card"; import Inventory from "../database/entities/app/Inventory"; +import Config from "../database/entities/app/Config"; export default class Reroll extends ButtonEvent { public override async execute(interaction: ButtonInteraction) { @@ -14,6 +14,12 @@ export default class Reroll extends ButtonEvent { return; } + if (await Config.GetValue('safemode') == "true") + { + await interaction.reply('Safe Mode has been activated, please resync to contunue.'); + return; + } + if (!interaction.guild || !interaction.guildId) return; let randomCard = await CardDropHelper.GetRandomCard(); diff --git a/src/commands/drop.ts b/src/commands/drop.ts index 11c00f4..bebe581 100644 --- a/src/commands/drop.ts +++ b/src/commands/drop.ts @@ -4,8 +4,8 @@ import CardDropHelper from "../helpers/CardDropHelper"; import { readFileSync } from "fs"; import { CoreClient } from "../client/client"; import { v4 } from "uuid"; -import Card from "../database/entities/card/Card"; import Inventory from "../database/entities/app/Inventory"; +import Config from "../database/entities/app/Config"; export default class Drop extends Command { constructor() { @@ -22,6 +22,12 @@ export default class Drop extends Command { return; } + if (await Config.GetValue('safemode') == "true") + { + await interaction.reply('Safe Mode has been activated, please resync to contunue.'); + return; + } + const randomCard = await CardDropHelper.GetRandomCard(); const image = readFileSync(randomCard.Path); diff --git a/src/commands/gdrivesync.ts b/src/commands/gdrivesync.ts index 5980c48..97009a2 100644 --- a/src/commands/gdrivesync.ts +++ b/src/commands/gdrivesync.ts @@ -3,6 +3,7 @@ import { Command } from "../type/command"; import { ExecException, exec } from "child_process"; import CardSetupFunction from "../Functions/CardSetupFunction"; import { CoreClient } from "../client/client"; +import Config from "../database/entities/app/Config"; export default class Gdrivesync extends Command { constructor() { @@ -30,12 +31,14 @@ export default class Gdrivesync extends Command { exec(`rclone sync card-drop-gdrive: ${process.cwd()}/cards`, async (error: ExecException | null) => { if (error) { - await interaction.editReply(`Error while running sync command. Code: ${error.code}`); + await interaction.editReply(`Error while running sync command. Safe Mode has been activated. Code: ${error.code}`); + await Config.SetValue('safemode', 'true'); } else { await CardSetupFunction.Execute(); await interaction.editReply('Synced successfully.'); CoreClient.AllowDrops = true; + await Config.SetValue('safemode', 'false'); } }); } diff --git a/src/database/entities/app/Config.ts b/src/database/entities/app/Config.ts new file mode 100644 index 0000000..50915d0 --- /dev/null +++ b/src/database/entities/app/Config.ts @@ -0,0 +1,53 @@ +import { Column, Entity } from "typeorm"; +import AppBaseEntity from "../../../contracts/AppBaseEntity"; +import AppDataSource from "../../dataSources/appDataSource"; + +@Entity() +export default class Config extends AppBaseEntity { + constructor(key: string, value: string) { + super(); + + this.Key = key; + this.Value = value; + } + + @Column() + Key: string; + + @Column() + Value: string; + + public SetValue(value: string) { + this.Value = value; + } + + public static async FetchOneByKey(key: string): Promise { + const repository = AppDataSource.getRepository(Config); + + const single = await repository.findOne({ where: { Key: key }}); + + return single; + } + + public static async GetValue(key: string): Promise { + const config = await Config.FetchOneByKey(key); + + if (!config) return undefined; + + return config.Value; + } + + public static async SetValue(key: string, value: string) { + const config = await Config.FetchOneByKey(key); + + if (!config) { + const newConfig = new Config(key, value); + + await newConfig.Save(Config, newConfig); + } else { + config.SetValue(value); + + await config.Save(Config, config); + } + } +} \ No newline at end of file diff --git a/src/database/migrations/app/0.2/1699814500650-createConfig.ts b/src/database/migrations/app/0.2/1699814500650-createConfig.ts new file mode 100644 index 0000000..0a1f22e --- /dev/null +++ b/src/database/migrations/app/0.2/1699814500650-createConfig.ts @@ -0,0 +1,15 @@ +import { MigrationInterface, QueryRunner } from "typeorm" +import MigrationHelper from "../../../../helpers/MigrationHelper" + +export class CreateConfig1699814500650 implements MigrationInterface { + + public async up(queryRunner: QueryRunner): Promise { + MigrationHelper.Up('1699814500650-createConfig', '0.2', [ + "01-table/Config", + ], queryRunner); + } + + public async down(queryRunner: QueryRunner): Promise { + } + +}