# Status and presence

> Set your status, activity and custom status text, and show everyone else's: online, idle, do not disturb, invisible.

Every member has a **presence**: a status (online, idle, do not disturb, or offline) and
optionally an **activity**, what they are doing ("Playing chess", "Listening to lofi radio"). A
member can also set a **custom status text** with an emoji, which the host keeps until it is
changed or expires. Bots have the same presence as members
([bots: status and presence](/docs/guide/bots/status-and-presence/)).

## Setting your status and activity

Presence is set once for the whole client and goes to every community it is connected to.

**JavaScript:**

```js
client.setPresence("online", { type: "listening", name: "lofi radio", state: "ChilledCow" });
client.setPresence("dnd");          // no activity
client.setPresence("invisible");    // everyone else sees you offline
```

`setPresence` returns `false`, and sends nothing, when the status or the activity is not valid.

**Rust:**

```rust
use zaun_client::presence::{activity, ActivityType, Status};

let mut listening = activity(ActivityType::Listening, "lofi radio");
listening.state = Some("ChilledCow".into());
client.set_presence(Status::Online, Some(listening))?;
client.set_presence(Status::Dnd, None)?;          // no activity
client.set_presence(Status::Invisible, None)?;    // everyone else sees you offline
```

| Status | Everyone else sees |
|---|---|
| `online` | online |
| `idle` | idle (away) |
| `dnd` | do not disturb |
| `invisible` | offline, with no activity |

An activity has a `type` (`kind` in Rust): `playing`, `streaming`, `listening`, `watching`,
`competing` or `custom`; a `name`; and optionally `details` and `state` (a second and third line),
`started_at` (ms since the epoch, to show an elapsed time), `url` (an `https://` link, streaming
only) and `emoji` (custom only).

The host shows at most one change per member every 3 seconds. Call it as often as you like: the
client keeps the latest, sends it when the window allows, and sends it again after each
reconnection.

## Custom status text

The custom status is set per community, and the host keeps it (presence is live only).

**JavaScript:**

```js
await community.setStatusText("Back soon", { emoji: "☕", expiresAt: new Date(Date.now() + 3_600_000) });
await community.setStatusText("Reviewing PRs");   // kept until changed
await community.setStatusText(null);              // clears it
```

**Rust:**

```rust
community.set_status_text(Some("Back soon"), Some("☕"), Some(expires_at_ms))?;
community.set_status_text(Some("Reviewing PRs"), None, None)?;   // kept until changed
community.set_status_text(None, None, None)?;                    // clears it
```

When it is set, changed, cleared or expires, everyone else gets a **member update**
(`memberUpdate` / `Event::MemberUpdate`).

## Showing presence

**JavaScript:**

```js
const verbs = { playing: "Playing", streaming: "Streaming", listening: "Listening to",
                watching: "Watching", competing: "Competing in" };
const line = (a) => a.type === "custom" ? `${a.emoji ?? ""} ${a.name}`.trim() : `${verbs[a.type]} ${a.name}`;

client.on("presenceUpdate", ({ member, status, activity }) => {
  showPresence(member, status, activity ? line(activity) : null);
});

member.presence;      // { status: "online" | "idle" | "dnd" | "offline", activity }
member.statusText;    // custom status text, or null
member.statusEmoji;   // its emoji, or null
community.presences;  // every member who is not offline: [{ member, status, activity }]
```

**Rust:**

```rust
use zaun_client::Event;
use zaun_client::presence::Status;

if let Event::PresenceUpdate { community, presence } = event {
    let member = community.member(presence.member);
    let away = presence.status == Status::Idle;
    let line = presence.activity_line();   // "Listening to lofi radio", or the custom text
}

let p = member.presence();             // offline when unknown
let text = member.status_text();       // Option<String>
let emoji = member.status_emoji();     // Option<String>
let online = community.presences();    // every member who is not offline
```

After a community is ready, the client loads who is online and emits `presencesReady`
(`Event::PresencesReady`); from then on each change arrives as `presenceUpdate`
(`Event::PresenceUpdate`). Invisible members look offline, and a member who goes offline leaves
`community.presences`.

A streaming activity's link should open only after the reader confirms. Activities carry no
pictures and no other links, so showing one never makes your app fetch anything.

## Limits

| Item | Limit |
|---|---|
| Activity name | 1–128 characters |
| Activity details, state | 0–128 characters each |
| Custom status text | 1–128 characters, plus an optional emoji |
| Emoji | one Unicode emoji, ≤ 64 bytes |
| Presence changes shown | one every 3 s per member; the latest wins |
