[API reference](https://jsprismarine.dev/api/index.md) / [@jsprismarine/prismarine](https://jsprismarine.dev/api/@jsprismarine/prismarine/index.md) / [chat/ChatManager](https://jsprismarine.dev/api/@jsprismarine/prismarine/chat/ChatManager/index.md) / ChatManager

# Class: ChatManager

Defined in: [packages/prismarine/src/chat/ChatManager.ts:137](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/prismarine/src/chat/ChatManager.ts#L137)

Chat for the whole server: sending a message, and which players it reaches.

A message goes out as a `chat` event first, which the console, the remote console and plugins
listen for, and then to every player that has joined whom its channel reaches. A player joins
the fan-out with [ChatManager.join](#join) once its join completes and leaves it with
[ChatManager.leave](#leave), both keyed by `keyOfPlayer`. The table is server-wide state, not a
world's, so it changes when the call is made, inside a tick or between ticks.

A line a player typed goes past the guard stage first, where a script's `chatSend`
before-event runs, and a veto there drops it before the event or any player sees it.

The event is a before-hook: its listeners are called in the order they subscribed, and a
listener that cancels it before its first wait stops every player, whenever they joined. The
players are not listeners themselves, so the fan-out is one pass over them after the event.

## Constructors

### Constructor

> **new ChatManager**(`events`): `ChatManager`

Defined in: [packages/prismarine/src/chat/ChatManager.ts:145](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/prismarine/src/chat/ChatManager.ts#L145)

#### Parameters

##### events

[`ChatBus`](https://jsprismarine.dev/api/@jsprismarine/prismarine/chat/ChatManager/index.md#chatbus)

The bus `chat` events go out on, which is the server's.

#### Returns

`ChatManager`

## Methods

### addGuard()

> **addGuard**(`guard`): () => `void`

Defined in: [packages/prismarine/src/chat/ChatManager.ts:178](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/prismarine/src/chat/ChatManager.ts#L178)

Adds a guard to the guard stage of a player's chat, after the ones already there.

#### Parameters

##### guard

[`PlayerChatGuard`](https://jsprismarine.dev/api/@jsprismarine/prismarine/chat/ChatManager/index.md#playerchatguard)

#### Returns

A function that removes it.

() => `void`

***

### hasJoined()

> **hasJoined**(`key`): `boolean`

Defined in: [packages/prismarine/src/chat/ChatManager.ts:223](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/prismarine/src/chat/ChatManager.ts#L223)

Whether a player is in the fan-out under a key.

#### Parameters

##### key

`string`

The player's key; see `keyOfPlayer`.

#### Returns

`boolean`

***

### join()

> **join**(`recipient`): `void`

Defined in: [packages/prismarine/src/chat/ChatManager.ts:203](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/prismarine/src/chat/ChatManager.ts#L203)

Adds a player to the fan-out. A player already joined under the same key is replaced.

#### Parameters

##### recipient

[`ChatRecipient`](https://jsprismarine.dev/api/@jsprismarine/prismarine/chat/ChatManager/index.md#chatrecipient)

The connected player.

#### Returns

`void`

***

### leave()

> **leave**(`recipient`): `void`

Defined in: [packages/prismarine/src/chat/ChatManager.ts:214](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/prismarine/src/chat/ChatManager.ts#L214)

Takes a player out of the fan-out. Does nothing when another player holds the key by
now, so a replaced login leaving late does not take its successor's chat with it.

#### Parameters

##### recipient

[`ChatRecipient`](https://jsprismarine.dev/api/@jsprismarine/prismarine/chat/ChatManager/index.md#chatrecipient)

The recipient [ChatManager.join](#join) was given.

#### Returns

`void`

***

### onSent()

> **onSent**(`listener`): () => `void`

Defined in: [packages/prismarine/src/chat/ChatManager.ts:191](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/prismarine/src/chat/ChatManager.ts#L191)

Calls `listener` with each line a player typed that went past the guards and the `chat`
event, as it goes out to the players, for a script's `world.afterEvents.chatSend`.

#### Parameters

##### listener

(`intent`) => `void`

#### Returns

A function that stops it.

() => `void`

***

### send()

> **send**(`chat`): `Promise`\<`void`\>

Defined in: [packages/prismarine/src/chat/ChatManager.ts:157](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/prismarine/src/chat/ChatManager.ts#L157)

Sends a message to the console, the plugins and every player its channel reaches.

The `chat` listeners run first, each to its first wait, and the players get the message
once they all have, unless one canceled it. Every line is queued before this returns.

#### Parameters

##### chat

[`Chat`](https://jsprismarine.dev/api/@jsprismarine/prismarine/chat/Chat/classes/Chat.md)

The message.

#### Returns

`Promise`\<`void`\>

When the transport has framed every player's line.
