Add a preCreateChatMessage hook for auto-logging of dice results

This commit is contained in:
Oliver-Akins 2025-05-04 21:36:26 -06:00
parent f2f742e65a
commit 1a4d764a15
2 changed files with 33 additions and 0 deletions

View file

@ -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],
);
};
});

View file

@ -1,4 +1,8 @@
import "./api.mjs"; import "./api.mjs";
// Lifecycle Hooks
import "./hooks/init.mjs"; import "./hooks/init.mjs";
import "./hooks/ready.mjs"; import "./hooks/ready.mjs";
// Document Hooks
import "./hooks/preCreateChatMessage.mjs";