From 8905cb05bc365397cfde2c22dbad20ecce474b04 Mon Sep 17 00:00:00 2001 From: Oliver-Akins Date: Fri, 13 Jun 2025 19:28:08 -0600 Subject: [PATCH] Update the message listening to use createChatMessage instead of preCreateChatMessage --- module/hooks/createChatMessage.mjs | 41 +++++++++++++++++++++++++++ module/hooks/preCreateChatMessage.mjs | 30 -------------------- module/main.mjs | 2 +- 3 files changed, 42 insertions(+), 31 deletions(-) create mode 100644 module/hooks/createChatMessage.mjs delete mode 100644 module/hooks/preCreateChatMessage.mjs diff --git a/module/hooks/createChatMessage.mjs b/module/hooks/createChatMessage.mjs new file mode 100644 index 0000000..12d1bdf --- /dev/null +++ b/module/hooks/createChatMessage.mjs @@ -0,0 +1,41 @@ +import { determinePrivacyFromRollMode } from "../utils/privacy.mjs"; +import { inferRollMode } from "../utils/inferRollMode.mjs"; + +Hooks.on(`createChatMessage`, (message, options, author) => { + console.log({ message, options, author}); + const isSelf = author === game.user.id; + const isNew = options.action === `create`; + const hasRolls = message.rolls?.length > 0; + const autoTracking = game.settings.get(__ID__, `autoTrackRolls`); + if (!isSelf || !isNew || !hasRolls || !autoTracking) { return }; + + /** An object of dice denomination to database rows */ + const rows = {}; + + const privacy = determinePrivacyFromRollMode(options.rollMode ?? inferRollMode(message)); + + /* + Goes through all of the dice within the message and grabs their result in order + to batch-save them all to the database handler. + */ + for (const roll of message.rolls) { + for (const die of roll.dice) { + const size = die.denomination; + rows[size] ??= []; + for (const result of die.results) { + rows[size].push({ privacy, value: result.result }); + }; + }; + }; + + // save all the rows, then rerender once we're properly done + for (const denomination in rows) { + CONFIG.stats.db.createRows( + `Dice/${denomination}`, + author, + rows[denomination], + { rerender: false }, + ); + }; + CONFIG.stats.db.render({ userUpdated: author }); +}); diff --git a/module/hooks/preCreateChatMessage.mjs b/module/hooks/preCreateChatMessage.mjs deleted file mode 100644 index 05a4d41..0000000 --- a/module/hooks/preCreateChatMessage.mjs +++ /dev/null @@ -1,30 +0,0 @@ -import { determinePrivacyFromRollMode } from "../utils/privacy.mjs"; - -Hooks.on(`preCreateChatMessage`, (_message, context, options, author) => { - const isNew = options.action === `create`; - const hasRolls = context.rolls?.length > 0; - const autoTracking = game.settings.get(__ID__, `autoTrackRolls`); - if (!isNew || !hasRolls || !autoTracking) { return }; - - /** An object of dice denomination to rows to add */ - const rows = {}; - - const privacy = determinePrivacyFromRollMode(options.rollMode); - for (const roll of context.rolls) { - for (const die of roll.dice) { - const size = die.denomination; - rows[size] ??= []; - for (const result of die.results) { - rows[size].push({ privacy, value: result.result }); - }; - }; - }; - - for (const denomination in rows) { - CONFIG.stats.db.createRows( - `Dice/${denomination}`, - author, - rows[denomination], - ); - }; -}); diff --git a/module/main.mjs b/module/main.mjs index 12d5422..4a90d3f 100644 --- a/module/main.mjs +++ b/module/main.mjs @@ -5,7 +5,7 @@ import "./hooks/init.mjs"; import "./hooks/ready.mjs"; // Document Hooks -import "./hooks/preCreateChatMessage.mjs"; +import "./hooks/createChatMessage.mjs"; // Dev Only imports if (import.meta.env.DEV) {