Add a utility to the API for inferring a chat message's roll mode (and update the docs)
This commit is contained in:
parent
cbc2691a0e
commit
8c42f1b240
3 changed files with 41 additions and 3 deletions
|
|
@ -13,6 +13,7 @@ import { UserFlagDatabase } from "./utils/databases/UserFlag.mjs";
|
|||
import { barGraphSchema, numberBucketSchema, rowSchema, stringBucketSchema, tableSchema } from "./utils/databases/model.mjs";
|
||||
import { determinePrivacyFromRollMode, filterPrivateRows, PrivacyMode } from "./utils/privacy.mjs";
|
||||
import { validateBucketConfig, validateValue } from "./utils/buckets.mjs";
|
||||
import { inferRollMode } from "./utils/inferRollMode.mjs";
|
||||
|
||||
const { deepFreeze } = foundry.utils;
|
||||
|
||||
|
|
@ -25,6 +26,7 @@ export const api = deepFreeze({
|
|||
},
|
||||
utils: {
|
||||
determinePrivacyFromRollMode,
|
||||
inferRollMode,
|
||||
filterPrivateRows,
|
||||
validateValue,
|
||||
validateBucketConfig,
|
||||
|
|
|
|||
36
module/utils/inferRollMode.mjs
Normal file
36
module/utils/inferRollMode.mjs
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
/**
|
||||
* A helper function to try and infer what roll mode was used when creating a
|
||||
* chat message in case the roll mode was not provided during the createChatMessage
|
||||
* hook for whatever reason.
|
||||
*
|
||||
* **Disclaimer**: This inference is not totally correct. Particularly when inferring
|
||||
* a GM's message, as it won't be able to distinguish between a self-roll and a
|
||||
* private GM roll when it's
|
||||
*
|
||||
* @param {ChatMessage} message The ChatMessage document to infer from
|
||||
* @returns The Foundry-specified roll mode
|
||||
*/
|
||||
export function inferRollMode(message) {
|
||||
const whisperCount = message.whisper.length;
|
||||
if (whisperCount === 0) {
|
||||
return CONST.DICE_ROLL_MODES.PUBLIC;
|
||||
};
|
||||
|
||||
if (whisperCount === 1 && message.whisper[0] === game.user.id) {
|
||||
return CONST.DICE_ROLL_MODES.SELF;
|
||||
};
|
||||
|
||||
let allGMs = true;
|
||||
for (const userID of message.whisper) {
|
||||
const user = game.users.get(userID);
|
||||
if (!user) { continue };
|
||||
allGMs &&= user.isGM;
|
||||
};
|
||||
|
||||
if (!allGMs) {
|
||||
return CONST.DICE_ROLL_MODES.PUBLIC;
|
||||
};
|
||||
return message.blind
|
||||
? CONST.DICE_ROLL_MODES.BLIND
|
||||
: CONST.DICE_ROLL_MODES.PRIVATE;
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue