File size: 840 Bytes
36ba3ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { Bot } from 'mineflayer'

class Subscription {
  constructor (readonly eventName: string, readonly callback: Function) {}
}

export class TemporarySubscriber {
  private readonly subscriptions: Subscription[] = []

  constructor (readonly bot: Bot) {}

  /**
   * Adds a new temporary event listener to the bot.
   *
   * @param event - The event to subscribe to.
   * @param callback - The function to execute.
   */
  subscribeTo (event: string, callback: Function): void {
    this.subscriptions.push(new Subscription(event, callback))

    // @ts-expect-error
    this.bot.on(event, callback)
  }

  /**
   * Removes all attached event listeners from the bot.
   */
  cleanup (): void {
    for (const sub of this.subscriptions) {
      // @ts-expect-error
      this.bot.removeListener(sub.eventName, sub.callback)
    }
  }
}