Fix droprarity and dropnumber commands (#431)
All checks were successful
Test / build (push) Successful in 20s
All checks were successful
Test / build (push) Successful in 20s
- Fix the droprarity and dropnumber commands with the changes previously made regarding image urls - Add a `CatchError` function to the app logger to easily send caught errors to the error logs - Add choices to the droprarity command to make them easier to select #429 Reviewed-on: #431 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
ad34cc7b7f
commit
9b34bc65a7
4 changed files with 83 additions and 56 deletions
|
@ -86,4 +86,12 @@ export default class AppLogger {
|
||||||
public static LogSilly(label: string, message: string) {
|
public static LogSilly(label: string, message: string) {
|
||||||
AppLogger.Logger.silly({ label, message });
|
AppLogger.Logger.silly({ label, message });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static CatchError(label: string, error: unknown) {
|
||||||
|
if (error instanceof Error) {
|
||||||
|
AppLogger.Logger.error({ label, message: error.message });
|
||||||
|
} else {
|
||||||
|
AppLogger.Logger.error({ label, message: error });
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { AttachmentBuilder, CacheType, CommandInteraction, DiscordAPIError, SlashCommandBuilder } from "discord.js";
|
import { AttachmentBuilder, CacheType, CommandInteraction, SlashCommandBuilder } from "discord.js";
|
||||||
import { Command } from "../../type/command";
|
import { Command } from "../../type/command";
|
||||||
import { readFileSync } from "fs";
|
import { readFileSync } from "fs";
|
||||||
import Inventory from "../../database/entities/app/Inventory";
|
import Inventory from "../../database/entities/app/Inventory";
|
||||||
|
@ -6,6 +6,7 @@ import { v4 } from "uuid";
|
||||||
import { CoreClient } from "../../client/client";
|
import { CoreClient } from "../../client/client";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
import CardDropHelperMetadata from "../../helpers/CardDropHelperMetadata";
|
import CardDropHelperMetadata from "../../helpers/CardDropHelperMetadata";
|
||||||
|
import AppLogger from "../../client/appLogger";
|
||||||
|
|
||||||
export default class Dropnumber extends Command {
|
export default class Dropnumber extends Command {
|
||||||
constructor() {
|
constructor() {
|
||||||
|
@ -43,43 +44,39 @@ export default class Dropnumber extends Command {
|
||||||
const series = CoreClient.Cards
|
const series = CoreClient.Cards
|
||||||
.find(x => x.cards.includes(card))!;
|
.find(x => x.cards.includes(card))!;
|
||||||
|
|
||||||
const files = [];
|
const claimId = v4();
|
||||||
let imageFileName = "";
|
|
||||||
|
|
||||||
if (!(card.path.startsWith("http://") || card.path.startsWith("https://"))) {
|
|
||||||
const image = readFileSync(path.join(process.env.DATA_DIR!, "cards", card.path));
|
|
||||||
imageFileName = card.path.split("/").pop()!;
|
|
||||||
|
|
||||||
const attachment = new AttachmentBuilder(image, { name: imageFileName });
|
|
||||||
|
|
||||||
files.push(attachment);
|
|
||||||
}
|
|
||||||
|
|
||||||
await interaction.deferReply();
|
await interaction.deferReply();
|
||||||
|
|
||||||
const inventory = await Inventory.FetchOneByCardNumberAndUserId(interaction.user.id, card.id);
|
|
||||||
const quantityClaimed = inventory ? inventory.Quantity : 0;
|
|
||||||
|
|
||||||
const embed = CardDropHelperMetadata.GenerateDropEmbed({ card, series }, quantityClaimed, imageFileName);
|
|
||||||
|
|
||||||
const claimId = v4();
|
|
||||||
|
|
||||||
const row = CardDropHelperMetadata.GenerateDropButtons({ card, series }, claimId, interaction.user.id);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
const files = [];
|
||||||
|
let imageFileName = "";
|
||||||
|
|
||||||
|
if (!(card.path.startsWith("http://") || card.path.startsWith("https://"))) {
|
||||||
|
const image = readFileSync(path.join(process.env.DATA_DIR!, "cards", card.path));
|
||||||
|
imageFileName = card.path.split("/").pop()!;
|
||||||
|
|
||||||
|
const attachment = new AttachmentBuilder(image, { name: imageFileName });
|
||||||
|
|
||||||
|
files.push(attachment);
|
||||||
|
}
|
||||||
|
|
||||||
|
const inventory = await Inventory.FetchOneByCardNumberAndUserId(interaction.user.id, card.id);
|
||||||
|
const quantityClaimed = inventory ? inventory.Quantity : 0;
|
||||||
|
|
||||||
|
const embed = CardDropHelperMetadata.GenerateDropEmbed({ card, series }, quantityClaimed, imageFileName);
|
||||||
|
|
||||||
|
const row = CardDropHelperMetadata.GenerateDropButtons({ card, series }, claimId, interaction.user.id);
|
||||||
|
|
||||||
await interaction.editReply({
|
await interaction.editReply({
|
||||||
embeds: [ embed ],
|
embeds: [ embed ],
|
||||||
files: files,
|
files: files,
|
||||||
components: [ row ],
|
components: [ row ],
|
||||||
});
|
});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e);
|
AppLogger.CatchError("Dropnumber", e);
|
||||||
|
await interaction.editReply("Unable to send next drop. Please try again, and report this if it keeps happening");
|
||||||
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");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
CoreClient.ClaimId = claimId;
|
CoreClient.ClaimId = claimId;
|
||||||
|
|
|
@ -1,12 +1,13 @@
|
||||||
import { AttachmentBuilder, CacheType, CommandInteraction, DiscordAPIError, SlashCommandBuilder } from "discord.js";
|
import { AttachmentBuilder, CacheType, CommandInteraction, SlashCommandBuilder } from "discord.js";
|
||||||
import { Command } from "../../type/command";
|
import { Command } from "../../type/command";
|
||||||
import { CardRarity, CardRarityParse } from "../../constants/CardRarity";
|
import { CardRarity, CardRarityChoices, CardRarityParse } from "../../constants/CardRarity";
|
||||||
import { readFileSync } from "fs";
|
import { readFileSync } from "fs";
|
||||||
import Inventory from "../../database/entities/app/Inventory";
|
import Inventory from "../../database/entities/app/Inventory";
|
||||||
import { v4 } from "uuid";
|
import { v4 } from "uuid";
|
||||||
import { CoreClient } from "../../client/client";
|
import { CoreClient } from "../../client/client";
|
||||||
import CardDropHelperMetadata from "../../helpers/CardDropHelperMetadata";
|
import CardDropHelperMetadata from "../../helpers/CardDropHelperMetadata";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
|
import AppLogger from "../../client/appLogger";
|
||||||
|
|
||||||
export default class Droprarity extends Command {
|
export default class Droprarity extends Command {
|
||||||
constructor() {
|
constructor() {
|
||||||
|
@ -19,7 +20,8 @@ export default class Droprarity extends Command {
|
||||||
x
|
x
|
||||||
.setName("rarity")
|
.setName("rarity")
|
||||||
.setDescription("The rarity you want to summon")
|
.setDescription("The rarity you want to summon")
|
||||||
.setRequired(true));
|
.setRequired(true)
|
||||||
|
.setChoices(CardRarityChoices));
|
||||||
}
|
}
|
||||||
|
|
||||||
public override async execute(interaction: CommandInteraction<CacheType>) {
|
public override async execute(interaction: CommandInteraction<CacheType>) {
|
||||||
|
@ -39,48 +41,45 @@ export default class Droprarity extends Command {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const card = await CardDropHelperMetadata.GetRandomCardByRarity(rarityType);
|
const card = CardDropHelperMetadata.GetRandomCardByRarity(rarityType);
|
||||||
|
|
||||||
if (!card) {
|
if (!card) {
|
||||||
await interaction.reply("Card not found");
|
await interaction.reply("Card not found");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const files = [];
|
|
||||||
let imageFileName = "";
|
|
||||||
|
|
||||||
if (!(card.card.path.startsWith("http://") || card.card.path.startsWith("https://"))) {
|
|
||||||
const image = readFileSync(path.join(process.env.DATA_DIR!, "cards", card.card.path));
|
|
||||||
imageFileName = card.card.path.split("/").pop()!;
|
|
||||||
|
|
||||||
const attachment = new AttachmentBuilder(image, { name: imageFileName });
|
|
||||||
|
|
||||||
files.push(attachment);
|
|
||||||
}
|
|
||||||
|
|
||||||
const inventory = await Inventory.FetchOneByCardNumberAndUserId(interaction.user.id, card.card.id);
|
|
||||||
const quantityClaimed = inventory ? inventory.Quantity : 0;
|
|
||||||
|
|
||||||
const embed = CardDropHelperMetadata.GenerateDropEmbed(card, quantityClaimed, imageFileName);
|
|
||||||
|
|
||||||
const claimId = v4();
|
const claimId = v4();
|
||||||
|
|
||||||
const row = CardDropHelperMetadata.GenerateDropButtons(card, claimId, interaction.user.id);
|
await interaction.deferReply();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
const files = [];
|
||||||
|
let imageFileName = "";
|
||||||
|
|
||||||
|
if (!(card.card.path.startsWith("http://") || card.card.path.startsWith("https://"))) {
|
||||||
|
const image = readFileSync(path.join(process.env.DATA_DIR!, "cards", card.card.path));
|
||||||
|
imageFileName = card.card.path.split("/").pop()!;
|
||||||
|
|
||||||
|
const attachment = new AttachmentBuilder(image, { name: imageFileName });
|
||||||
|
|
||||||
|
files.push(attachment);
|
||||||
|
}
|
||||||
|
|
||||||
|
const inventory = await Inventory.FetchOneByCardNumberAndUserId(interaction.user.id, card.card.id);
|
||||||
|
const quantityClaimed = inventory ? inventory.Quantity : 0;
|
||||||
|
|
||||||
|
const embed = CardDropHelperMetadata.GenerateDropEmbed(card, quantityClaimed, imageFileName);
|
||||||
|
|
||||||
|
const row = CardDropHelperMetadata.GenerateDropButtons(card, claimId, interaction.user.id);
|
||||||
|
|
||||||
await interaction.editReply({
|
await interaction.editReply({
|
||||||
embeds: [ embed ],
|
embeds: [ embed ],
|
||||||
files: files,
|
files: files,
|
||||||
components: [ row ],
|
components: [ row ],
|
||||||
});
|
});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e);
|
AppLogger.CatchError("Droprarity", e);
|
||||||
|
await interaction.editReply("Unable to send next drop. Please try again, and report this if it keeps happening");
|
||||||
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");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
CoreClient.ClaimId = claimId;
|
CoreClient.ClaimId = claimId;
|
||||||
|
|
|
@ -9,6 +9,29 @@ export enum CardRarity {
|
||||||
Legendary,
|
Legendary,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const CardRarityChoices = [
|
||||||
|
{
|
||||||
|
name: "Bronze",
|
||||||
|
value: "bronze",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Silver",
|
||||||
|
value: "silver",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Gold",
|
||||||
|
value: "gold",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Manga",
|
||||||
|
value: "manga",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Legendary",
|
||||||
|
value: "legendary",
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
export function CardRarityToString(rarity: CardRarity): string {
|
export function CardRarityToString(rarity: CardRarity): string {
|
||||||
switch (rarity) {
|
switch (rarity) {
|
||||||
case CardRarity.Unknown:
|
case CardRarity.Unknown:
|
||||||
|
|
Loading…
Add table
Reference in a new issue