discord-audio-stream
Version:
Node library to make discord audio streaming easier.
134 lines (131 loc) • 4.33 kB
JavaScript
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var index_exports = {};
__export(index_exports, {
AudioManager: () => AudioManager
});
module.exports = __toCommonJS(index_exports);
// src/Services/AudioManager/AudioManager.ts
var import_voice = require("@discordjs/voice");
var import_node_path = require("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 = (0, import_voice.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 = (0, import_voice.createAudioResource)((0, import_node_path.join)(__dirname, this.AudioData.Resource), { inlineVolume: true });
} else if (this.AudioData.ResourceType === "Link") {
this.AudioResource = (0, import_voice.createAudioResource)(this.AudioData.Resource, { inlineVolume: true });
} else {
throw new Error("Invalid resource type.");
}
this.AudioPlayer = (0, import_voice.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;
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
AudioManager
});