Skip to content

Commit 4231a16

Browse files
authored
Timeout for bot.tabComplete() (#3293)
* 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
1 parent 210785e commit 4231a16

File tree

3 files changed

+9
-7
lines changed

3 files changed

+9
-7
lines changed

docs/api.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@
225225
- ["blockBreakProgressEnd" (block, entity)](#blockbreakprogressend-block-entity)
226226
- ["diggingCompleted" (block)](#diggingcompleted-block)
227227
- ["diggingAborted" (block)](#diggingaborted-block)
228-
- ["usedfirework"](#usedfirework)
228+
- ["usedFirework" (fireworkEntityId)](#usedfirework-fireworkentityid)
229229
- ["move"](#move)
230230
- ["forcedMove"](#forcedmove)
231231
- ["mount"](#mount)
@@ -270,7 +270,7 @@
270270
- [Methods](#methods)
271271
- [bot.end(reason)](#botendreason)
272272
- [bot.quit(reason)](#botquitreason)
273-
- [bot.tabComplete(str, [assumeCommand], [sendBlockInSight])](#bottabcompletestr-assumecommand-sendblockinsight)
273+
- [bot.tabComplete(str, [assumeCommand], [sendBlockInSight], [timeout])](#bottabcompletestr-assumecommand-sendblockinsight-timeout)
274274
- [bot.chat(message)](#botchatmessage)
275275
- [bot.whisper(username, message)](#botwhisperusername-message)
276276
- [bot.chatAddPattern(pattern, chatType, description)](#botchataddpatternpattern-chattype-description)
@@ -1649,14 +1649,15 @@ End the connection with the server.
16491649

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

1652-
#### bot.tabComplete(str, [assumeCommand], [sendBlockInSight])
1652+
#### bot.tabComplete(str, [assumeCommand], [sendBlockInSight], [timeout])
16531653

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

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

16611662
#### bot.chat(message)
16621663

index.d.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,8 @@ export interface Bot extends TypedEmitter<BotEvents> {
245245
tabComplete: (
246246
str: string,
247247
assumeCommand?: boolean,
248-
sendBlockInSight?: boolean
248+
sendBlockInSight?: boolean,
249+
timeout?: number
249250
) => Promise<string[]>
250251

251252
chat: (message: string) => void

lib/plugins/chat.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { once } = require('events')
1+
const { onceWithCleanup } = require('../promise_utils')
22

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

168-
async function tabComplete (text, assumeCommand = false, sendBlockInSight = true) {
168+
async function tabComplete (text, assumeCommand = false, sendBlockInSight = true, timeout = 5000) {
169169
let position
170170

171171
if (sendBlockInSight) {
@@ -182,7 +182,7 @@ function inject (bot, options) {
182182
lookedAtBlock: position
183183
})
184184

185-
const [packet] = await once(bot._client, 'tab_complete')
185+
const [packet] = await onceWithCleanup(bot._client, 'tab_complete', { timeout })
186186
return packet.matches
187187
}
188188

0 commit comments

Comments
 (0)