Added yarn, bun and pnpm config; Added eslint and prettier; Refactored Type import; Updated Version;
This commit is contained in:
@@ -0,0 +1,159 @@
|
||||
import type { AudioPlayer, AudioResource, VoiceConnection } from '@discordjs/voice';
|
||||
import { createAudioPlayer, createAudioResource, joinVoiceChannel } from '@discordjs/voice';
|
||||
import { join } from 'node:path';
|
||||
import type { VoiceAudioDataModel, VoiceConnectionDataModel } from './index.d';
|
||||
|
||||
export default class AudioManager {
|
||||
public VoiceConnection?: VoiceConnection;
|
||||
public AudioPlayer?: AudioPlayer;
|
||||
public AudioResource?: AudioResource;
|
||||
|
||||
protected IsActive?: boolean;
|
||||
protected IsAudioPlaying?: boolean;
|
||||
|
||||
protected ConnectionData?: VoiceConnectionDataModel;
|
||||
protected AudioData?: VoiceAudioDataModel;
|
||||
|
||||
private TimeoutHandle?: NodeJS.Timeout;
|
||||
private RenewInMs?: number;
|
||||
|
||||
constructor(connectionData?: VoiceConnectionDataModel, audioData?: VoiceAudioDataModel, renewInMs = 5400000) {
|
||||
this.RenewInMs = renewInMs;
|
||||
this.ConnectionData = connectionData;
|
||||
this.AudioData = audioData;
|
||||
}
|
||||
|
||||
public OverrideRenewInMs(renewInMs: number = 5400000): void {
|
||||
this.RenewInMs = renewInMs;
|
||||
}
|
||||
|
||||
public OverrideVoiceConnectionData(connectionData: VoiceConnectionDataModel): void {
|
||||
this.ConnectionData = connectionData;
|
||||
}
|
||||
|
||||
public OverrideVoiceAudioDataModel(audioData: VoiceAudioDataModel): void {
|
||||
this.AudioData = audioData;
|
||||
}
|
||||
|
||||
public CreateConnection(isRenew: boolean = false): void {
|
||||
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,
|
||||
});
|
||||
|
||||
this.TimeoutHandle = setTimeout((): void => {
|
||||
this.DestroyConnection(false);
|
||||
if (this.IsActive) {
|
||||
this.CreateConnection(true);
|
||||
|
||||
if (this.IsAudioPlaying) {
|
||||
this.PlayAudioOnConnection();
|
||||
}
|
||||
}
|
||||
}, this.RenewInMs);
|
||||
}
|
||||
|
||||
public PlayAudioOnConnection(): void {
|
||||
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;
|
||||
}
|
||||
|
||||
public StopAudioOnConnection(): void {
|
||||
if (this.IsAudioPlaying) {
|
||||
this.DestroyConnection(false);
|
||||
this.CreateConnection();
|
||||
} else {
|
||||
throw new Error('Audio is not playing.');
|
||||
}
|
||||
}
|
||||
|
||||
public PauseAudio(): void {
|
||||
if (this.IsAudioPlaying) {
|
||||
this.AudioPlayer!.pause();
|
||||
this.IsAudioPlaying = false;
|
||||
} else {
|
||||
throw new TypeError('Audio is not playing.');
|
||||
}
|
||||
}
|
||||
|
||||
public ResumeAudio(): void {
|
||||
if (!this.IsAudioPlaying) {
|
||||
this.AudioPlayer!.unpause();
|
||||
this.IsAudioPlaying = true;
|
||||
} else {
|
||||
throw new TypeError('Audio is playing.');
|
||||
}
|
||||
}
|
||||
|
||||
public CreateConnectionAndPlayAudio(): void {
|
||||
this.CreateConnection();
|
||||
this.PlayAudioOnConnection();
|
||||
}
|
||||
|
||||
public DestroyConnection(resetValidationParameters: boolean = true): void {
|
||||
if (this.CheckIfNull(this.VoiceConnection)) return;
|
||||
this.VoiceConnection!.destroy();
|
||||
|
||||
if (resetValidationParameters) {
|
||||
this.IsActive = false;
|
||||
this.IsAudioPlaying = false;
|
||||
}
|
||||
}
|
||||
|
||||
public SetMaxListeners(maxListeners: number): void {
|
||||
if (this.CheckIfNull(this.VoiceConnection)) return;
|
||||
this.VoiceConnection!.setMaxListeners(maxListeners);
|
||||
}
|
||||
|
||||
public SetVolume(volumeInPercent: number): void {
|
||||
if (volumeInPercent < 0 || volumeInPercent > 100) throw new Error('Volume must be between 0 and 100.');
|
||||
|
||||
if (this.CheckIfNull(this.VoiceConnection)) return;
|
||||
|
||||
this.AudioResource!.volume!.setVolume(volumeInPercent / 100);
|
||||
}
|
||||
|
||||
public Dispose(): void {
|
||||
this.VoiceConnection = undefined;
|
||||
this.AudioPlayer = undefined;
|
||||
this.AudioResource = undefined;
|
||||
this.IsActive = undefined;
|
||||
this.IsAudioPlaying = undefined;
|
||||
this.ConnectionData = undefined;
|
||||
this.AudioData = undefined;
|
||||
this.TimeoutHandle = undefined;
|
||||
this.RenewInMs = undefined;
|
||||
}
|
||||
|
||||
private CheckIfNull<T>(value: T | null): boolean {
|
||||
if (value === null) {
|
||||
throw new Error(`${value} cannot be null in this case.`);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -1,166 +0,0 @@
|
||||
import {
|
||||
AudioPlayer,
|
||||
AudioResource,
|
||||
createAudioPlayer,
|
||||
createAudioResource,
|
||||
joinVoiceChannel,
|
||||
VoiceConnection
|
||||
} from "@discordjs/voice";
|
||||
import {join} from "node:path";
|
||||
import {VoiceAudioDataModel, VoiceConnectionDataModel} from "./typings";
|
||||
|
||||
export default class AudioManager {
|
||||
public VoiceConnection?: VoiceConnection;
|
||||
public AudioPlayer?: AudioPlayer;
|
||||
public AudioResource?: AudioResource;
|
||||
|
||||
protected IsActive?: boolean;
|
||||
protected IsAudioPlaying?: boolean;
|
||||
|
||||
protected ConnectionData?: VoiceConnectionDataModel;
|
||||
protected AudioData?: VoiceAudioDataModel;
|
||||
|
||||
private TimeoutHandle?: NodeJS.Timeout;
|
||||
private RenewInMs?: number;
|
||||
|
||||
constructor(connectionData?: VoiceConnectionDataModel, audioData?: VoiceAudioDataModel, renewInMs = 5400000) {
|
||||
this.RenewInMs = renewInMs;
|
||||
this.ConnectionData = connectionData;
|
||||
this.AudioData = audioData;
|
||||
}
|
||||
|
||||
public OverrideRenewInMs(renewInMs: number = 5400000): void {
|
||||
this.RenewInMs = renewInMs;
|
||||
}
|
||||
|
||||
public OverrideVoiceConnectionData(connectionData: VoiceConnectionDataModel): void {
|
||||
this.ConnectionData = connectionData;
|
||||
}
|
||||
|
||||
public OverrideVoiceAudioDataModel(audioData: VoiceAudioDataModel): void {
|
||||
this.AudioData = audioData;
|
||||
}
|
||||
|
||||
public CreateConnection(isRenew: boolean = false): void {
|
||||
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
|
||||
});
|
||||
|
||||
this.TimeoutHandle = setTimeout((): void => {
|
||||
this.DestroyConnection(false);
|
||||
if (this.IsActive) {
|
||||
this.CreateConnection(true);
|
||||
|
||||
if (this.IsAudioPlaying) {
|
||||
this.PlayAudioOnConnection();
|
||||
}
|
||||
}
|
||||
}, this.RenewInMs);
|
||||
}
|
||||
|
||||
public PlayAudioOnConnection(): void {
|
||||
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;
|
||||
}
|
||||
|
||||
public StopAudioOnConnection(): void {
|
||||
if (this.IsAudioPlaying) {
|
||||
this.DestroyConnection(false)
|
||||
this.CreateConnection();
|
||||
} else {
|
||||
throw new Error("Audio is not playing.");
|
||||
}
|
||||
}
|
||||
|
||||
public PauseAudio(): void {
|
||||
if (this.IsAudioPlaying) {
|
||||
this.AudioPlayer!.pause();
|
||||
this.IsAudioPlaying = false;
|
||||
} else {
|
||||
throw new TypeError("Audio is not playing.");
|
||||
}
|
||||
}
|
||||
|
||||
public ResumeAudio(): void {
|
||||
if (!this.IsAudioPlaying) {
|
||||
this.AudioPlayer!.unpause();
|
||||
this.IsAudioPlaying = true;
|
||||
} else {
|
||||
throw new TypeError("Audio is playing.");
|
||||
}
|
||||
}
|
||||
|
||||
public CreateConnectionAndPlayAudio(): void {
|
||||
this.CreateConnection();
|
||||
this.PlayAudioOnConnection();
|
||||
}
|
||||
|
||||
public DestroyConnection(resetValidationParameters: boolean = true): void {
|
||||
if (this.CheckIfNull(this.VoiceConnection)) return;
|
||||
this.VoiceConnection!.destroy();
|
||||
|
||||
if (resetValidationParameters) {
|
||||
this.IsActive = false;
|
||||
this.IsAudioPlaying = false;
|
||||
}
|
||||
}
|
||||
|
||||
public SetMaxListeners(maxListeners: number): void {
|
||||
if (this.CheckIfNull(this.VoiceConnection)) return;
|
||||
this.VoiceConnection!.setMaxListeners(maxListeners);
|
||||
}
|
||||
|
||||
public SetVolume(volumeInPercent: number): void {
|
||||
if (volumeInPercent < 0 || volumeInPercent > 100)
|
||||
throw new Error("Volume must be between 0 and 100.");
|
||||
|
||||
if (this.CheckIfNull(this.VoiceConnection)) return;
|
||||
|
||||
this.AudioResource!.volume!.setVolume(volumeInPercent / 100);
|
||||
}
|
||||
|
||||
public Dispose(): void {
|
||||
this.VoiceConnection = undefined;
|
||||
this.AudioPlayer = undefined;
|
||||
this.AudioResource = undefined;
|
||||
this.IsActive = undefined;
|
||||
this.IsAudioPlaying = undefined;
|
||||
this.ConnectionData = undefined;
|
||||
this.AudioData = undefined;
|
||||
this.TimeoutHandle = undefined;
|
||||
this.RenewInMs = undefined;
|
||||
}
|
||||
|
||||
private CheckIfNull<T>(value: T | null): boolean {
|
||||
if (value === null) {
|
||||
throw new Error(`${value} cannot be null in this case.`);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Vendored
+32
@@ -0,0 +1,32 @@
|
||||
import type { DiscordGatewayAdapterCreator } from '@discordjs/voice';
|
||||
|
||||
export type VoiceConnectionDataModel = {
|
||||
/**
|
||||
* The ID of the voice channel to connect to.
|
||||
*/
|
||||
VoiceChannelId: string;
|
||||
|
||||
/**
|
||||
* The Id of the guild (server) to connect to.
|
||||
*/
|
||||
GuildId: string;
|
||||
|
||||
/**
|
||||
* The adapter creator for the voice connection.
|
||||
* Can be archived by the guild instance via the voiceAdapterCreator property.
|
||||
*/
|
||||
VoiceAdapter: DiscordGatewayAdapterCreator;
|
||||
};
|
||||
|
||||
export type VoiceAudioDataModel = {
|
||||
/**
|
||||
* The unique identifier for the audio resource.
|
||||
*/
|
||||
ResourceType: 'Link' | 'File';
|
||||
|
||||
/**
|
||||
* The URL or file path of the audio resource.
|
||||
* any is to be assumed to require(filepath)
|
||||
*/
|
||||
Resource: string | any;
|
||||
};
|
||||
+1
-2
@@ -1,2 +1 @@
|
||||
export {default as AudioManager} from './audioManager';
|
||||
export type {VoiceConnectionDataModel, VoiceAudioDataModel} from './typings';
|
||||
export { default as AudioManager } from './audio-manager';
|
||||
|
||||
Vendored
-32
@@ -1,32 +0,0 @@
|
||||
import type {DiscordGatewayAdapterCreator} from "@discordjs/voice";
|
||||
|
||||
export type VoiceConnectionDataModel = {
|
||||
/**
|
||||
* The ID of the voice channel to connect to.
|
||||
*/
|
||||
VoiceChannelId: string;
|
||||
|
||||
/**
|
||||
* The Id of the guild (server) to connect to.
|
||||
*/
|
||||
GuildId: string;
|
||||
|
||||
/**
|
||||
* The adapter creator for the voice connection.
|
||||
* Can be archived by the guild instance via the voiceAdapterCreator property.
|
||||
*/
|
||||
VoiceAdapter: DiscordGatewayAdapterCreator;
|
||||
}
|
||||
|
||||
export type VoiceAudioDataModel = {
|
||||
/**
|
||||
* The unique identifier for the audio resource.
|
||||
*/
|
||||
ResourceType: "Link" | "File";
|
||||
|
||||
/**
|
||||
* The URL or file path of the audio resource.
|
||||
* any is to be assumed to require(filepath)
|
||||
*/
|
||||
Resource: string | any;
|
||||
}
|
||||
Reference in New Issue
Block a user