UNPKG

discord-audio-stream

Version:

Node library to make discord audio streaming easier.

118 lines (116 loc) 3.51 kB
// node_modules/tsup/assets/esm_shims.js import { fileURLToPath } from "url"; import path from "path"; var getFilename = () => fileURLToPath(import.meta.url); var getDirname = () => path.dirname(getFilename()); var __dirname = /* @__PURE__ */ getDirname(); // src/Services/AudioManager/AudioManager.ts import { createAudioPlayer, createAudioResource, joinVoiceChannel } from "@discordjs/voice"; import { join } from "node:path"; var AudioManager = class { VoiceConnection = null; AudioPlayer = null; AudioResource = null; IsActive = false; IsAudioPlaying = false; ConnectionData; AudioData; constructor(connectionData = null, audioData = null) { this.ConnectionData = connectionData; this.AudioData = audioData; } OverrideOptions(connectionData = null, audioData = null) { this.ConnectionData = connectionData; this.AudioData = audioData; } CreateConnection(isRenew = false) { this.CheckIfNull(this.ConnectionData); if (!isRenew) { if (this.IsActive) { this.DestroyConnection(false); } else { this.IsActive = true; } } this.VoiceConnection = joinVoiceChannel({ channelId: this.ConnectionData.VoiceChannelId.toString(), guildId: this.ConnectionData.GuildId.toString(), adapterCreator: this.ConnectionData.VoiceAdapter }); setTimeout(() => { this.RenewConnectionAndAudio(); }, 54e5); } PlayAudioOnConnection() { this.CheckIfNull(this.AudioData); if (this.AudioData.ResourceType === "File") { this.AudioResource = createAudioResource(join(__dirname, this.AudioData.Resource), { inlineVolume: true }); } else if (this.AudioData.ResourceType === "Link") { this.AudioResource = createAudioResource(this.AudioData.Resource, { inlineVolume: true }); } else { throw new Error("Invalid resource type."); } this.AudioPlayer = createAudioPlayer(); this.AudioPlayer.play(this.AudioResource); this.VoiceConnection.subscribe(this.AudioPlayer); this.IsAudioPlaying = true; } StopAudioOnConnection() { if (this.IsAudioPlaying) { this.DestroyConnection(false); this.CreateConnection(); } else { throw new Error("Audio is not playing."); } } PauseAudio() { this.CheckIfNull(this.AudioPlayer); this.AudioPlayer.pause(); } ResumeAudio() { this.CheckIfNull(this.AudioPlayer); this.AudioPlayer.unpause(); } CreateConnectionAndPlayAudio() { this.CreateConnection(); this.PlayAudioOnConnection(); } DestroyConnection(resetValidationParameters = true) { this.CheckIfNull(this.VoiceConnection); this.VoiceConnection.destroy(); if (resetValidationParameters) { this.IsActive = false; this.IsAudioPlaying = false; } } SetMaxListeners(maxListeners) { this.CheckIfNull(this.VoiceConnection); this.VoiceConnection.setMaxListeners(maxListeners); } SetVolume(volumeInPercent) { if (volumeInPercent < 0 || volumeInPercent > 100) throw new Error("Volume must be between 0 and 100."); this.AudioResource.volume.setVolume(volumeInPercent / 100); } RenewConnectionAndAudio() { this.DestroyConnection(false); if (this.IsActive) { this.CreateConnection(true); if (this.IsAudioPlaying) { this.PlayAudioOnConnection(); } } } CheckIfNull(value) { if (value === null) { throw new Error(`${value} cannot be null in this case.`); } return true; } }; export { AudioManager };