Skip to content

Commit

Permalink
fix: statistic not found when registering the first time a dice
Browse files Browse the repository at this point in the history
  • Loading branch information
Mara-Li committed Dec 1, 2024
1 parent 59946ed commit 498cfa1
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 12 deletions.
3 changes: 0 additions & 3 deletions src/interactions/add/dice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { editUserButtons, registerDmgButton } from "@utils/buttons";
import { getTemplateWithDB, getUserByEmbed, registerUser } from "@utils/db";
import { ensureEmbed, getEmbeds } from "@utils/parse";
import * as Djs from "discord.js";
import { logger } from "../../logger";
/**
* Interaction to add a new skill dice
* @param interaction {Djs.ButtonInteraction}
Expand Down Expand Up @@ -144,7 +143,6 @@ export async function registerDamageDice(
}
}
const user = getUserByEmbed(interaction.message, ul, first);
logger.trace("user", user);
if (!user) throw new Error(ul("error.user")); //mean that there is no embed
value = evalStatsDice(value, user.stats);

Expand Down Expand Up @@ -220,7 +218,6 @@ export async function registerDamageDice(
const userEmbed = getEmbeds(ul, interaction.message ?? undefined, "user");
if (!userEmbed) throw new NoEmbed(); //mean that there is no embed
const statsEmbed = getEmbeds(ul, interaction.message ?? undefined, "stats");
logger.trace("stats", statsEmbed);
const allEmbeds = [userEmbed];
if (statsEmbed) allEmbeds.push(statsEmbed);
allEmbeds.push(diceEmbed);
Expand Down
13 changes: 4 additions & 9 deletions src/utils/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,14 +273,11 @@ export function getUserByEmbed(
if (charNameFields && charNameFields.value !== "common.noSet") {
user.userName = charNameFields.value;
}
const templateStat = first
? userEmbed.toJSON().fields
: getEmbeds(ul, message, "stats")?.toJSON()?.fields;
const statsFields = getEmbeds(ul, message, "stats")?.toJSON()?.fields;
let stats: { [name: string]: number } | undefined = undefined;
if (templateStat) {
if (statsFields) {
stats = {};
for (const stat of templateStat) {
if (first && !stat.name.startsWith("✏")) continue;
for (const stat of statsFields) {
const value = Number.parseInt(stat.value.removeBacktick(), 10);
if (Number.isNaN(value)) {
//it's a combinaison
Expand All @@ -292,9 +289,7 @@ export function getUserByEmbed(
}
}
user.stats = stats;
const damageFields = first
? userEmbed.toJSON().fields
: getEmbeds(ul, message, "damage")?.toJSON()?.fields;
const damageFields = getEmbeds(ul, message, "damage")?.toJSON()?.fields;
let templateDamage: { [name: string]: string } | undefined = undefined;
if (damageFields) {
templateDamage = {};
Expand Down

0 comments on commit 498cfa1

Please sign in to comment.