# Bot

Defined in: [sdk/js/src/bot.ts:45](https://github.com/zaunchat/zaun-bot/blob/main/sdk/js/src/bot.ts#L45)

One Zaun bot.

## Constructors

### Constructor

> **new Bot**(): `Bot`

#### Returns

`Bot`

## Properties

### audio

> `readonly` **audio**: `object`

Defined in: [sdk/js/src/bot.ts:58](https://github.com/zaunchat/zaun-bot/blob/main/sdk/js/src/bot.ts#L58)

Low-level audio output in the bot's voice channel: write Opus or PCM yourself.

#### bufferedMs()

> **bufferedMs**(): `number`

The audio written and not yet played, in ms (0 without a stream).

##### Returns

`number`

#### clear()

> **clear**(): `void`

Drops everything buffered at once; the stream stays open.

##### Returns

`void`

#### close()

> **close**(`drain?`): `void`

Closes the stream, after the buffer has played (`drain`, the default) or at once.

##### Parameters

###### drain?

`boolean` = `true`

##### Returns

`void`

#### open()

> **open**(`options`): `void`

Opens the stream, pausing the player. `CONFLICT` when the bot is in no voice channel or a
stream is open; `UNSUPPORTED` for a format the host cannot take (PCM without an encoder).

##### Parameters

###### options

[`AudioStreamOptions`](/docs/reference/js/zaun-bot/interfaces/audiostreamoptions/)

##### Returns

`void`

#### writeOpus()

> **writeOpus**(`packets`): `number`

Opus packets (2.5–120 ms each, ≤ 1000 bytes); returns the ms buffered (`BUSY` past 3 s).

##### Parameters

###### packets

`Uint8Array`\<`ArrayBufferLike`\>[]

##### Returns

`number`

#### writePcm()

> **writePcm**(`samples`): `number`

Interleaved samples, any count; returns the ms buffered (`BUSY` past 3 s).

##### Parameters

###### samples

`Int16Array`

##### Returns

`number`

***

### commands

> `readonly` **commands**: `Map`\<`string`, [`Command`](/docs/reference/js/zaun-bot/interfaces/command/)\>

Defined in: [sdk/js/src/bot.ts:64](https://github.com/zaunchat/zaun-bot/blob/main/sdk/js/src/bot.ts#L64)

Commands by name.

***

### community

> `readonly` **community**: `object` = `api.community`

Defined in: [sdk/js/src/bot.ts:52](https://github.com/zaunchat/zaun-bot/blob/main/sdk/js/src/bot.ts#L52)

#### addRole

> **addRole**: (`memberId`, `roleId`) => `void`

##### Parameters

###### memberId

`string`

###### roleId

`string`

##### Returns

`void`

#### getChannel

> **getChannel**: (`id`) => `Channel`

##### Parameters

###### id

`string`

##### Returns

`Channel`

#### getMember

> **getMember**: (`id`) => [`Member`](/docs/reference/js/zaun-bot/classes/member/)

##### Parameters

###### id

`string`

##### Returns

[`Member`](/docs/reference/js/zaun-bot/classes/member/)

#### getRole

> **getRole**: (`id`) => `Role`

##### Parameters

###### id

`string`

##### Returns

`Role`

#### kick

> **kick**: (`memberId`, `reason?`) => `void`

##### Parameters

###### memberId

`string`

###### reason?

`string`

##### Returns

`void`

#### listChannels

> **listChannels**: () => `Channel`[]

##### Returns

`Channel`[]

#### listMembers

> **listMembers**: (`options`) => [`Member`](/docs/reference/js/zaun-bot/classes/member/)[]

##### Parameters

###### options?

###### after?

`string`

###### limit?

`number`

##### Returns

[`Member`](/docs/reference/js/zaun-bot/classes/member/)[]

#### listRoles

> **listRoles**: () => `Role`[]

##### Returns

`Role`[]

#### removeRole

> **removeRole**: (`memberId`, `roleId`) => `void`

##### Parameters

###### memberId

`string`

###### roleId

`string`

##### Returns

`void`

#### timeout

> **timeout**: (`memberId`, `until`, `reason?`) => `void`

Times a member out until `until` (ms); `null` clears it.

##### Parameters

###### memberId

`string`

###### until

`number`

###### reason?

`string`

##### Returns

`void`

***

### config

> **config**: `ReadonlyMap`\<`string`, `string`\>

Defined in: [sdk/js/src/bot.ts:69](https://github.com/zaunchat/zaun-bot/blob/main/sdk/js/src/bot.ts#L69)

The bot's configuration: the keys its manifest declares in `[config]`, with the owner's
values or the defaults. Set at `init`, replaced on every change (`config` event).

***

### dms

> `readonly` **dms**: `object`

Defined in: [sdk/js/src/bot.ts:54](https://github.com/zaunchat/zaun-bot/blob/main/sdk/js/src/bot.ts#L54)

Direct messages (capability `dms`).

#### close()

> **close**(`channelId`): `void`

Hides a DM from the bot's list.

##### Parameters

###### channelId

`string`

##### Returns

`void`

#### listOpen()

> **listOpen**(): `Channel`[]

The DMs the bot has open.

##### Returns

`Channel`[]

#### open()

> **open**(`memberId`): `Channel`

The DM with `memberId`, opened when there is none.

##### Parameters

###### memberId

`string`

##### Returns

`Channel`

***

### http

> `readonly` **http**: `object` = `api.http`

Defined in: [sdk/js/src/bot.ts:50](https://github.com/zaunchat/zaun-bot/blob/main/sdk/js/src/bot.ts#L50)

#### fetch()

> **fetch**(`url`, `init?`): [`HttpResponse`](/docs/reference/js/zaun-bot/interfaces/httpresponse/)

##### Parameters

###### url

`string`

###### init?

###### body?

`string` \| `Uint8Array`\<`ArrayBufferLike`\>

###### headers?

`Record`\<`string`, `string`\>

###### method?

`string`

###### timeoutMs?

`number`

##### Returns

[`HttpResponse`](/docs/reference/js/zaun-bot/interfaces/httpresponse/)

***

### log

> `readonly` **log**: `Record`\<`"trace"` \| `"debug"` \| `"info"` \| `"warn"` \| `"error"`, (...`args`) => `void`\> = `api.log`

Defined in: [sdk/js/src/bot.ts:51](https://github.com/zaunchat/zaun-bot/blob/main/sdk/js/src/bot.ts#L51)

***

### me

> **me**: [`Me`](/docs/reference/js/zaun-bot/interfaces/me/)

Defined in: [sdk/js/src/bot.ts:62](https://github.com/zaunchat/zaun-bot/blob/main/sdk/js/src/bot.ts#L62)

Who the bot is, once the host's `ready` event arrived.

***

### messages

> `readonly` **messages**: `object` = `api.messages`

Defined in: [sdk/js/src/bot.ts:47](https://github.com/zaunchat/zaun-bot/blob/main/sdk/js/src/bot.ts#L47)

Host APIs (also importable on their own).

#### delete()

> **delete**(`channelId`, `messageId`): `void`

##### Parameters

###### channelId

`string`

###### messageId

`string`

##### Returns

`void`

#### dm()

> **dm**(`memberId`, `payload`): `object`

A direct message to `memberId` (capability `dms`): opens the DM when needed. Returns the
DM's channel id and the message id.

##### Parameters

###### memberId

`string`

###### payload

[`MessagePayload`](/docs/reference/js/zaun-bot/type-aliases/messagepayload/)

##### Returns

`object`

###### channelId

> **channelId**: `string`

###### messageId

> **messageId**: `string`

#### edit()

> **edit**(`channelId`, `messageId`, `payload`): `void`

Edits one of the bot's messages.

##### Parameters

###### channelId

`string`

###### messageId

`string`

###### payload

[`MessagePayload`](/docs/reference/js/zaun-bot/type-aliases/messagepayload/)

##### Returns

`void`

#### history()

> **history**(`channelId`, `options?`): [`Message`](/docs/reference/js/zaun-bot/classes/message/)[]

Up to `limit` (1–100) messages before the sequence `before`.

##### Parameters

###### channelId

`string`

###### options?

###### before?

`number`

###### limit?

`number`

##### Returns

[`Message`](/docs/reference/js/zaun-bot/classes/message/)[]

#### react()

> **react**(`channelId`, `messageId`, `emoji`): `void`

##### Parameters

###### channelId

`string`

###### messageId

`string`

###### emoji

`string`

##### Returns

`void`

#### send()

> **send**(`channelId`, `payload`, `options?`): `string`

Posts a message; returns its id.

##### Parameters

###### channelId

`string`

###### payload

[`MessagePayload`](/docs/reference/js/zaun-bot/type-aliases/messagepayload/)

###### options?

###### replyTo?

`string`

##### Returns

`string`

#### unreact()

> **unreact**(`channelId`, `messageId`, `emoji`): `void`

##### Parameters

###### channelId

`string`

###### messageId

`string`

###### emoji

`string`

##### Returns

`void`

#### upload()

> **upload**(`channelId`, `name`, `mime`, `bytes`): `string`

Uploads a file for the bot's next message (capability `attachments`); returns its id.

##### Parameters

###### channelId

`string`

###### name

`string`

###### mime

`string`

###### bytes

`Uint8Array`

##### Returns

`string`

***

### presence

> `readonly` **presence**: `object`

Defined in: [sdk/js/src/bot.ts:60](https://github.com/zaunchat/zaun-bot/blob/main/sdk/js/src/bot.ts#L60)

The bot's status, activity and custom status text.

#### clearStatus()

> **clearStatus**(): `void`

Clears the custom status text and emoji.

##### Returns

`void`

#### set()

> **set**(`status`, `activity?`): `void`

Sets the status and activity (none: no activity).

##### Parameters

###### status

[`PresenceStatus`](/docs/reference/js/zaun-bot/type-aliases/presencestatus/)

###### activity?

[`Activity`](/docs/reference/js/zaun-bot/classes/activity/)

##### Returns

`void`

#### setStatus()

> **setStatus**(`text?`, `emoji?`, `expiresAt?`): `void`

Sets the custom status text and emoji, cleared by the host at `expiresAt` (ms) if given.

##### Parameters

###### text?

`string`

###### emoji?

`string`

###### expiresAt?

`number` \| `bigint`

##### Returns

`void`

#### setStatusText()

> **setStatusText**(`text`): `void`

Sets the custom status text, kept until changed.

##### Parameters

###### text

`string`

##### Returns

`void`

***

### storage

> `readonly` **storage**: `object` = `api.storage`

Defined in: [sdk/js/src/bot.ts:48](https://github.com/zaunchat/zaun-bot/blob/main/sdk/js/src/bot.ts#L48)

#### delete()

> **delete**(`key`): `void`

##### Parameters

###### key

`string`

##### Returns

`void`

#### get()

> **get**(`key`): `Uint8Array`\<`ArrayBufferLike`\>

##### Parameters

###### key

`string`

##### Returns

`Uint8Array`\<`ArrayBufferLike`\>

#### getJSON()

> **getJSON**\<`T`\>(`key`): `T`

A JSON value (bigints are not supported by JSON).

##### Type Parameters

###### T

`T` = `unknown`

##### Parameters

###### key

`string`

##### Returns

`T`

#### getString()

> **getString**(`key`): `string`

##### Parameters

###### key

`string`

##### Returns

`string`

#### keys()

> **keys**(`prefix?`, `options?`): `string`[]

Keys starting with `prefix`, in order.

##### Parameters

###### prefix?

`string` = `''`

###### options?

###### after?

`string`

###### limit?

`number`

##### Returns

`string`[]

#### set()

> **set**(`key`, `value`): `void`

##### Parameters

###### key

`string`

###### value

`Uint8Array`

##### Returns

`void`

#### setJSON()

> **setJSON**(`key`, `value`): `void`

##### Parameters

###### key

`string`

###### value

`unknown`

##### Returns

`void`

#### setString()

> **setString**(`key`, `value`): `void`

##### Parameters

###### key

`string`

###### value

`string`

##### Returns

`void`

***

### timers

> `readonly` **timers**: `object` = `api.timers`

Defined in: [sdk/js/src/bot.ts:49](https://github.com/zaunchat/zaun-bot/blob/main/sdk/js/src/bot.ts#L49)

#### after()

> **after**(`ms`, `tag`): `number`

Once after `ms`; returns the timer id.

##### Parameters

###### ms

`number`

###### tag

`string`

##### Returns

`number`

#### cancel()

> **cancel**(`id`): `void`

##### Parameters

###### id

`number`

##### Returns

`void`

#### every()

> **every**(`ms`, `tag`): `number`

Every `ms` (≥ 1000).

##### Parameters

###### ms

`number`

###### tag

`string`

##### Returns

`number`

***

### voice

> `readonly` **voice**: `object`

Defined in: [sdk/js/src/bot.ts:56](https://github.com/zaunchat/zaun-bot/blob/main/sdk/js/src/bot.ts#L56)

Voice channels and music (capability `voice`).

#### clearQueue()

> **clearQueue**(): `void`

Empties the queue; unlike `stop`, the current track keeps playing.

##### Returns

`void`

#### endStream()

> **endStream**(): `void`

The `pushed` track ends once its buffer has played.

##### Returns

`void`

#### enqueue()

> **enqueue**(`t`): `number`

Adds to the queue (≤ 100); returns its length.

##### Parameters

###### t

`string` \| [`TrackOptions`](/docs/reference/js/zaun-bot/type-aliases/trackoptions/)

##### Returns

`number`

#### join()

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

Joins a voice channel (leaving another first); returns once audio can flow.

##### Parameters

###### channelId

`string`

##### Returns

`void`

#### leave()

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

Leaves; playback stops and the queue clears.

##### Returns

`void`

#### moveTrack()

> **moveTrack**(`from`, `to`): `void`

Moves the queued track at `from` to `to` (both 0-based); the tracks between shift.

##### Parameters

###### from

`number`

###### to

`number`

##### Returns

`void`

#### pause()

> **pause**(): `void`

##### Returns

`void`

#### play()

> **play**(`t`): `void`

Plays now, replacing the current track (a string is a URL).

##### Parameters

###### t

`string` \| [`TrackOptions`](/docs/reference/js/zaun-bot/type-aliases/trackoptions/)

##### Returns

`void`

#### push()

> **push**(`packets`): `number`

Opus packets for the current `pushed` track; returns ms buffered (`BUSY` past 3 s).

##### Parameters

###### packets

`Uint8Array`\<`ArrayBufferLike`\>[]

##### Returns

`number`

#### remove()

> **remove**(`index`): `string`

Removes the queued track at `index` (0 plays next); returns its title. Throws `NOT_FOUND`
past the end. Like every queue call, `UNSUPPORTED` on hosts without queue control.

##### Parameters

###### index

`number`

##### Returns

`string`

#### resume()

> **resume**(): `void`

##### Returns

`void`

#### seek()

> **seek**(`ms`): `void`

Jumps to `ms` into the current track; past the end, the track ends. Throws `UNSUPPORTED` for
live streams and pushed audio, and on hosts that cannot seek; `CONFLICT` when nothing plays.

##### Parameters

###### ms

`number`

##### Returns

`void`

#### setLoop()

> **setLoop**(`mode`): `void`

Sets what happens when a track ends.

##### Parameters

###### mode

`LoopMode`

##### Returns

`void`

#### setVolume()

> **setVolume**(`percent`): `void`

Sets the playback volume, 0–200 % (100 when the bot joins). Throws `ZaunError` `BAD_REQUEST`
(detail `percent`) outside 0–200, checked before the call, and `UNSUPPORTED` on hosts without
an Opus encoder, which is most of them for now: tell the member and go on.

##### Parameters

###### percent

`number`

##### Returns

`void`

#### shuffle()

> **shuffle**(): `void`

Puts the queue in a random order; the current track keeps playing.

##### Returns

`void`

#### skip()

> **skip**(): `void`

##### Returns

`void`

#### state()

> **state**(): [`VoiceState`](/docs/reference/js/zaun-bot/type-aliases/voicestate/)

##### Returns

[`VoiceState`](/docs/reference/js/zaun-bot/type-aliases/voicestate/)

#### stop()

> **stop**(): `void`

Ends the current track and clears the queue.

##### Returns

`void`

## Methods

### command()

#### Call Signature

> **command**(`data`, `execute?`, `autocomplete?`): `this`

Defined in: [sdk/js/src/bot.ts:76](https://github.com/zaunchat/zaun-bot/blob/main/sdk/js/src/bot.ts#L76)

Registers a command; `execute` answers it (or handle it in `interactionCreate`).

##### Parameters

###### data

[`CommandLike`](/docs/reference/js/zaun-bot/interfaces/commandlike/)

###### execute?

(`i`) => `unknown`

###### autocomplete?

(`i`) => `unknown`

##### Returns

`this`

#### Call Signature

> **command**(`command`): `this`

Defined in: [sdk/js/src/bot.ts:77](https://github.com/zaunchat/zaun-bot/blob/main/sdk/js/src/bot.ts#L77)

Registers a command; `execute` answers it (or handle it in `interactionCreate`).

##### Parameters

###### command

[`Command`](/docs/reference/js/zaun-bot/interfaces/command/)

##### Returns

`this`

***

### component()

> **component**(`match`, `handler`): `this`

Defined in: [sdk/js/src/bot.ts:85](https://github.com/zaunchat/zaun-bot/blob/main/sdk/js/src/bot.ts#L85)

Handles clicks and choices whose custom id matches.

#### Parameters

##### match

`Match`

##### handler

(`i`) => `unknown`

#### Returns

`this`

***

### emit()

> **emit**\<`K`\>(`event`, ...`args`): `void`

Defined in: [sdk/js/src/bot.ts:110](https://github.com/zaunchat/zaun-bot/blob/main/sdk/js/src/bot.ts#L110)

Calls every listener of `event`; errors go to `error` listeners and the log.

#### Type Parameters

##### K

`K` *extends* keyof [`BotEvents`](/docs/reference/js/zaun-bot/interfaces/botevents/)

#### Parameters

##### event

`K`

##### args

...`Parameters`\<[`BotEvents`](/docs/reference/js/zaun-bot/interfaces/botevents/)\[`K`\]\>

#### Returns

`void`

***

### exports()

> **exports**(): `object`

Defined in: [sdk/js/src/bot.ts:160](https://github.com/zaunchat/zaun-bot/blob/main/sdk/js/src/bot.ts#L160)

The `zaun:bot/bot` world's exports; export them from your entry module.

#### Returns

`object`

##### init

> **init**: (`config`) => `Registration`

###### Parameters

###### config

\[`string`, `string`\][]

###### Returns

`Registration`

##### onAutocomplete

> **onAutocomplete**: (`interaction`) => `Choice`[]

###### Parameters

###### interaction

`Interaction`

###### Returns

`Choice`[]

##### onConfigChange

> **onConfigChange**: (`config`) => `void`

###### Parameters

###### config

\[`string`, `string`\][]

###### Returns

`void`

##### onEvent

> **onEvent**: (`event`) => `void`

###### Parameters

###### event

`Event`

###### Returns

`void`

##### onInteraction

> **onInteraction**: (`interaction`) => `void`

###### Parameters

###### interaction

`Interaction`

###### Returns

`void`

##### onTimer

> **onTimer**: (`id`, `tag`) => `void`

###### Parameters

###### id

`bigint`

###### tag

`string`

###### Returns

`void`

##### onVoiceEvent

> **onVoiceEvent**: (`event`) => `void`

###### Parameters

###### event

`VoiceEvent`

###### Returns

`void`

***

### modal()

> **modal**(`match`, `handler`): `this`

Defined in: [sdk/js/src/bot.ts:91](https://github.com/zaunchat/zaun-bot/blob/main/sdk/js/src/bot.ts#L91)

Handles submitted modals whose custom id matches.

#### Parameters

##### match

`Match`

##### handler

(`i`) => `unknown`

#### Returns

`this`

***

### off()

> **off**\<`K`\>(`event`, `listener`): `this`

Defined in: [sdk/js/src/bot.ts:103](https://github.com/zaunchat/zaun-bot/blob/main/sdk/js/src/bot.ts#L103)

#### Type Parameters

##### K

`K` *extends* keyof [`BotEvents`](/docs/reference/js/zaun-bot/interfaces/botevents/)

#### Parameters

##### event

`K`

##### listener

[`BotEvents`](/docs/reference/js/zaun-bot/interfaces/botevents/)\[`K`\]

#### Returns

`this`

***

### on()

> **on**\<`K`\>(`event`, `listener`): `this`

Defined in: [sdk/js/src/bot.ts:96](https://github.com/zaunchat/zaun-bot/blob/main/sdk/js/src/bot.ts#L96)

#### Type Parameters

##### K

`K` *extends* keyof [`BotEvents`](/docs/reference/js/zaun-bot/interfaces/botevents/)

#### Parameters

##### event

`K`

##### listener

[`BotEvents`](/docs/reference/js/zaun-bot/interfaces/botevents/)\[`K`\]

#### Returns

`this`
