Add syncing with google drive #75

Merged
Vylpes merged 2 commits from feature/40-stage-force-gdrive into develop 2023-11-10 18:32:09 +00:00
9 changed files with 85 additions and 0 deletions
Showing only changes of commit 05d307ad29 - Show all commits

View file

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

View file

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

View file

@ -29,3 +29,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;
}
let randomCard = await CardDropHelper.GetRandomCard();
if (process.env.DROP_RARITY && Number(process.env.DROP_RARITY) > 0) {

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";
@ -17,6 +18,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);