Add a preCreateChatMessage hook for auto-logging of dice results
This commit is contained in:
parent
f2f742e65a
commit
1a4d764a15
2 changed files with 33 additions and 0 deletions
29
module/hooks/preCreateChatMessage.mjs
Normal file
29
module/hooks/preCreateChatMessage.mjs
Normal 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],
|
||||
);
|
||||
};
|
||||
});
|
||||
|
|
@ -1,4 +1,8 @@
|
|||
import "./api.mjs";
|
||||
|
||||
// Lifecycle Hooks
|
||||
import "./hooks/init.mjs";
|
||||
import "./hooks/ready.mjs";
|
||||
|
||||
// Document Hooks
|
||||
import "./hooks/preCreateChatMessage.mjs";
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue