Skip to content

Commit

Permalink
Timeout for bot.tabComplete() (PrismarineJS#3293)
Browse files Browse the repository at this point in the history
* Added timeout to bot.tabComplete()

* Updated api.md with doctoc

* one too many blank line fix -_-

* Changed timeout system to onceWithCleanup() function

* Shorter require()

* Better default timeout

* Rollback

* No try, no catch
  • Loading branch information
Ynfuien authored Jan 27, 2024
1 parent 210785e commit 4231a16
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
7 changes: 4 additions & 3 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@
- ["blockBreakProgressEnd" (block, entity)](#blockbreakprogressend-block-entity)
- ["diggingCompleted" (block)](#diggingcompleted-block)
- ["diggingAborted" (block)](#diggingaborted-block)
- ["usedfirework"](#usedfirework)
- ["usedFirework" (fireworkEntityId)](#usedfirework-fireworkentityid)
- ["move"](#move)
- ["forcedMove"](#forcedmove)
- ["mount"](#mount)
Expand Down Expand Up @@ -270,7 +270,7 @@
- [Methods](#methods)
- [bot.end(reason)](#botendreason)
- [bot.quit(reason)](#botquitreason)
- [bot.tabComplete(str, [assumeCommand], [sendBlockInSight])](#bottabcompletestr-assumecommand-sendblockinsight)
- [bot.tabComplete(str, [assumeCommand], [sendBlockInSight], [timeout])](#bottabcompletestr-assumecommand-sendblockinsight-timeout)
- [bot.chat(message)](#botchatmessage)
- [bot.whisper(username, message)](#botwhisperusername-message)
- [bot.chatAddPattern(pattern, chatType, description)](#botchataddpatternpattern-chattype-description)
Expand Down Expand Up @@ -1649,14 +1649,15 @@ End the connection with the server.

Gracefully disconnect from the server with the given reason (defaults to 'disconnect.quitting').

#### bot.tabComplete(str, [assumeCommand], [sendBlockInSight])
#### bot.tabComplete(str, [assumeCommand], [sendBlockInSight], [timeout])

This function returns a `Promise`, with `matches` as its argument upon completion.

Requests chat completion from the server.
* `str` - String to complete.
* `assumeCommand` - Field sent to server, defaults to false.
* `sendBlockInSight` - Field sent to server, defaults to true. Set this option to false if you want more performance.
* `timeout` - Timeout in milliseconds, after which the function will return an ampty array, defaults to 5000.

#### bot.chat(message)

Expand Down
3 changes: 2 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,8 @@ export interface Bot extends TypedEmitter<BotEvents> {
tabComplete: (
str: string,
assumeCommand?: boolean,
sendBlockInSight?: boolean
sendBlockInSight?: boolean,
timeout?: number
) => Promise<string[]>

chat: (message: string) => void
Expand Down
6 changes: 3 additions & 3 deletions lib/plugins/chat.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { once } = require('events')
const { onceWithCleanup } = require('../promise_utils')

const USERNAME_REGEX = '(?:\\(.{1,15}\\)|\\[.{1,15}\\]|.){0,5}?(\\w+)'
const LEGACY_VANILLA_CHAT_REGEX = new RegExp(`^${USERNAME_REGEX}\\s?[>:\\-»\\]\\)~]+\\s(.*)$`)
Expand Down Expand Up @@ -165,7 +165,7 @@ function inject (bot, options) {
})
}

async function tabComplete (text, assumeCommand = false, sendBlockInSight = true) {
async function tabComplete (text, assumeCommand = false, sendBlockInSight = true, timeout = 5000) {
let position

if (sendBlockInSight) {
Expand All @@ -182,7 +182,7 @@ function inject (bot, options) {
lookedAtBlock: position
})

const [packet] = await once(bot._client, 'tab_complete')
const [packet] = await onceWithCleanup(bot._client, 'tab_complete', { timeout })
return packet.matches
}

Expand Down

0 comments on commit 4231a16

Please sign in to comment.