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",
|
||||
"soundcloud-scraper": "^5.0.0",
|
||||
"spotify-url-info": "^2.2.3",
|
||||
"tiny-typed-emitter": "^2.0.3",
|
||||
"youtube-sr": "^4.1.4",
|
||||
"ytdl-core": "^4.8.2"
|
||||
},
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
import {
|
||||
AudioPlayer,
|
||||
AudioPlayerError,
|
||||
AudioPlayerStatus,
|
||||
AudioResource,
|
||||
createAudioPlayer,
|
||||
createAudioResource,
|
||||
|
@ -9,18 +11,26 @@ import {
|
|||
VoiceConnectionStatus
|
||||
} from "@discordjs/voice";
|
||||
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 audioPlayer: AudioPlayer;
|
||||
public connectPromise?: Promise<void>;
|
||||
|
||||
constructor(connection: VoiceConnection) {
|
||||
super();
|
||||
|
||||
this.voiceConnection = connection;
|
||||
this.audioPlayer = createAudioPlayer();
|
||||
|
||||
connection.subscribe(this.audioPlayer);
|
||||
|
||||
this.voiceConnection.on("stateChange", (_, newState) => {
|
||||
if (newState.status === VoiceConnectionStatus.Disconnected) {
|
||||
if (this.voiceConnection.reconnectAttempts < 5) {
|
||||
|
@ -48,6 +58,18 @@ class VoiceSubscription {
|
|||
.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
|
||||
* @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, {
|
||||
inputType: ops?.type ?? StreamType.Arbitrary,
|
||||
metadata: ops?.data,
|
||||
|
|
|
@ -3,7 +3,6 @@ import { entersState, joinVoiceChannel, VoiceConnection, VoiceConnectionStatus }
|
|||
import { VoiceSubscription } from "./VoiceSubscription";
|
||||
|
||||
class VoiceUtils {
|
||||
|
||||
constructor() {
|
||||
throw new Error("Cannot instantiate static class!");
|
||||
}
|
||||
|
@ -17,9 +16,10 @@ class VoiceUtils {
|
|||
public static async connect(
|
||||
channel: VoiceChannel | StageChannel,
|
||||
options?: {
|
||||
deaf?: boolean,
|
||||
maxTime?: number
|
||||
}): Promise<VoiceSubscription> {
|
||||
deaf?: boolean;
|
||||
maxTime?: number;
|
||||
}
|
||||
): Promise<VoiceSubscription> {
|
||||
let conn = joinVoiceChannel({
|
||||
guildId: channel.guild.id,
|
||||
channelId: channel.id,
|
||||
|
@ -43,7 +43,6 @@ class VoiceUtils {
|
|||
public static disconnect(connection: VoiceConnection) {
|
||||
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