Add syncing with google drive #75
9 changed files with 85 additions and 0 deletions
3
.dev.env
3
.dev.env
|
@ -29,3 +29,6 @@ DB_LOGGING=true
|
||||||
DB_CARD_FILE=:memory:
|
DB_CARD_FILE=:memory:
|
||||||
|
|
||||||
EXPRESS_PORT=3303
|
EXPRESS_PORT=3303
|
||||||
|
|
||||||
|
GDRIVESYNC_WHITELIST=147392775707426816,887272961504071690
|
||||||
|
GDRIVESYNC_AUTO=true
|
||||||
|
|
|
@ -29,3 +29,6 @@ DB_LOGGING=false
|
||||||
DB_CARD_FILE=:memory:
|
DB_CARD_FILE=:memory:
|
||||||
|
|
||||||
EXPRESS_PORT=3323
|
EXPRESS_PORT=3323
|
||||||
|
|
||||||
|
GDRIVESYNC_WHITELIST=147392775707426816,887272961504071690
|
||||||
|
GDRIVESYNC_AUTO=false
|
||||||
|
|
|
@ -29,3 +29,6 @@ DB_LOGGING=false
|
||||||
DB_CARD_FILE=:memory:
|
DB_CARD_FILE=:memory:
|
||||||
|
|
||||||
EXPRESS_PORT=3313
|
EXPRESS_PORT=3313
|
||||||
|
|
||||||
|
GDRIVESYNC_WHITELIST=147392775707426816,887272961504071690
|
||||||
|
GDRIVESYNC_AUTO=false
|
||||||
|
|
19
src/bot.ts
19
src/bot.ts
|
@ -2,6 +2,8 @@ import * as dotenv from "dotenv";
|
||||||
import { CoreClient } from "./client/client";
|
import { CoreClient } from "./client/client";
|
||||||
import { IntentsBitField } from "discord.js";
|
import { IntentsBitField } from "discord.js";
|
||||||
import Registry from "./registry";
|
import Registry from "./registry";
|
||||||
|
import { existsSync } from "fs";
|
||||||
|
import { ExecException, exec } from "child_process";
|
||||||
|
|
||||||
dotenv.config();
|
dotenv.config();
|
||||||
|
|
||||||
|
@ -19,6 +21,7 @@ const requiredConfigs: string[] = [
|
||||||
"DB_SYNC",
|
"DB_SYNC",
|
||||||
"DB_LOGGING",
|
"DB_LOGGING",
|
||||||
"EXPRESS_PORT",
|
"EXPRESS_PORT",
|
||||||
|
"GDRIVESYNC_WHITELIST",
|
||||||
]
|
]
|
||||||
|
|
||||||
requiredConfigs.forEach(config => {
|
requiredConfigs.forEach(config => {
|
||||||
|
@ -36,4 +39,20 @@ Registry.RegisterCommands();
|
||||||
Registry.RegisterEvents();
|
Registry.RegisterEvents();
|
||||||
Registry.RegisterButtonEvents();
|
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();
|
client.start();
|
|
@ -9,6 +9,11 @@ import Inventory from "../database/entities/app/Inventory";
|
||||||
|
|
||||||
export default class Reroll extends ButtonEvent {
|
export default class Reroll extends ButtonEvent {
|
||||||
public override async execute(interaction: ButtonInteraction) {
|
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;
|
if (!interaction.guild || !interaction.guildId) return;
|
||||||
|
|
||||||
let randomCard = await CardDropHelper.GetRandomCard();
|
let randomCard = await CardDropHelper.GetRandomCard();
|
||||||
|
|
|
@ -26,6 +26,7 @@ export class CoreClient extends Client {
|
||||||
|
|
||||||
public static ClaimId: string;
|
public static ClaimId: string;
|
||||||
public static Environment: Environment;
|
public static Environment: Environment;
|
||||||
|
public static AllowDrops: boolean;
|
||||||
|
|
||||||
public static get commandItems(): ICommandItem[] {
|
public static get commandItems(): ICommandItem[] {
|
||||||
return this._commandItems;
|
return this._commandItems;
|
||||||
|
@ -53,6 +54,8 @@ export class CoreClient extends Client {
|
||||||
|
|
||||||
CoreClient.Environment = Number(process.env.BOT_ENV);
|
CoreClient.Environment = Number(process.env.BOT_ENV);
|
||||||
console.log(`Bot Environment: ${CoreClient.Environment}`);
|
console.log(`Bot Environment: ${CoreClient.Environment}`);
|
||||||
|
|
||||||
|
CoreClient.AllowDrops = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async start() {
|
public async start() {
|
||||||
|
|
|
@ -17,6 +17,11 @@ export default class Drop extends Command {
|
||||||
}
|
}
|
||||||
|
|
||||||
public override async execute(interaction: CommandInteraction) {
|
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();
|
let randomCard = await CardDropHelper.GetRandomCard();
|
||||||
|
|
||||||
if (process.env.DROP_RARITY && Number(process.env.DROP_RARITY) > 0) {
|
if (process.env.DROP_RARITY && Number(process.env.DROP_RARITY) > 0) {
|
||||||
|
|
42
src/commands/gdrivesync.ts
Normal file
42
src/commands/gdrivesync.ts
Normal 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;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
|
@ -3,6 +3,7 @@ import { CoreClient } from "./client/client";
|
||||||
// Global Command Imports
|
// Global Command Imports
|
||||||
import About from "./commands/about";
|
import About from "./commands/about";
|
||||||
import Drop from "./commands/drop";
|
import Drop from "./commands/drop";
|
||||||
|
import Gdrivesync from "./commands/gdrivesync";
|
||||||
|
|
||||||
// Test Command Imports
|
// Test Command Imports
|
||||||
import Dropnumber from "./commands/stage/dropnumber";
|
import Dropnumber from "./commands/stage/dropnumber";
|
||||||
|
@ -17,6 +18,7 @@ export default class Registry {
|
||||||
// Global Commands
|
// Global Commands
|
||||||
CoreClient.RegisterCommand('about', new About());
|
CoreClient.RegisterCommand('about', new About());
|
||||||
CoreClient.RegisterCommand('drop', new Drop());
|
CoreClient.RegisterCommand('drop', new Drop());
|
||||||
|
CoreClient.RegisterCommand('gdrivesync', new Gdrivesync());
|
||||||
|
|
||||||
// Test Commands
|
// Test Commands
|
||||||
CoreClient.RegisterCommand('dropnumber', new Dropnumber(), Environment.Test);
|
CoreClient.RegisterCommand('dropnumber', new Dropnumber(), Environment.Test);
|
||||||
|
|
Loading…
Reference in a new issue