Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/Typings/Main/Player.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { EmptyObject, JsonObject } from "../Utility";
import type { Exception, PlayerState, TrackEndReason } from "../API";
import type { CreateNodeOptions, NodeEventMap } from "../Node";
import type { QueueDestroyReasons, VoiceDestroyReasons } from "../Voice";
import type { CreateQueueOptions } from "../Queue";

import type { Node } from "../../Node";
Expand All @@ -23,12 +24,12 @@ export interface PlayerEventMap extends Record<string & {}, any> {
voiceConnect: [voice: VoiceState];
voiceClose: [voice: VoiceState, code: number, reason: string, byRemote: boolean];
voiceChange: [voice: VoiceState, previousNode: Node, wasPlaying: boolean];
voiceDestroy: [voice: VoiceState, reason: string];
voiceDestroy: [voice: VoiceState, reason: VoiceDestroyReasons];

queueCreate: [queue: Queue];
queueUpdate: [queue: Queue, state: PlayerState];
queueFinish: [queue: Queue];
queueDestroy: [queue: Queue, reason: string];
queueDestroy: [queue: Queue, reason: QueueDestroyReasons];

trackStart: [queue: Queue, track: Track];
trackStuck: [queue: Queue, track: Track, thresholdMs: number];
Expand Down
41 changes: 40 additions & 1 deletion src/Typings/Voice/VoiceManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ export interface CommonDispatchPayloadInfo {
*/
export interface ClientReadyPayload extends CommonDispatchPayloadInfo {
t: "READY";
d: { user: { id: string } };
d: {
user: {
id: string;
};
};
}

/**
Expand Down Expand Up @@ -48,6 +52,41 @@ export interface VoiceServerUpdatePayload extends CommonDispatchPayloadInfo {
};
}

/**
* Discord guild delete payload
*/
export interface GuildDeletePayload extends CommonDispatchPayloadInfo {
t: "GUILD_DELETE";
d: {
id: string;
unavailable?: true;
};
}

/**
* Discord channel delete payload (partial, essential only)
*/
export interface ChannelDeletePayload extends CommonDispatchPayloadInfo {
t: "CHANNEL_DELETE";
d: {
id: string;
guild_id?: string;
};
}

/**
* Discord dispatch payload
*/
export type DiscordDispatchPayload =
| ClientReadyPayload
| VoiceStateUpdatePayload
| VoiceServerUpdatePayload
| GuildDeletePayload
| ChannelDeletePayload;

export type VoiceDestroyReasons = ("destroyed" | "guildDeleted" | "channelDeleted") | (string & {});
export type QueueDestroyReasons = VoiceDestroyReasons;

/**
* VoiceManager intrinsic data
*/
Expand Down
24 changes: 23 additions & 1 deletion src/Voice/VoiceManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ import { isString, noop } from "../Functions";
import { VoiceRegion, VoiceState } from ".";

import type {
ChannelDeletePayload,
ClientReadyPayload,
ConnectOptions,
CreateQueueOptions,
DiscordDispatchPayload,
GuildDeletePayload,
VoiceDestroyReasons,
VoiceServerUpdatePayload,
VoiceStateInfo,
VoiceStateUpdatePayload,
Expand Down Expand Up @@ -200,7 +204,7 @@ export class VoiceManager implements Partial<Map<string, VoiceState>> {
* @param payload 'Dispatch' payload received from Discord
*/
handleDispatch(payload: unknown): void;
handleDispatch(payload: ClientReadyPayload | VoiceStateUpdatePayload | VoiceServerUpdatePayload) {
handleDispatch(payload: DiscordDispatchPayload) {
if (payload.op !== 0) return;
switch (payload.t) {
case "VOICE_STATE_UPDATE":
Expand All @@ -209,6 +213,12 @@ export class VoiceManager implements Partial<Map<string, VoiceState>> {
case "VOICE_SERVER_UPDATE":
this.#onServerUpdate(payload.d);
return;
case "GUILD_DELETE":
this.#onGuildDelete(payload.d);
return;
case "CHANNEL_DELETE":
this.#onChannelDelete(payload.d);
return;
case "READY":
this.#onClientReady(payload.d);
return;
Expand All @@ -220,6 +230,18 @@ export class VoiceManager implements Partial<Map<string, VoiceState>> {
return this.#player.init(data.user.id);
}

async #onGuildDelete(data: GuildDeletePayload["d"]) {
if (data.unavailable) return;
return this.destroy(data.id, "guildDeleted" satisfies VoiceDestroyReasons);
}

async #onChannelDelete(data: ChannelDeletePayload["d"]) {
if (!data.guild_id) return;
const voice = this.#voices.get(data.guild_id);
if (voice?.channelId !== data.id) return;
return voice.destroy("channelDeleted" satisfies VoiceDestroyReasons);
}

#onStateUpdate(data: VoiceStateUpdatePayload["d"]) {
if (!data.guild_id || data.user_id !== this.#player.clientId) return;
if (data.channel_id === null) {
Expand Down