From 70b42bb64a4f83da0da242569b9c7921c8d1e26f Mon Sep 17 00:00:00 2001 From: Skick Date: Thu, 23 Jun 2022 17:39:36 +0700 Subject: [PATCH] types(voice): bring back typed events (#8109) --- packages/voice/__tests__/SpeakingMap.test.ts | 4 +-- packages/voice/src/VoiceConnection.ts | 27 ++++++++++++++++++ packages/voice/src/audio/AudioPlayer.ts | 28 +++++++++++++++++-- .../voice/src/networking/VoiceUDPSocket.ts | 8 ++++++ packages/voice/src/receive/SSRCMap.ts | 7 +++++ packages/voice/src/receive/SpeakingMap.ts | 8 ++++++ 6 files changed, 77 insertions(+), 5 deletions(-) diff --git a/packages/voice/__tests__/SpeakingMap.test.ts b/packages/voice/__tests__/SpeakingMap.test.ts index 4f9a27462..92087ebd3 100644 --- a/packages/voice/__tests__/SpeakingMap.test.ts +++ b/packages/voice/__tests__/SpeakingMap.test.ts @@ -11,8 +11,8 @@ describe('SpeakingMap', () => { const starts: string[] = []; const ends: string[] = []; - speaking.on('start', (userId: string) => void starts.push(userId)); - speaking.on('end', (userId: string) => void ends.push(userId)); + speaking.on('start', (userId) => void starts.push(userId)); + speaking.on('end', (userId) => void ends.push(userId)); for (let i = 0; i < 10; i++) { speaking.onPacket(userId); diff --git a/packages/voice/src/VoiceConnection.ts b/packages/voice/src/VoiceConnection.ts index 78001934a..7794c31b9 100644 --- a/packages/voice/src/VoiceConnection.ts +++ b/packages/voice/src/VoiceConnection.ts @@ -1,3 +1,4 @@ +/* eslint-disable @typescript-eslint/method-signature-style */ import { EventEmitter } from 'node:events'; import type { GatewayVoiceServerUpdateDispatchData, GatewayVoiceStateUpdateDispatchData } from 'discord-api-types/v10'; import type { CreateVoiceConnectionOptions } from '.'; @@ -161,6 +162,32 @@ export type VoiceConnectionState = | VoiceConnectionReadyState | VoiceConnectionDestroyedState; +export interface VoiceConnection extends EventEmitter { + /** + * Emitted when there is an error emitted from the voice connection + * @event + */ + on(event: 'error', listener: (error: Error) => void): this; + /** + * Emitted debugging information about the voice connection + * @event + */ + on(event: 'debug', listener: (message: string) => void): this; + /** + * Emitted when the state of the voice connection changes + * @event + */ + on(event: 'stateChange', listener: (oldState: VoiceConnectionState, newState: VoiceConnectionState) => void): this; + /** + * Emitted when the state of the voice connection changes to a specific status + * @event + */ + on( + event: T, + listener: (oldState: VoiceConnectionState, newState: VoiceConnectionState & { status: T }) => void, + ): this; +} + /** * A connection to the voice server of a Guild, can be used to play audio in voice channels. */ diff --git a/packages/voice/src/audio/AudioPlayer.ts b/packages/voice/src/audio/AudioPlayer.ts index e5cd47f6b..f6a82fd19 100644 --- a/packages/voice/src/audio/AudioPlayer.ts +++ b/packages/voice/src/audio/AudioPlayer.ts @@ -1,4 +1,4 @@ -/* eslint-disable @typescript-eslint/prefer-ts-expect-error */ +/* eslint-disable @typescript-eslint/prefer-ts-expect-error, @typescript-eslint/method-signature-style */ import EventEmitter from 'node:events'; import { AudioPlayerError } from './AudioPlayerError'; import type { AudioResource } from './AudioResource'; @@ -154,10 +154,32 @@ export type AudioPlayerState = export interface AudioPlayer extends EventEmitter { /** * Emitted when there is an error emitted from the audio resource played by the audio player - * * @event */ - on: (event: 'error', listener: (error: AudioPlayerError) => void) => this; + on(event: 'error', listener: (error: AudioPlayerError) => void): this; + /** + * Emitted debugging information about the audio player + * @event + */ + on(event: 'debug', listener: (message: string) => void): this; + /** + * Emitted when the state of the audio player changes + * @event + */ + on(event: 'stateChange', listener: (oldState: AudioPlayerState, newState: AudioPlayerState) => void): this; + /** + * Emitted when the audio player is subscribed to a voice connection + * @event + */ + on(event: 'subscribe' | 'unsubscribe', listener: (subscription: PlayerSubscription) => void): this; + /** + * Emitted when the status of state changes to a specific status + * @event + */ + on( + event: T, + listener: (oldState: AudioPlayerState, newState: AudioPlayerState & { status: T }) => void, + ): this; } /** diff --git a/packages/voice/src/networking/VoiceUDPSocket.ts b/packages/voice/src/networking/VoiceUDPSocket.ts index 82d8589b0..d42d962ee 100644 --- a/packages/voice/src/networking/VoiceUDPSocket.ts +++ b/packages/voice/src/networking/VoiceUDPSocket.ts @@ -1,3 +1,4 @@ +/* eslint-disable @typescript-eslint/method-signature-style */ import { createSocket, Socket } from 'node:dgram'; import { EventEmitter } from 'node:events'; import { isIPv4 } from 'node:net'; @@ -50,6 +51,13 @@ const KEEP_ALIVE_LIMIT = 12; */ const MAX_COUNTER_VALUE = 2 ** 32 - 1; +export interface VoiceUDPSocket extends EventEmitter { + on(event: 'error', listener: (error: Error) => void): this; + on(event: 'close', listener: () => void): this; + on(event: 'debug', listener: (message: string) => void): this; + on(event: 'message', listener: (message: Buffer) => void): this; +} + /** * Manages the UDP networking for a voice connection. */ diff --git a/packages/voice/src/receive/SSRCMap.ts b/packages/voice/src/receive/SSRCMap.ts index 0d0fde291..4933e4fa3 100644 --- a/packages/voice/src/receive/SSRCMap.ts +++ b/packages/voice/src/receive/SSRCMap.ts @@ -1,3 +1,4 @@ +/* eslint-disable @typescript-eslint/method-signature-style */ import { EventEmitter } from 'node:events'; /** @@ -21,6 +22,12 @@ export interface VoiceUserData { userId: string; } +export interface SSRCMap extends EventEmitter { + on(event: 'create', listener: (newData: VoiceUserData) => void): this; + on(event: 'update', listener: (oldData: VoiceUserData | undefined, newData: VoiceUserData) => void): this; + on(event: 'delete', listener: (deletedData: VoiceUserData) => void): this; +} + /** * Maps audio SSRCs to data of users in voice connections. */ diff --git a/packages/voice/src/receive/SpeakingMap.ts b/packages/voice/src/receive/SpeakingMap.ts index faa2864ca..21301c377 100644 --- a/packages/voice/src/receive/SpeakingMap.ts +++ b/packages/voice/src/receive/SpeakingMap.ts @@ -1,5 +1,13 @@ import { EventEmitter } from 'node:events'; +export interface SpeakingMap extends EventEmitter { + /** + * Emitted when a user starts/stops speaking. + * @event + */ + on: (event: 'start' | 'end', listener: (userId: string) => void) => this; +} + /** * Tracks the speaking states of users in a voice channel. */