Blind Chat Messages

Ccalego10/23/2021
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?
Ccalego10/23/2021
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,
    }


however, the created chat message is visible plain as day to the creator
Ccalego10/23/2021
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;
  }
Ccalego10/23/2021
Trying to feed the api the GM's user id as speaker makes an error that the user lacks permission
Ccalego10/23/2021
Looks like this isn't strictly possible...

https://gitlab.com/foundrynet/foundryvtt/-/issues/4317
Ccalego10/23/2021
workaround time
Mmanaflower10/23/2021
You need to hook into renderChatMessage and do html.remove() for non-GMs or something.
Mmanaflower10/23/2021
Or possibly just hide them, in case Foundry gets huffy about the DOM element being missing.
Ccalego10/23/2021
If I put a flag to help distinguish which cards are mine, I can catch them in a renderChatMessage hook and nuke them yeah.
Ccalego10/23/2021
Hiding them is probably a better play
Ccalego10/23/2021
which would be easiest if I can set a class on the overall chat message instead of just its contents
Ccalego10/23/2021
cssClass on messageData?
Ccalego10/23/2021
noope
Ccalego10/23/2021
:has would solve me... https://caniuse.com/css-has
Mmanaflower10/23/2021
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.
Ccalego10/23/2021
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.
Ccalego11/3/2021
@drl2 you might find this thread insightful