Add safe mode
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Ethan Lane 2023-11-12 18:50:28 +00:00
parent 2462ca105a
commit 038f2cc130
7 changed files with 97 additions and 3 deletions

View file

@ -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;

View file

@ -4,9 +4,12 @@ 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() {
if (await Config.GetValue('safemode') == "true") return;
await this.ClearDatabase();
await this.ReadSeries();
await this.ReadCards();

View file

@ -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();

View file

@ -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);

View file

@ -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');
}
});
}

View file

@ -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<Config | null> {
const repository = AppDataSource.getRepository(Config);
const single = await repository.findOne({ where: { Key: key }});
return single;
}
public static async GetValue(key: string): Promise<string | undefined> {
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);
}
}
}

View file

@ -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<void> {
MigrationHelper.Up('1699814500650-createConfig', '0.2', [
"01-table/Config",
], queryRunner);
}
public async down(queryRunner: QueryRunner): Promise<void> {
}
}