diff --git a/module/hooks/preCreateChatMessage.mjs b/module/hooks/preCreateChatMessage.mjs new file mode 100644 index 0000000..ed84645 --- /dev/null +++ b/module/hooks/preCreateChatMessage.mjs @@ -0,0 +1,29 @@ +import { determinePrivacyFromRollMode } from "../utils/privacy.mjs"; + +Hooks.on(`preCreateChatMessage`, (_message, context, options, author) => { + const isNew = options.action === `create`; + const hasRolls = context.rolls?.length > 0; + if (!isNew || !hasRolls) { return }; + + /** An object of dice denomination to rows to add */ + const rows = {}; + + const mode = 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({ mode, 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 1bc2cd9..b3a76f4 100644 --- a/module/main.mjs +++ b/module/main.mjs @@ -1,4 +1,8 @@ import "./api.mjs"; +// Lifecycle Hooks import "./hooks/init.mjs"; import "./hooks/ready.mjs"; + +// Document Hooks +import "./hooks/preCreateChatMessage.mjs";