Add safe mode #85
7 changed files with 97 additions and 3 deletions
|
@ -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;
|
|
@ -4,9 +4,12 @@ import Card from "../database/entities/card/Card";
|
||||||
import Series from "../database/entities/card/Series";
|
import Series from "../database/entities/card/Series";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
import { CardRarity, CardRarityToString } from "../constants/CardRarity";
|
import { CardRarity, CardRarityToString } from "../constants/CardRarity";
|
||||||
|
import Config from "../database/entities/app/Config";
|
||||||
|
|
||||||
export default class CardSetupFunction {
|
export default class CardSetupFunction {
|
||||||
public static async Execute() {
|
public static async Execute() {
|
||||||
|
if (await Config.GetValue('safemode') == "true") return;
|
||||||
|
|
||||||
await this.ClearDatabase();
|
await this.ClearDatabase();
|
||||||
await this.ReadSeries();
|
await this.ReadSeries();
|
||||||
await this.ReadCards();
|
await this.ReadCards();
|
||||||
|
|
|
@ -4,8 +4,8 @@ import CardDropHelper from "../helpers/CardDropHelper";
|
||||||
import { readFileSync } from "fs";
|
import { readFileSync } from "fs";
|
||||||
import { v4 } from "uuid";
|
import { v4 } from "uuid";
|
||||||
import { CoreClient } from "../client/client";
|
import { CoreClient } from "../client/client";
|
||||||
import Card from "../database/entities/card/Card";
|
|
||||||
import Inventory from "../database/entities/app/Inventory";
|
import Inventory from "../database/entities/app/Inventory";
|
||||||
|
import Config from "../database/entities/app/Config";
|
||||||
|
|
||||||
export default class Reroll extends ButtonEvent {
|
export default class Reroll extends ButtonEvent {
|
||||||
public override async execute(interaction: ButtonInteraction) {
|
public override async execute(interaction: ButtonInteraction) {
|
||||||
|
@ -14,6 +14,12 @@ export default class Reroll extends ButtonEvent {
|
||||||
return;
|
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;
|
if (!interaction.guild || !interaction.guildId) return;
|
||||||
|
|
||||||
let randomCard = await CardDropHelper.GetRandomCard();
|
let randomCard = await CardDropHelper.GetRandomCard();
|
||||||
|
|
|
@ -4,8 +4,8 @@ import CardDropHelper from "../helpers/CardDropHelper";
|
||||||
import { readFileSync } from "fs";
|
import { readFileSync } from "fs";
|
||||||
import { CoreClient } from "../client/client";
|
import { CoreClient } from "../client/client";
|
||||||
import { v4 } from "uuid";
|
import { v4 } from "uuid";
|
||||||
import Card from "../database/entities/card/Card";
|
|
||||||
import Inventory from "../database/entities/app/Inventory";
|
import Inventory from "../database/entities/app/Inventory";
|
||||||
|
import Config from "../database/entities/app/Config";
|
||||||
|
|
||||||
export default class Drop extends Command {
|
export default class Drop extends Command {
|
||||||
constructor() {
|
constructor() {
|
||||||
|
@ -22,6 +22,12 @@ export default class Drop extends Command {
|
||||||
return;
|
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 randomCard = await CardDropHelper.GetRandomCard();
|
||||||
|
|
||||||
const image = readFileSync(randomCard.Path);
|
const image = readFileSync(randomCard.Path);
|
||||||
|
|
|
@ -3,6 +3,7 @@ import { Command } from "../type/command";
|
||||||
import { ExecException, exec } from "child_process";
|
import { ExecException, exec } from "child_process";
|
||||||
import CardSetupFunction from "../Functions/CardSetupFunction";
|
import CardSetupFunction from "../Functions/CardSetupFunction";
|
||||||
import { CoreClient } from "../client/client";
|
import { CoreClient } from "../client/client";
|
||||||
|
import Config from "../database/entities/app/Config";
|
||||||
|
|
||||||
export default class Gdrivesync extends Command {
|
export default class Gdrivesync extends Command {
|
||||||
constructor() {
|
constructor() {
|
||||||
|
@ -30,12 +31,14 @@ export default class Gdrivesync extends Command {
|
||||||
|
|
||||||
exec(`rclone sync card-drop-gdrive: ${process.cwd()}/cards`, async (error: ExecException | null) => {
|
exec(`rclone sync card-drop-gdrive: ${process.cwd()}/cards`, async (error: ExecException | null) => {
|
||||||
if (error) {
|
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 {
|
} else {
|
||||||
await CardSetupFunction.Execute();
|
await CardSetupFunction.Execute();
|
||||||
await interaction.editReply('Synced successfully.');
|
await interaction.editReply('Synced successfully.');
|
||||||
|
|
||||||
CoreClient.AllowDrops = true;
|
CoreClient.AllowDrops = true;
|
||||||
|
await Config.SetValue('safemode', 'false');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
53
src/database/entities/app/Config.ts
Normal file
53
src/database/entities/app/Config.ts
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -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> {
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in a new issue