Add syncing with google drive (#75)
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.

- 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 <tester@vylpes.com>
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-10 18:32:08 +00:00 committed by Vylpes
parent 21bef358b3
commit 2462ca105a
9 changed files with 85 additions and 0 deletions

View file

@ -27,3 +27,6 @@ DB_LOGGING=true
DB_CARD_FILE=:memory:
EXPRESS_PORT=3303
GDRIVESYNC_WHITELIST=147392775707426816,887272961504071690
GDRIVESYNC_AUTO=true

View file

@ -27,3 +27,6 @@ DB_LOGGING=false
DB_CARD_FILE=:memory:
EXPRESS_PORT=3323
GDRIVESYNC_WHITELIST=147392775707426816,887272961504071690
GDRIVESYNC_AUTO=false

View file

@ -27,3 +27,6 @@ DB_LOGGING=false
DB_CARD_FILE=:memory:
EXPRESS_PORT=3313
GDRIVESYNC_WHITELIST=147392775707426816,887272961504071690
GDRIVESYNC_AUTO=false

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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