Blind Chat Messages

Anyone have good examples of creating a chat message that should only be visible to a connected GM, but might be created by any connected client?
7 Replies
Calego
Calego3y ago
What I've got:
const messageData = {
whisper: ChatMessage.getWhisperRecipients('gm'),
blind: !game.user.isGM,
user: game.user.data._id,
type: CONST.CHAT_MESSAGE_TYPES.WHISPER,
speaker: { alias: game.i18n.localize(`${this.MODULE_NAME}.MESSAGE_HEADER`) },
content: html,
}
const messageData = {
whisper: ChatMessage.getWhisperRecipients('gm'),
blind: !game.user.isGM,
user: game.user.data._id,
type: CONST.CHAT_MESSAGE_TYPES.WHISPER,
speaker: { alias: game.i18n.localize(`${this.MODULE_NAME}.MESSAGE_HEADER`) },
content: html,
}
however, the created chat message is visible plain as day to the creator It looks to me like ChatMessage.isVisible makes a chat message visible to the creating user always (comments mine):
get visible() {
// if this has whisper recipients
if ( this.data.whisper.length ) {
// if its a roll, always yes (`isContentVisible` has more rules)
if ( this.data.type === CONST.CHAT_MESSAGE_TYPES.ROLL ) return true;

// if this chat message's user is the same as the logged in user --> true
// or if the logged in user matches one of the recipients
return (this.data.user === game.user.id) || (this.data.whisper.indexOf(game.user.id) !== -1);
}
return true;
}
get visible() {
// if this has whisper recipients
if ( this.data.whisper.length ) {
// if its a roll, always yes (`isContentVisible` has more rules)
if ( this.data.type === CONST.CHAT_MESSAGE_TYPES.ROLL ) return true;

// if this chat message's user is the same as the logged in user --> true
// or if the logged in user matches one of the recipients
return (this.data.user === game.user.id) || (this.data.whisper.indexOf(game.user.id) !== -1);
}
return true;
}
Trying to feed the api the GM's user id as speaker makes an error that the user lacks permission
Calego
Calego3y ago
Looks like this isn't strictly possible... https://gitlab.com/foundrynet/foundryvtt/-/issues/4317
GitLab
Suggestion: Private GM / Blind / Self Roll should also apply to cha...
Currently, if you just type some text into the chat box (i.e. not a roll), it does not honour the current default roll setting (public, private, blind, self)....
Calego
Calego3y ago
workaround time
Mana
Mana3y ago
You need to hook into renderChatMessage and do html.remove() for non-GMs or something. Or possibly just hide them, in case Foundry gets huffy about the DOM element being missing.
Calego
Calego3y ago
If I put a flag to help distinguish which cards are mine, I can catch them in a renderChatMessage hook and nuke them yeah. Hiding them is probably a better play which would be easiest if I can set a class on the overall chat message instead of just its contents cssClass on messageData? noope :has would solve me... https://caniuse.com/css-has
Mana
Mana3y ago
renderChatMessage happens before the chat message is actually visible, so as long as you aren't turning the hook async, you can modify it all you like before it gets shown.
Calego
Calego3y ago
Yeah I ended up adding a class to the html in the hook if the message has my flag. It's annoying to me that I need to make code which runs on every message to affect only mine though. I gave my thumbsup to the issue I linked. @drl2 you might find this thread insightful