Skip to content

Commit 54bd9d8

Browse files
committed
fix: enhance waitEvent method with timeout handling#150
1 parent 954c053 commit 54bd9d8

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

src/playback/player.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,25 @@ export class Player {
8383
player: this.toJSON()
8484
})
8585

86-
this.waitEvent = (event, filter) =>
87-
new Promise((resolve) => {
86+
this.waitEvent = (event, filter, timeout = 15000) =>
87+
new Promise((resolve, reject) => {
8888
const handler = (_, payload) => {
8989
if (!filter || filter(payload)) {
90-
this.connection.off(event, handler)
90+
clearTimeout(timeoutId)
91+
this.connection?.off(event, handler)
9192
resolve(payload)
9293
}
9394
}
95+
96+
const timeoutId = setTimeout(() => {
97+
this.connection?.off(event, handler)
98+
reject(
99+
new Error(
100+
`Event ${event} timed out after ${timeout}ms for guild ${this.guildId}`
101+
)
102+
)
103+
}, timeout)
104+
94105
this.connection.on(event, handler)
95106
})
96107

0 commit comments

Comments
 (0)