From 0d6005044c6f8f5bd810df3e219f2c918565825d Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Thu, 23 Nov 2023 17:28:15 +0000 Subject: [PATCH] Add resync commmand --- src/Functions/CardSetupFunction.ts | 7 ++++-- src/commands/resync.ts | 38 ++++++++++++++++++++++++++++++ src/registry.ts | 2 ++ 3 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 src/commands/resync.ts diff --git a/src/Functions/CardSetupFunction.ts b/src/Functions/CardSetupFunction.ts index 037b87b..ea8c2c6 100644 --- a/src/Functions/CardSetupFunction.ts +++ b/src/Functions/CardSetupFunction.ts @@ -7,8 +7,8 @@ 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; + public static async Execute(): Promise { + if (await Config.GetValue('safemode') == "true") return false; try { await this.ClearDatabase(); @@ -16,7 +16,10 @@ export default class CardSetupFunction { await this.ReadCards(); } catch { await Config.SetValue('safemode', 'true'); + return false; } + + return true; } private static async ClearDatabase() { diff --git a/src/commands/resync.ts b/src/commands/resync.ts new file mode 100644 index 0000000..a3ea823 --- /dev/null +++ b/src/commands/resync.ts @@ -0,0 +1,38 @@ +import { CacheType, CommandInteraction, PermissionsBitField, SlashCommandBuilder } from "discord.js"; +import { Command } from "../type/command"; +import CardSetupFunction from "../Functions/CardSetupFunction"; +import Config from "../database/entities/app/Config"; + +export default class Resync extends Command { + constructor() { + super(); + + super.CommandBuilder = new SlashCommandBuilder() + .setName('resync') + .setDescription('Resync the card database') + .setDefaultMemberPermissions(PermissionsBitField.Flags.Administrator); + } + + public override async execute(interaction: CommandInteraction) { + if (!interaction.isChatInputCommand()) return; + + const whitelistedUsers = process.env.GDRIVESYNC_WHITELIST!.split(','); + + if (!whitelistedUsers.find(x => x == interaction.user.id)) { + await interaction.reply("Only whitelisted users can use this command."); + return; + } + + if (await CardSetupFunction.Execute()) { + if (await Config.GetValue('safemode') == "true") { + await Config.SetValue('safemode', 'false'); + await interaction.reply("Resynced database and disabled safe mode."); + + return; + } + await interaction.reply("Resynced database."); + } else { + await interaction.reply("Resync failed, safe mode has been activated until successful resync."); + } + } +} \ No newline at end of file diff --git a/src/registry.ts b/src/registry.ts index 9619437..e602198 100644 --- a/src/registry.ts +++ b/src/registry.ts @@ -4,6 +4,7 @@ import { CoreClient } from "./client/client"; import About from "./commands/about"; import Drop from "./commands/drop"; import Gdrivesync from "./commands/gdrivesync"; +import Resync from "./commands/resync"; // Test Command Imports import Dropnumber from "./commands/stage/dropnumber"; @@ -20,6 +21,7 @@ export default class Registry { CoreClient.RegisterCommand('about', new About()); CoreClient.RegisterCommand('drop', new Drop()); CoreClient.RegisterCommand('gdrivesync', new Gdrivesync()); + CoreClient.RegisterCommand('resync', new Resync()); // Test Commands CoreClient.RegisterCommand('dropnumber', new Dropnumber(), Environment.Test);