Update card database to use JSON files (#107)
All checks were successful
continuous-integration/drone/push Build is passing
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. - Updated the card database to use JSON files instead of directly assuming the file system into a SQLITE database - Removed sqlite3 package #27 ## Type of change Please delete options that are not relevant. - [x] New feature (non-breaking change which adds functionality) - [x] Breaking change (fix or feature that would cause existing functionality to not work as expected) # 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. - This has been tested locally using my dev bot - I have tested using an up-to-date copy of the google drive data # Checklist - [x] My code follows the style guidelines of this project - [x] 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 - [x] 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/107 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:
parent
2091e326b8
commit
be7a7c4ca4
19 changed files with 575 additions and 363 deletions
|
@ -1,11 +1,12 @@
|
|||
import { AttachmentBuilder, CommandInteraction, DiscordAPIError, SlashCommandBuilder } from "discord.js";
|
||||
import { Command } from "../type/command";
|
||||
import CardDropHelper from "../helpers/CardDropHelper";
|
||||
import { readFileSync } from "fs";
|
||||
import { CoreClient } from "../client/client";
|
||||
import { v4 } from "uuid";
|
||||
import Inventory from "../database/entities/app/Inventory";
|
||||
import Config from "../database/entities/app/Config";
|
||||
import CardDropHelperMetadata from "../helpers/CardDropHelperMetadata";
|
||||
import path from "path";
|
||||
|
||||
export default class Drop extends Command {
|
||||
constructor() {
|
||||
|
@ -22,28 +23,40 @@ export default class Drop extends Command {
|
|||
return;
|
||||
}
|
||||
|
||||
if (await Config.GetValue('safemode') == "true")
|
||||
{
|
||||
await interaction.reply('Safe Mode has been activated, please resync to contunue.');
|
||||
if (await Config.GetValue('safemode') == "true") {
|
||||
await interaction.reply('Safe Mode has been activated, please resync to continue.');
|
||||
return;
|
||||
}
|
||||
|
||||
const randomCard = await CardDropHelper.GetRandomCard();
|
||||
const randomCard = CardDropHelperMetadata.GetRandomCard();
|
||||
|
||||
const image = readFileSync(randomCard.Path);
|
||||
if (!randomCard) {
|
||||
await interaction.reply('Unable to fetch card, please try again.');
|
||||
return;
|
||||
}
|
||||
|
||||
let image: Buffer;
|
||||
const imageFileName = randomCard.card.path.split("/").pop()!;
|
||||
|
||||
try {
|
||||
image = readFileSync(path.join(process.cwd(), 'cards', randomCard.card.path));
|
||||
} catch {
|
||||
await interaction.reply(`Unable to fetch image for card ${randomCard.card.id}`);
|
||||
return;
|
||||
}
|
||||
|
||||
await interaction.deferReply();
|
||||
|
||||
const attachment = new AttachmentBuilder(image, { name: randomCard.FileName });
|
||||
const attachment = new AttachmentBuilder(image, { name: imageFileName });
|
||||
|
||||
const inventory = await Inventory.FetchOneByCardNumberAndUserId(interaction.user.id, randomCard.CardNumber);
|
||||
const inventory = await Inventory.FetchOneByCardNumberAndUserId(interaction.user.id, randomCard.card.id);
|
||||
const quantityClaimed = inventory ? inventory.Quantity : 0;
|
||||
|
||||
const embed = CardDropHelper.GenerateDropEmbed(randomCard, quantityClaimed || 0);
|
||||
const embed = CardDropHelperMetadata.GenerateDropEmbed(randomCard, quantityClaimed, imageFileName);
|
||||
|
||||
const claimId = v4();
|
||||
|
||||
const row = CardDropHelper.GenerateDropButtons(randomCard, claimId, interaction.user.id);
|
||||
const row = CardDropHelperMetadata.GenerateDropButtons(randomCard, claimId, interaction.user.id);
|
||||
|
||||
try {
|
||||
await interaction.editReply({
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
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";
|
||||
import Config from "../database/entities/app/Config";
|
||||
import CardMetadataFunction from "../Functions/CardMetadataFunction";
|
||||
|
||||
export default class Gdrivesync extends Command {
|
||||
constructor() {
|
||||
|
@ -34,7 +34,8 @@ export default class Gdrivesync extends Command {
|
|||
await interaction.editReply(`Error while running sync command. Safe Mode has been activated. Code: ${error.code}`);
|
||||
await Config.SetValue('safemode', 'true');
|
||||
} else {
|
||||
await CardSetupFunction.Execute();
|
||||
await CardMetadataFunction.Execute();
|
||||
|
||||
await interaction.editReply('Synced successfully.');
|
||||
|
||||
CoreClient.AllowDrops = true;
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
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";
|
||||
import CardMetadataFunction from "../Functions/CardMetadataFunction";
|
||||
|
||||
export default class Resync extends Command {
|
||||
constructor() {
|
||||
|
@ -23,7 +23,9 @@ export default class Resync extends Command {
|
|||
return;
|
||||
}
|
||||
|
||||
if (await CardSetupFunction.Execute()) {
|
||||
let result = await CardMetadataFunction.Execute(true);
|
||||
|
||||
if (result) {
|
||||
if (await Config.GetValue('safemode') == "true") {
|
||||
await Config.SetValue('safemode', 'false');
|
||||
await interaction.reply("Resynced database and disabled safe mode.");
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
import { AttachmentBuilder, CacheType, CommandInteraction, DiscordAPIError, SlashCommandBuilder } from "discord.js";
|
||||
import { Command } from "../../type/command";
|
||||
import Card from "../../database/entities/card/Card";
|
||||
import { readFileSync } from "fs";
|
||||
import Inventory from "../../database/entities/app/Inventory";
|
||||
import CardDropHelper from "../../helpers/CardDropHelper";
|
||||
import { v4 } from "uuid";
|
||||
import { CoreClient } from "../../client/client";
|
||||
import path from "path";
|
||||
import CardDropHelperMetadata from "../../helpers/CardDropHelperMetadata";
|
||||
|
||||
export default class Dropnumber extends Command {
|
||||
constructor() {
|
||||
|
@ -31,29 +31,42 @@ export default class Dropnumber extends Command {
|
|||
return;
|
||||
}
|
||||
|
||||
const card = await Card.FetchOneByCardNumber(cardNumber.value.toString(), [
|
||||
"Series"
|
||||
]);
|
||||
const series = CoreClient.Cards.find(x => x.cards.find(y => y.id == cardNumber.toString()));
|
||||
|
||||
if (!series) {
|
||||
await interaction.reply('Card not found');
|
||||
return;
|
||||
}
|
||||
|
||||
const card = series.cards.find(x => x.id == cardNumber.toString());
|
||||
|
||||
if (!card) {
|
||||
await interaction.reply('Card not found');
|
||||
return;
|
||||
}
|
||||
|
||||
const image = readFileSync(card.Path);
|
||||
let image: Buffer;
|
||||
const imageFileName = card.path.split("/").pop()!;
|
||||
|
||||
try {
|
||||
image = readFileSync(path.join(process.cwd(), 'cards', card.path));
|
||||
} catch {
|
||||
await interaction.reply(`Unable to fetch image for card ${card.id}`);
|
||||
return;
|
||||
}
|
||||
|
||||
await interaction.deferReply();
|
||||
|
||||
const attachment = new AttachmentBuilder(image, { name: card.FileName });
|
||||
const attachment = new AttachmentBuilder(image, { name: imageFileName });
|
||||
|
||||
const inventory = await Inventory.FetchOneByCardNumberAndUserId(interaction.user.id, card.CardNumber);
|
||||
const inventory = await Inventory.FetchOneByCardNumberAndUserId(interaction.user.id, card.id);
|
||||
const quantityClaimed = inventory ? inventory.Quantity : 0;
|
||||
|
||||
const embed = CardDropHelper.GenerateDropEmbed(card, quantityClaimed || 0);
|
||||
const embed = CardDropHelperMetadata.GenerateDropEmbed({ card, series }, quantityClaimed, imageFileName);
|
||||
|
||||
const claimId = v4();
|
||||
|
||||
const row = CardDropHelper.GenerateDropButtons(card, claimId, interaction.user.id);
|
||||
const row = CardDropHelperMetadata.GenerateDropButtons({ card, series }, claimId, interaction.user.id);
|
||||
|
||||
try {
|
||||
await interaction.editReply({
|
||||
|
@ -67,7 +80,7 @@ export default class Dropnumber extends Command {
|
|||
if (e instanceof DiscordAPIError) {
|
||||
await interaction.editReply(`Unable to send next drop. Please try again, and report this if it keeps happening. Code: ${e.code}`);
|
||||
} else {
|
||||
await interaction.editReply(`Unable to send next drop. Please try again, and report this if it keeps happening.Code: UNKNOWN`);
|
||||
await interaction.editReply(`Unable to send next drop. Please try again, and report this if it keeps happening. Code: UNKNOWN`);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,11 +1,12 @@
|
|||
import { AttachmentBuilder, CacheType, CommandInteraction, DiscordAPIError, SlashCommandBuilder } from "discord.js";
|
||||
import { Command } from "../../type/command";
|
||||
import { CardRarity, CardRarityParse } from "../../constants/CardRarity";
|
||||
import CardDropHelper from "../../helpers/CardDropHelper";
|
||||
import { readFileSync } from "fs";
|
||||
import Inventory from "../../database/entities/app/Inventory";
|
||||
import { v4 } from "uuid";
|
||||
import { CoreClient } from "../../client/client";
|
||||
import CardDropHelperMetadata from "../../helpers/CardDropHelperMetadata";
|
||||
import path from "path";
|
||||
|
||||
export default class Droprarity extends Command {
|
||||
constructor() {
|
||||
|
@ -38,27 +39,35 @@ export default class Droprarity extends Command {
|
|||
return;
|
||||
}
|
||||
|
||||
const card = await CardDropHelper.GetRandomCardByRarity(rarityType);
|
||||
const card = await CardDropHelperMetadata.GetRandomCardByRarity(rarityType);
|
||||
|
||||
if (!card) {
|
||||
await interaction.reply('Card not found');
|
||||
return;
|
||||
}
|
||||
|
||||
const image = readFileSync(card.Path);
|
||||
let image: Buffer;
|
||||
const imageFileName = card.card.path.split("/").pop()!;
|
||||
|
||||
try {
|
||||
image = readFileSync(path.join(process.cwd(), 'cards', card.card.path));
|
||||
} catch {
|
||||
await interaction.reply(`Unable to fetch image for card ${card.card.id}`);
|
||||
return;
|
||||
}
|
||||
|
||||
await interaction.deferReply();
|
||||
|
||||
const attachment = new AttachmentBuilder(image, { name: card.FileName });
|
||||
const attachment = new AttachmentBuilder(image, { name: imageFileName });
|
||||
|
||||
const inventory = await Inventory.FetchOneByCardNumberAndUserId(interaction.user.id, card.CardNumber);
|
||||
const inventory = await Inventory.FetchOneByCardNumberAndUserId(interaction.user.id, card.card.id);
|
||||
const quantityClaimed = inventory ? inventory.Quantity : 0;
|
||||
|
||||
const embed = CardDropHelper.GenerateDropEmbed(card, quantityClaimed || 0);
|
||||
const embed = CardDropHelperMetadata.GenerateDropEmbed(card, quantityClaimed, imageFileName);
|
||||
|
||||
const claimId = v4();
|
||||
|
||||
const row = CardDropHelper.GenerateDropButtons(card, claimId, interaction.user.id);
|
||||
const row = CardDropHelperMetadata.GenerateDropButtons(card, claimId, interaction.user.id);
|
||||
|
||||
try {
|
||||
await interaction.editReply({
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue