mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-13 10:03:31 +01:00
refactor: use eslint-config-neon for packages. (#8579)
Co-authored-by: Noel <buechler.noel@outlook.com>
This commit is contained in:
@@ -1,40 +1,39 @@
|
||||
/* eslint-disable consistent-return */
|
||||
/* eslint-disable @typescript-eslint/unbound-method */
|
||||
/* eslint-disable jsdoc/check-param-names */
|
||||
/* eslint-disable @typescript-eslint/method-signature-style */
|
||||
import type { Buffer } from 'node:buffer';
|
||||
import { EventEmitter } from 'node:events';
|
||||
import type { GatewayVoiceServerUpdateDispatchData, GatewayVoiceStateUpdateDispatchData } from 'discord-api-types/v10';
|
||||
import type { CreateVoiceConnectionOptions } from '.';
|
||||
import type { JoinConfig } from './DataStore';
|
||||
import {
|
||||
getVoiceConnection,
|
||||
createJoinVoiceChannelPayload,
|
||||
trackVoiceConnection,
|
||||
JoinConfig,
|
||||
untrackVoiceConnection,
|
||||
} from './DataStore';
|
||||
import type { AudioPlayer } from './audio/AudioPlayer';
|
||||
import type { PlayerSubscription } from './audio/PlayerSubscription';
|
||||
import type { VoiceWebSocket, VoiceUDPSocket } from './networking';
|
||||
import { Networking, NetworkingState, NetworkingStatusCode } from './networking/Networking';
|
||||
import { VoiceReceiver } from './receive';
|
||||
import { Networking, NetworkingStatusCode, type NetworkingState } from './networking/Networking';
|
||||
import { VoiceReceiver } from './receive/index';
|
||||
import type { DiscordGatewayAdapterImplementerMethods } from './util/adapter';
|
||||
import { noop } from './util/util';
|
||||
import type { CreateVoiceConnectionOptions } from './index';
|
||||
|
||||
/**
|
||||
* The various status codes a voice connection can hold at any one time.
|
||||
*/
|
||||
export enum VoiceConnectionStatus {
|
||||
/**
|
||||
* Sending a packet to the main Discord gateway to indicate we want to change our voice state.
|
||||
*/
|
||||
Signalling = 'signalling',
|
||||
|
||||
/**
|
||||
* The `VOICE_SERVER_UPDATE` and `VOICE_STATE_UPDATE` packets have been received, now attempting to establish a voice connection.
|
||||
*/
|
||||
Connecting = 'connecting',
|
||||
|
||||
/**
|
||||
* A voice connection has been established, and is ready to be used.
|
||||
* The voice connection has been destroyed and untracked, it cannot be reused.
|
||||
*/
|
||||
Ready = 'ready',
|
||||
Destroyed = 'destroyed',
|
||||
|
||||
/**
|
||||
* The voice connection has either been severed or not established.
|
||||
@@ -42,9 +41,14 @@ export enum VoiceConnectionStatus {
|
||||
Disconnected = 'disconnected',
|
||||
|
||||
/**
|
||||
* The voice connection has been destroyed and untracked, it cannot be reused.
|
||||
* A voice connection has been established, and is ready to be used.
|
||||
*/
|
||||
Destroyed = 'destroyed',
|
||||
Ready = 'ready',
|
||||
|
||||
/**
|
||||
* Sending a packet to the main Discord gateway to indicate we want to change our voice state.
|
||||
*/
|
||||
Signalling = 'signalling',
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -52,9 +56,9 @@ export enum VoiceConnectionStatus {
|
||||
* VOICE_STATE_UPDATE packet from Discord, provided by the adapter.
|
||||
*/
|
||||
export interface VoiceConnectionSignallingState {
|
||||
adapter: DiscordGatewayAdapterImplementerMethods;
|
||||
status: VoiceConnectionStatus.Signalling;
|
||||
subscription?: PlayerSubscription | undefined;
|
||||
adapter: DiscordGatewayAdapterImplementerMethods;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -87,9 +91,9 @@ export enum VoiceConnectionDisconnectReason {
|
||||
* it attempting to connect. You can manually attempt to reconnect using VoiceConnection#reconnect.
|
||||
*/
|
||||
export interface VoiceConnectionDisconnectedBaseState {
|
||||
adapter: DiscordGatewayAdapterImplementerMethods;
|
||||
status: VoiceConnectionStatus.Disconnected;
|
||||
subscription?: PlayerSubscription | undefined;
|
||||
adapter: DiscordGatewayAdapterImplementerMethods;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -105,12 +109,12 @@ export interface VoiceConnectionDisconnectedOtherState extends VoiceConnectionDi
|
||||
* You can manually attempt to reconnect using VoiceConnection#reconnect.
|
||||
*/
|
||||
export interface VoiceConnectionDisconnectedWebSocketState extends VoiceConnectionDisconnectedBaseState {
|
||||
reason: VoiceConnectionDisconnectReason.WebSocketClose;
|
||||
|
||||
/**
|
||||
* The close code of the WebSocket connection to the Discord voice server.
|
||||
*/
|
||||
closeCode: number;
|
||||
|
||||
reason: VoiceConnectionDisconnectReason.WebSocketClose;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -126,10 +130,10 @@ export type VoiceConnectionDisconnectedState =
|
||||
* voice server.
|
||||
*/
|
||||
export interface VoiceConnectionConnectingState {
|
||||
status: VoiceConnectionStatus.Connecting;
|
||||
networking: Networking;
|
||||
subscription?: PlayerSubscription | undefined;
|
||||
adapter: DiscordGatewayAdapterImplementerMethods;
|
||||
networking: Networking;
|
||||
status: VoiceConnectionStatus.Connecting;
|
||||
subscription?: PlayerSubscription | undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -137,10 +141,10 @@ export interface VoiceConnectionConnectingState {
|
||||
* voice server.
|
||||
*/
|
||||
export interface VoiceConnectionReadyState {
|
||||
status: VoiceConnectionStatus.Ready;
|
||||
networking: Networking;
|
||||
subscription?: PlayerSubscription | undefined;
|
||||
adapter: DiscordGatewayAdapterImplementerMethods;
|
||||
networking: Networking;
|
||||
status: VoiceConnectionStatus.Ready;
|
||||
subscription?: PlayerSubscription | undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -156,30 +160,34 @@ export interface VoiceConnectionDestroyedState {
|
||||
* The various states that a voice connection can be in.
|
||||
*/
|
||||
export type VoiceConnectionState =
|
||||
| VoiceConnectionSignallingState
|
||||
| VoiceConnectionDisconnectedState
|
||||
| VoiceConnectionConnectingState
|
||||
| VoiceConnectionDestroyedState
|
||||
| VoiceConnectionDisconnectedState
|
||||
| VoiceConnectionReadyState
|
||||
| VoiceConnectionDestroyedState;
|
||||
| VoiceConnectionSignallingState;
|
||||
|
||||
export interface VoiceConnection extends EventEmitter {
|
||||
/**
|
||||
* Emitted when there is an error emitted from the voice connection
|
||||
*
|
||||
* @eventProperty
|
||||
*/
|
||||
on(event: 'error', listener: (error: Error) => void): this;
|
||||
/**
|
||||
* Emitted debugging information about the voice connection
|
||||
*
|
||||
* @eventProperty
|
||||
*/
|
||||
on(event: 'debug', listener: (message: string) => void): this;
|
||||
/**
|
||||
* Emitted when the state of the voice connection changes
|
||||
*
|
||||
* @eventProperty
|
||||
*/
|
||||
on(event: 'stateChange', listener: (oldState: VoiceConnectionState, newState: VoiceConnectionState) => void): this;
|
||||
/**
|
||||
* Emitted when the state of the voice connection changes to a specific status
|
||||
*
|
||||
* @eventProperty
|
||||
*/
|
||||
on<T extends VoiceConnectionStatus>(
|
||||
@@ -228,7 +236,7 @@ export class VoiceConnection extends EventEmitter {
|
||||
/**
|
||||
* The debug logger function, if debugging is enabled.
|
||||
*/
|
||||
private readonly debug: null | ((message: string) => void);
|
||||
private readonly debug: ((message: string) => void) | null;
|
||||
|
||||
/**
|
||||
* Creates a new voice connection.
|
||||
@@ -292,6 +300,7 @@ export class VoiceConnection extends EventEmitter {
|
||||
oldNetworking.off('stateChange', this.onNetworkingStateChange);
|
||||
oldNetworking.destroy();
|
||||
}
|
||||
|
||||
if (newNetworking) this.updateReceiveBindings(newNetworking.state, oldNetworking?.state);
|
||||
}
|
||||
|
||||
@@ -362,6 +371,7 @@ export class VoiceConnection extends EventEmitter {
|
||||
/**
|
||||
* Called when the networking state changes, and the new ws/udp packet/message handlers need to be rebound
|
||||
* to the new instances.
|
||||
*
|
||||
* @param newState - The new networking state
|
||||
* @param oldState - The old networking state, if there is one
|
||||
*/
|
||||
@@ -432,13 +442,12 @@ export class VoiceConnection extends EventEmitter {
|
||||
* If the close code was anything other than 4014, it is likely that the closing was not intended, and so the
|
||||
* VoiceConnection will signal to Discord that it would like to rejoin the channel. This automatically attempts
|
||||
* to re-establish the connection. This would be seen as a transition from the Ready state to the Signalling state.
|
||||
*
|
||||
* @param code - The close code
|
||||
*/
|
||||
private onNetworkingClose(code: number) {
|
||||
if (this.state.status === VoiceConnectionStatus.Destroyed) return;
|
||||
// If networking closes, try to connect to the voice channel again.
|
||||
if (code === 4014) {
|
||||
if (code === 4_014) {
|
||||
// Disconnected - networking is already destroyed here
|
||||
this.state = {
|
||||
...this.state,
|
||||
@@ -548,12 +557,15 @@ export class VoiceConnection extends EventEmitter {
|
||||
if (this.state.status === VoiceConnectionStatus.Destroyed) {
|
||||
throw new Error('Cannot destroy VoiceConnection - it has already been destroyed');
|
||||
}
|
||||
|
||||
if (getVoiceConnection(this.joinConfig.guildId, this.joinConfig.group) === this) {
|
||||
untrackVoiceConnection(this);
|
||||
}
|
||||
|
||||
if (adapterAvailable) {
|
||||
this.state.adapter.sendPayload(createJoinVoiceChannelPayload({ ...this.joinConfig, channelId: null }));
|
||||
}
|
||||
|
||||
this.state = {
|
||||
status: VoiceConnectionStatus.Destroyed,
|
||||
};
|
||||
@@ -571,6 +583,7 @@ export class VoiceConnection extends EventEmitter {
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
this.joinConfig.channelId = null;
|
||||
if (!this.state.adapter.sendPayload(createJoinVoiceChannelPayload(this.joinConfig))) {
|
||||
this.state = {
|
||||
@@ -581,6 +594,7 @@ export class VoiceConnection extends EventEmitter {
|
||||
};
|
||||
return false;
|
||||
}
|
||||
|
||||
this.state = {
|
||||
adapter: this.state.adapter,
|
||||
reason: VoiceConnectionDisconnectReason.Manual,
|
||||
@@ -599,7 +613,7 @@ export class VoiceConnection extends EventEmitter {
|
||||
*
|
||||
* A state transition from Disconnected to Signalling will be observed when this is called.
|
||||
*/
|
||||
public rejoin(joinConfig?: Omit<JoinConfig, 'guildId' | 'group'>) {
|
||||
public rejoin(joinConfig?: Omit<JoinConfig, 'group' | 'guildId'>) {
|
||||
if (this.state.status === VoiceConnectionStatus.Destroyed) {
|
||||
return false;
|
||||
}
|
||||
@@ -615,6 +629,7 @@ export class VoiceConnection extends EventEmitter {
|
||||
status: VoiceConnectionStatus.Signalling,
|
||||
};
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -635,6 +650,7 @@ export class VoiceConnection extends EventEmitter {
|
||||
*/
|
||||
public setSpeaking(enabled: boolean) {
|
||||
if (this.state.status !== VoiceConnectionStatus.Ready) return false;
|
||||
// eslint-disable-next-line @typescript-eslint/no-confusing-void-expression
|
||||
return this.state.networking.setSpeaking(enabled);
|
||||
}
|
||||
|
||||
@@ -642,7 +658,6 @@ export class VoiceConnection extends EventEmitter {
|
||||
* Subscribes to an audio player, allowing the player to play audio on this voice connection.
|
||||
*
|
||||
* @param player - The audio player to subscribe to
|
||||
*
|
||||
* @returns The created subscription
|
||||
*/
|
||||
public subscribe(player: AudioPlayer) {
|
||||
@@ -677,6 +692,7 @@ export class VoiceConnection extends EventEmitter {
|
||||
udp: this.state.networking.state.udp.ping,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
ws: undefined,
|
||||
udp: undefined,
|
||||
@@ -721,19 +737,22 @@ export function createVoiceConnection(joinConfig: JoinConfig, options: CreateVoi
|
||||
reason: VoiceConnectionDisconnectReason.AdapterUnavailable,
|
||||
};
|
||||
}
|
||||
|
||||
return existing;
|
||||
}
|
||||
|
||||
const voiceConnection = new VoiceConnection(joinConfig, options);
|
||||
trackVoiceConnection(voiceConnection);
|
||||
if (voiceConnection.state.status !== VoiceConnectionStatus.Destroyed) {
|
||||
if (!voiceConnection.state.adapter.sendPayload(payload)) {
|
||||
voiceConnection.state = {
|
||||
...voiceConnection.state,
|
||||
status: VoiceConnectionStatus.Disconnected,
|
||||
reason: VoiceConnectionDisconnectReason.AdapterUnavailable,
|
||||
};
|
||||
}
|
||||
if (
|
||||
voiceConnection.state.status !== VoiceConnectionStatus.Destroyed &&
|
||||
!voiceConnection.state.adapter.sendPayload(payload)
|
||||
) {
|
||||
voiceConnection.state = {
|
||||
...voiceConnection.state,
|
||||
status: VoiceConnectionStatus.Disconnected,
|
||||
reason: VoiceConnectionDisconnectReason.AdapterUnavailable,
|
||||
};
|
||||
}
|
||||
|
||||
return voiceConnection;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user