From 2462ca105a54b85f2b21a832d03ecd89fdbf4732 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Fri, 10 Nov 2023 18:32:08 +0000 Subject: [PATCH] Add syncing with google drive (#75) # 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. - Add command (for admins and whitelisted-users only) to sync the google drive folder #40 ## Type of change Please delete options that are not relevant. - [x] New feature (non-breaking change which adds functionality) # 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/75 Reviewed-by: VylpesTester Co-authored-by: Ethan Lane Co-committed-by: Ethan Lane --- .dev.env | 3 +++ .prod.env | 3 +++ .stage.env | 3 +++ src/bot.ts | 19 +++++++++++++++++ src/buttonEvents/Reroll.ts | 5 +++++ src/client/client.ts | 3 +++ src/commands/drop.ts | 5 +++++ src/commands/gdrivesync.ts | 42 ++++++++++++++++++++++++++++++++++++++ src/registry.ts | 2 ++ 9 files changed, 85 insertions(+) create mode 100644 src/commands/gdrivesync.ts diff --git a/.dev.env b/.dev.env index a8ca023..02c46a1 100644 --- a/.dev.env +++ b/.dev.env @@ -27,3 +27,6 @@ DB_LOGGING=true DB_CARD_FILE=:memory: EXPRESS_PORT=3303 + +GDRIVESYNC_WHITELIST=147392775707426816,887272961504071690 +GDRIVESYNC_AUTO=true diff --git a/.prod.env b/.prod.env index 97472be..1f338a2 100644 --- a/.prod.env +++ b/.prod.env @@ -27,3 +27,6 @@ DB_LOGGING=false DB_CARD_FILE=:memory: EXPRESS_PORT=3323 + +GDRIVESYNC_WHITELIST=147392775707426816,887272961504071690 +GDRIVESYNC_AUTO=false diff --git a/.stage.env b/.stage.env index 66c4653..0a63651 100644 --- a/.stage.env +++ b/.stage.env @@ -27,3 +27,6 @@ DB_LOGGING=false DB_CARD_FILE=:memory: EXPRESS_PORT=3313 + +GDRIVESYNC_WHITELIST=147392775707426816,887272961504071690 +GDRIVESYNC_AUTO=false diff --git a/src/bot.ts b/src/bot.ts index 45ae48d..8bddbf4 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -2,6 +2,8 @@ import * as dotenv from "dotenv"; import { CoreClient } from "./client/client"; import { IntentsBitField } from "discord.js"; import Registry from "./registry"; +import { existsSync } from "fs"; +import { ExecException, exec } from "child_process"; dotenv.config(); @@ -19,6 +21,7 @@ const requiredConfigs: string[] = [ "DB_SYNC", "DB_LOGGING", "EXPRESS_PORT", + "GDRIVESYNC_WHITELIST", ] requiredConfigs.forEach(config => { @@ -36,4 +39,20 @@ Registry.RegisterCommands(); Registry.RegisterEvents(); Registry.RegisterButtonEvents(); +if (!existsSync(`${process.cwd()}/cards`) && process.env.GDRIVESYNC_AUTO && process.env.GDRIVESYNC_AUTO == 'true') { + console.log("Card directory not found, syncing..."); + + CoreClient.AllowDrops = false; + + exec(`rclone sync card-drop-gdrive: ${process.cwd()}/cards`, async (error: ExecException | null) => { + if (error) { + console.error(error.code); + throw `Error while running sync command. Code: ${error.code}`; + } else { + console.log('Synced successfully.'); + CoreClient.AllowDrops = true; + } + }); +} + client.start(); \ No newline at end of file diff --git a/src/buttonEvents/Reroll.ts b/src/buttonEvents/Reroll.ts index 0f99267..7dd6d19 100644 --- a/src/buttonEvents/Reroll.ts +++ b/src/buttonEvents/Reroll.ts @@ -9,6 +9,11 @@ import Inventory from "../database/entities/app/Inventory"; export default class Reroll extends ButtonEvent { public override async execute(interaction: ButtonInteraction) { + if (!CoreClient.AllowDrops) { + await interaction.reply('Bot is currently syncing, please wait until its done.'); + return; + } + if (!interaction.guild || !interaction.guildId) return; let randomCard = await CardDropHelper.GetRandomCard(); diff --git a/src/client/client.ts b/src/client/client.ts index 76ef03f..f909d19 100644 --- a/src/client/client.ts +++ b/src/client/client.ts @@ -26,6 +26,7 @@ export class CoreClient extends Client { public static ClaimId: string; public static Environment: Environment; + public static AllowDrops: boolean; public static get commandItems(): ICommandItem[] { return this._commandItems; @@ -53,6 +54,8 @@ export class CoreClient extends Client { CoreClient.Environment = Number(process.env.BOT_ENV); console.log(`Bot Environment: ${CoreClient.Environment}`); + + CoreClient.AllowDrops = true; } public async start() { diff --git a/src/commands/drop.ts b/src/commands/drop.ts index c23f699..11c00f4 100644 --- a/src/commands/drop.ts +++ b/src/commands/drop.ts @@ -17,6 +17,11 @@ export default class Drop extends Command { } public override async execute(interaction: CommandInteraction) { + if (!CoreClient.AllowDrops) { + await interaction.reply('Bot is currently syncing, please wait until its done.'); + return; + } + const randomCard = await CardDropHelper.GetRandomCard(); const image = readFileSync(randomCard.Path); diff --git a/src/commands/gdrivesync.ts b/src/commands/gdrivesync.ts new file mode 100644 index 0000000..5980c48 --- /dev/null +++ b/src/commands/gdrivesync.ts @@ -0,0 +1,42 @@ +import { CacheType, CommandInteraction, PermissionsBitField, SlashCommandBuilder } from "discord.js"; +import { Command } from "../type/command"; +import { ExecException, exec } from "child_process"; +import CardSetupFunction from "../Functions/CardSetupFunction"; +import { CoreClient } from "../client/client"; + +export default class Gdrivesync extends Command { + constructor() { + super(); + + super.CommandBuilder = new SlashCommandBuilder() + .setName('gdrivesync') + .setDescription('Sync google drive to the bot') + .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; + } + + await interaction.reply('Syncing, this might take a while...'); + + CoreClient.AllowDrops = false; + + 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}`); + } else { + await CardSetupFunction.Execute(); + await interaction.editReply('Synced successfully.'); + + CoreClient.AllowDrops = true; + } + }); + } +} \ No newline at end of file diff --git a/src/registry.ts b/src/registry.ts index 05a5485..9619437 100644 --- a/src/registry.ts +++ b/src/registry.ts @@ -3,6 +3,7 @@ import { CoreClient } from "./client/client"; // Global Command Imports import About from "./commands/about"; import Drop from "./commands/drop"; +import Gdrivesync from "./commands/gdrivesync"; // Test Command Imports import Dropnumber from "./commands/stage/dropnumber"; @@ -18,6 +19,7 @@ export default class Registry { // Global Commands CoreClient.RegisterCommand('about', new About()); CoreClient.RegisterCommand('drop', new Drop()); + CoreClient.RegisterCommand('gdrivesync', new Gdrivesync()); // Test Commands CoreClient.RegisterCommand('dropnumber', new Dropnumber(), Environment.Test);