voice events
This commit is contained in:
parent
875a554b63
commit
a0d3859230
7 changed files with 37 additions and 15 deletions
|
@ -54,6 +54,7 @@
|
||||||
"discord-ytdl-core": "^5.0.3",
|
"discord-ytdl-core": "^5.0.3",
|
||||||
"soundcloud-scraper": "^5.0.0",
|
"soundcloud-scraper": "^5.0.0",
|
||||||
"spotify-url-info": "^2.2.3",
|
"spotify-url-info": "^2.2.3",
|
||||||
|
"tiny-typed-emitter": "^2.0.3",
|
||||||
"youtube-sr": "^4.1.4",
|
"youtube-sr": "^4.1.4",
|
||||||
"ytdl-core": "^4.8.2"
|
"ytdl-core": "^4.8.2"
|
||||||
},
|
},
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
import {
|
import {
|
||||||
AudioPlayer,
|
AudioPlayer,
|
||||||
|
AudioPlayerError,
|
||||||
|
AudioPlayerStatus,
|
||||||
AudioResource,
|
AudioResource,
|
||||||
createAudioPlayer,
|
createAudioPlayer,
|
||||||
createAudioResource,
|
createAudioResource,
|
||||||
|
@ -9,18 +11,26 @@ import {
|
||||||
VoiceConnectionStatus
|
VoiceConnectionStatus
|
||||||
} from "@discordjs/voice";
|
} from "@discordjs/voice";
|
||||||
import { Duplex, Readable } from "stream";
|
import { Duplex, Readable } from "stream";
|
||||||
|
import { TypedEmitter as EventEmitter } from "tiny-typed-emitter";
|
||||||
|
|
||||||
class VoiceSubscription {
|
export interface VoiceEvents {
|
||||||
|
error: (error: AudioPlayerError) => any;
|
||||||
|
debug: (message: string) => any;
|
||||||
|
start: () => any;
|
||||||
|
finish: () => any;
|
||||||
|
}
|
||||||
|
|
||||||
|
class VoiceSubscription extends EventEmitter<VoiceEvents> {
|
||||||
public readonly voiceConnection: VoiceConnection;
|
public readonly voiceConnection: VoiceConnection;
|
||||||
public readonly audioPlayer: AudioPlayer;
|
public readonly audioPlayer: AudioPlayer;
|
||||||
public connectPromise?: Promise<void>;
|
public connectPromise?: Promise<void>;
|
||||||
|
|
||||||
constructor(connection: VoiceConnection) {
|
constructor(connection: VoiceConnection) {
|
||||||
|
super();
|
||||||
|
|
||||||
this.voiceConnection = connection;
|
this.voiceConnection = connection;
|
||||||
this.audioPlayer = createAudioPlayer();
|
this.audioPlayer = createAudioPlayer();
|
||||||
|
|
||||||
connection.subscribe(this.audioPlayer);
|
|
||||||
|
|
||||||
this.voiceConnection.on("stateChange", (_, newState) => {
|
this.voiceConnection.on("stateChange", (_, newState) => {
|
||||||
if (newState.status === VoiceConnectionStatus.Disconnected) {
|
if (newState.status === VoiceConnectionStatus.Disconnected) {
|
||||||
if (this.voiceConnection.reconnectAttempts < 5) {
|
if (this.voiceConnection.reconnectAttempts < 5) {
|
||||||
|
@ -48,6 +58,18 @@ class VoiceSubscription {
|
||||||
.finally(() => (this.connectPromise = undefined));
|
.finally(() => (this.connectPromise = undefined));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
this.audioPlayer.on("stateChange", (oldState, newState) => {
|
||||||
|
if (newState.status === AudioPlayerStatus.Idle && oldState.status !== AudioPlayerStatus.Idle) {
|
||||||
|
void this.emit("finish");
|
||||||
|
} else if (newState.status === AudioPlayerStatus.Playing) {
|
||||||
|
void this.emit("start");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
this.audioPlayer.on("debug", (m) => void this.emit("debug", m));
|
||||||
|
this.audioPlayer.on("error", (error) => void this.emit("error", error));
|
||||||
|
this.voiceConnection.subscribe(this.audioPlayer);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -56,7 +78,7 @@ class VoiceSubscription {
|
||||||
* @param {({type?:StreamType;data?:any;inlineVolume?:boolean})} [ops] Options
|
* @param {({type?:StreamType;data?:any;inlineVolume?:boolean})} [ops] Options
|
||||||
* @returns {AudioResource}
|
* @returns {AudioResource}
|
||||||
*/
|
*/
|
||||||
createStream(src: Readable | Duplex | string, ops?: { type?: StreamType, data?: any, inlineVolume?: boolean }) {
|
createStream(src: Readable | Duplex | string, ops?: { type?: StreamType; data?: any; inlineVolume?: boolean }) {
|
||||||
return createAudioResource(src, {
|
return createAudioResource(src, {
|
||||||
inputType: ops?.type ?? StreamType.Arbitrary,
|
inputType: ops?.type ?? StreamType.Arbitrary,
|
||||||
metadata: ops?.data,
|
metadata: ops?.data,
|
||||||
|
|
|
@ -3,7 +3,6 @@ import { entersState, joinVoiceChannel, VoiceConnection, VoiceConnectionStatus }
|
||||||
import { VoiceSubscription } from "./VoiceSubscription";
|
import { VoiceSubscription } from "./VoiceSubscription";
|
||||||
|
|
||||||
class VoiceUtils {
|
class VoiceUtils {
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
throw new Error("Cannot instantiate static class!");
|
throw new Error("Cannot instantiate static class!");
|
||||||
}
|
}
|
||||||
|
@ -17,9 +16,10 @@ class VoiceUtils {
|
||||||
public static async connect(
|
public static async connect(
|
||||||
channel: VoiceChannel | StageChannel,
|
channel: VoiceChannel | StageChannel,
|
||||||
options?: {
|
options?: {
|
||||||
deaf?: boolean,
|
deaf?: boolean;
|
||||||
maxTime?: number
|
maxTime?: number;
|
||||||
}): Promise<VoiceSubscription> {
|
}
|
||||||
|
): Promise<VoiceSubscription> {
|
||||||
let conn = joinVoiceChannel({
|
let conn = joinVoiceChannel({
|
||||||
guildId: channel.guild.id,
|
guildId: channel.guild.id,
|
||||||
channelId: channel.id,
|
channelId: channel.id,
|
||||||
|
@ -30,7 +30,7 @@ class VoiceUtils {
|
||||||
try {
|
try {
|
||||||
conn = await entersState(conn, VoiceConnectionStatus.Ready, options?.maxTime ?? 20000);
|
conn = await entersState(conn, VoiceConnectionStatus.Ready, options?.maxTime ?? 20000);
|
||||||
return new VoiceSubscription(conn);
|
return new VoiceSubscription(conn);
|
||||||
} catch(err) {
|
} catch (err) {
|
||||||
conn.destroy();
|
conn.destroy();
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
|
@ -43,7 +43,6 @@ class VoiceUtils {
|
||||||
public static disconnect(connection: VoiceConnection) {
|
public static disconnect(connection: VoiceConnection) {
|
||||||
connection.destroy();
|
connection.destroy();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export { VoiceUtils }
|
export { VoiceUtils };
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
export { }
|
export {};
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
export { }
|
export {};
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
export { }
|
export {};
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
export { }
|
export {};
|
||||||
|
|
Loading…
Reference in a new issue