# Members, roles and moderation

> Read members and roles, check permissions, and kick, ban and time out from code.

## Members and roles

The cache has the community's roles and channels, and the members it has seen. Fetch the full
member list when you need it:

**JavaScript:**

```js
const members = await community.members.fetch();
const mods = await community.roles.create("Moderators", { permissions: ["KickMembers", "TimeoutMembers"] });
await members.first().roles.add(mods);
```

**Rust:**

```rust
let members = community.fetch_members()?;
let mods = community.create_role("Moderators", Permissions::KICK_MEMBERS | Permissions::TIMEOUT_MEMBERS)?;
members[0].add_role(&mods)?;
```

## Checking permissions

**JavaScript:**

```js
if (member.permissions(channel).has("SendMessages")) { /* … */ }
```

**Rust:**

```rust
use zaun_client::protocol::bits::Permissions;

if author.can(Permissions::SEND_MESSAGES, Some(message.channel())) { /* … */ }
```

This uses the same resolver as the host, applied to the cache. Use it to hide what a member can't
do; the host still decides (it also applies lockdowns and newcomer rules).

## Moderation

**JavaScript:**

```js
await target.timeout(10 * 60_000, "cool down");
await target.kick("asked to");
await target.ban("spam");
```

**Rust:**

```rust
use std::time::Duration;

target.timeout(Some(Duration::from_secs(600)), Some("cool down"))?;
target.kick(Some("asked to"))?;
target.ban(Some("spam"))?;
```

Each needs the matching permission (`TIMEOUT_MEMBERS`, `KICK_MEMBERS`, `BAN_MEMBERS`) and a highest
role above the target's. Nobody can act on the owner. Bans follow the member's key, so a banned
member can't come back with the same identity.

## Invites and channels

**JavaScript:**

```js
const link = await community.createInvite({ maxUses: 5, expiresIn: 86_400 });
const lounge = await community.channels.create("lounge", { type: "voice" });
```

**Rust:**

```rust
use zaun_client::protocol::model::ChannelKind;

let link = community.create_invite(5, Some(Duration::from_secs(86_400)))?; // 5 uses, one day
let lounge = community.create_channel("lounge", ChannelKind::Voice)?;
```
