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

# Description

Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change.

#76

## Type of change

Please delete options that are not relevant.

- [x] Bug fix (non-breaking change which fixes an issue)

# How Has This Been Tested?

Please describe the tests that you ran to verify the changes. Provide instructions so we can reproduce. Please also list any relevant details to your test configuration.

# Checklist

- [ ] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my own code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [ ] My changes generate no new warnings
- [ ] I have added tests that provde my fix is effective or that my feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] Any dependent changes have been merged and published in downstream modules

Reviewed-on: https://gitea.vylpes.xyz/External/card-drop/pulls/85
Co-authored-by: Ethan Lane <ethan@vylpes.com>
Co-committed-by: Ethan Lane <ethan@vylpes.com>
This commit is contained in:
Ethan Lane 2023-11-12 20:53:50 +00:00 committed by Vylpes
parent 2462ca105a
commit 4a5f624da3
7 changed files with 104 additions and 6 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,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() {

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> {
}
}