discord-player-play-dl/src/VoiceInterface/VoiceUtils.ts

89 lines
2.7 KiB
TypeScript
Raw Normal View History

2021-06-11 16:50:43 +05:00
import { VoiceChannel, StageChannel, Collection, Snowflake } from "discord.js";
2021-06-23 00:11:50 +05:00
import { DiscordGatewayAdapterCreator, entersState, joinVoiceChannel, VoiceConnection, VoiceConnectionStatus } from "@discordjs/voice";
2021-06-25 12:24:53 +05:00
import { StreamDispatcher } from "./StreamDispatcher";
2021-06-11 14:06:51 +05:00
class VoiceUtils {
2021-06-20 19:22:09 +05:00
public cache: Collection<Snowflake, StreamDispatcher>;
2021-06-11 14:06:51 +05:00
/**
2021-06-20 19:22:09 +05:00
* The voice utils
2021-06-21 10:34:49 +05:00
* @private
2021-06-20 19:22:09 +05:00
*/
constructor() {
/**
* The cache where voice utils stores stream managers
* @type {Collection<Snowflake, StreamDispatcher>}
*/
this.cache = new Collection<Snowflake, StreamDispatcher>();
}
/**
* Joins a voice channel, creating basic stream dispatch manager
2021-06-11 14:06:51 +05:00
* @param {StageChannel|VoiceChannel} channel The voice channel
2021-06-20 19:22:09 +05:00
* @param {object} [options={}] Join options
* @returns {Promise<StreamDispatcher>}
2021-06-11 14:06:51 +05:00
*/
2021-06-11 16:50:43 +05:00
public async connect(
2021-06-11 14:06:51 +05:00
channel: VoiceChannel | StageChannel,
options?: {
2021-06-11 14:17:42 +05:00
deaf?: boolean;
maxTime?: number;
}
2021-06-11 23:19:52 +05:00
): Promise<StreamDispatcher> {
2021-06-14 19:44:15 +05:00
const conn = await this.join(channel, options);
const sub = new StreamDispatcher(conn, channel, options.maxTime);
2021-06-14 19:44:15 +05:00
this.cache.set(channel.guild.id, sub);
return sub;
}
2021-06-20 19:22:09 +05:00
/**
* Joins a voice channel
* @param {StageChannel|VoiceChannel} [channel] The voice/stage channel to join
* @param {object} [options={}] Join options
* @returns {VoiceConnection}
*/
2021-06-14 19:44:15 +05:00
public async join(
channel: VoiceChannel | StageChannel,
options?: {
deaf?: boolean;
maxTime?: number;
}
) {
2021-06-11 14:06:51 +05:00
let conn = joinVoiceChannel({
guildId: channel.guild.id,
channelId: channel.id,
adapterCreator: channel.guild.voiceAdapterCreator as unknown as DiscordGatewayAdapterCreator,
2021-06-14 19:44:15 +05:00
selfDeaf: Boolean(options.deaf)
2021-06-11 14:06:51 +05:00
});
try {
conn = await entersState(conn, VoiceConnectionStatus.Ready, options?.maxTime ?? 20000);
2021-06-14 19:44:15 +05:00
return conn;
2021-06-11 14:17:42 +05:00
} catch (err) {
2021-06-11 14:06:51 +05:00
conn.destroy();
throw err;
}
}
/**
* Disconnects voice connection
* @param {VoiceConnection} connection The voice connection
2021-06-20 19:22:09 +05:00
* @returns {void}
2021-06-11 14:06:51 +05:00
*/
2021-06-11 23:19:52 +05:00
public disconnect(connection: VoiceConnection | StreamDispatcher) {
if (connection instanceof StreamDispatcher) return connection.voiceConnection.destroy();
2021-06-11 14:45:27 +05:00
return connection.destroy();
2021-06-11 14:06:51 +05:00
}
2021-06-11 16:50:43 +05:00
2021-06-20 19:22:09 +05:00
/**
* Returns Discord Player voice connection
* @param {Snowflake} guild The guild id
* @returns {StreamDispatcher}
*/
2021-06-11 16:50:43 +05:00
public getConnection(guild: Snowflake) {
return this.cache.get(guild);
}
2021-06-11 14:06:51 +05:00
}
2021-06-11 14:17:42 +05:00
export { VoiceUtils };