Merge pull request #17 from FrauJulian/fraujulian

Fixed AudioTimeout (hopefully); Refactored; Updated Dependencies;
This commit is contained in:
2025-10-03 15:37:21 +02:00
committed by GitHub
7 changed files with 508 additions and 1381 deletions
+17 -8
View File
@@ -26,7 +26,8 @@ bun add discord-audio-stream
**Encryption Libraries (npm install):** **Encryption Libraries (npm install):**
> You only need to install one of these libraries if your system does not support `aes-256-gcm` (verify by running `require('node:crypto').getCiphers().includes('aes-256-gcm')`). > You only need to install one of these libraries if your system does not support `aes-256-gcm` (verify by running
`require('node:crypto').getCiphers().includes('aes-256-gcm')`).
- `sodium-native` - `sodium-native`
- `sodium` - `sodium`
@@ -79,8 +80,10 @@ let audioManager = new AudioManager({
#### Methods #### Methods
| Callable with | Parameters | Return type | Description | | Callable with | Parameters | Return type | Description |
|--------------------------------|--------------------------------------------------------------------------------------------------------|-------------|--------------------------------------------------------------------| |--------------------------------|---------------------------------------------------------|-------------|---------------------------------------------------------------|
| `OverrideOptions` | `connectionData` (type of **VoiceConnectionDataModel**), `audioData` (type of **VoiceAudioDataModel**) | void | Method to override global variables, connectionData and audioData. | | `OverrideVoiceConnectionData` | `connectionData` (type of **VoiceConnectionDataModel**) | void | Method to override global connectionData variable. |
| `OverrideVoiceAudioDataModel` | `audioData` (type of **VoiceAudioDataModel**) | void | Method to override global audioData variable. |
| `OverrideRenewInMs` | `renewInMs` (type of int, default value is 5400000) | void | Method to override global renewInMs variable. |
| `CreateConnection` | `isRenew` (type of boolean, default value is false) | void | Method to join the voice connection. | | `CreateConnection` | `isRenew` (type of boolean, default value is false) | void | Method to join the voice connection. |
| `PlayAudioOnConnection` | | void | Method to play audio on the existing voice connection. | | `PlayAudioOnConnection` | | void | Method to play audio on the existing voice connection. |
| `PauseAudio` | | void | Method to pause the audio. | | `PauseAudio` | | void | Method to pause the audio. |
@@ -94,11 +97,17 @@ let audioManager = new AudioManager({
##### Types History ##### Types History
- [**VoiceConnection** by discord.js/voice](https://github.com/discordjs/discord.js/blob/main/packages/voice/src/VoiceConnection.ts#L166) - [**VoiceConnection
- [**AudioPlayer** by discord.js/voice](https://github.com/discordjs/discord.js/blob/main/packages/voice/src/audio/AudioPlayer.ts#L155) ** by discord.js/voice](https://github.com/discordjs/discord.js/blob/main/packages/voice/src/VoiceConnection.ts#L166)
- [**AudioResource** by discord.js/voice](https://github.com/discordjs/discord.js/blob/main/packages/voice/src/audio/AudioResource.ts#L44) - [**AudioPlayer
- [**VoiceConnectionDataModel**](https://github.com/FrauJulian/Discord-Audio-Stream/blob/main/src/Models/VoiceConnectionDataModel.d.ts#L3) (custom type) ** by discord.js/voice](https://github.com/discordjs/discord.js/blob/main/packages/voice/src/audio/AudioPlayer.ts#L155)
- [**VoiceAudioDataModel**](https://github.com/FrauJulian/Discord-Audio-Stream/blob/main/src/Models/VoiceAudioDataModel.d.ts#L1) (custom type) - [**AudioResource
** by discord.js/voice](https://github.com/discordjs/discord.js/blob/main/packages/voice/src/audio/AudioResource.ts#L44)
- [**VoiceConnectionDataModel
**](https://github.com/FrauJulian/Discord-Audio-Stream/blob/main/src/Models/VoiceConnectionDataModel.d.ts#L3) (custom
type)
- [**VoiceAudioDataModel
**](https://github.com/FrauJulian/Discord-Audio-Stream/blob/main/src/Models/VoiceAudioDataModel.d.ts#L1) (custom type)
## 📋 Credits: ## 📋 Credits:
+422 -1301
View File
File diff suppressed because it is too large Load Diff
+7 -7
View File
@@ -43,19 +43,19 @@
"stream" "stream"
], ],
"contributors": [ "contributors": [
"Lechner Julian - FrauJulian <office@lechner-systems.at>" "FrauJulian - Lechner Julian <fraujulian@lechner.top>"
], ],
"dependencies": { "dependencies": {
"@discordjs/voice": "^0.18.0" "@discordjs/voice": "^0.19.0"
}, },
"devDependencies": { "devDependencies": {
"@types/ejs": "^3.1.5", "@types/ejs": "^3.1.5",
"@types/node": "^24.1.0", "@types/node": "^24.6.2",
"del-cli": "^6.0.0", "del-cli": "^7.0.0",
"npm-check-updates": "^18.0.2", "npm-check-updates": "^19.0.0",
"ts-jest": "^29.4.0", "ts-jest": "^29.4.4",
"tsup": "^8.5.0", "tsup": "^8.5.0",
"typescript": "^5.9.2" "typescript": "^5.9.3"
}, },
"engines": { "engines": {
"node": ">=18" "node": ">=18"
+34 -37
View File
@@ -7,16 +7,15 @@ import {
VoiceConnection VoiceConnection
} from "@discordjs/voice"; } from "@discordjs/voice";
import {join} from "node:path"; import {join} from "node:path";
import {VoiceConnectionDataModel} from "./types/VoiceConnectionDataModel"; import {VoiceAudioDataModel, VoiceConnectionDataModel} from "./typings";
import {VoiceAudioDataModel} from "./types/VoiceAudioDataModel";
export default class AudioManager { export default class AudioManager {
public VoiceConnection?: VoiceConnection = undefined; public VoiceConnection?: VoiceConnection;
public AudioPlayer?: AudioPlayer = undefined; public AudioPlayer?: AudioPlayer;
public AudioResource?: AudioResource = undefined; public AudioResource?: AudioResource;
protected IsActive?: boolean = undefined; protected IsActive?: boolean;
protected IsAudioPlaying?: boolean = undefined; protected IsAudioPlaying?: boolean;
protected ConnectionData?: VoiceConnectionDataModel; protected ConnectionData?: VoiceConnectionDataModel;
protected AudioData?: VoiceAudioDataModel; protected AudioData?: VoiceAudioDataModel;
@@ -30,9 +29,15 @@ export default class AudioManager {
this.AudioData = audioData; this.AudioData = audioData;
} }
public OverrideOptions(connectionData?: VoiceConnectionDataModel, audioData?: VoiceAudioDataModel, renewInMs = 5400000): void { public OverrideRenewInMs(renewInMs: number = 5400000): void {
this.RenewInMs = renewInMs; this.RenewInMs = renewInMs;
}
public OverrideVoiceConnectionData(connectionData: VoiceConnectionDataModel): void {
this.ConnectionData = connectionData; this.ConnectionData = connectionData;
}
public OverrideVoiceAudioDataModel(audioData: VoiceAudioDataModel): void {
this.AudioData = audioData; this.AudioData = audioData;
} }
@@ -54,7 +59,14 @@ export default class AudioManager {
}); });
this.TimeoutHandle = setTimeout((): void => { this.TimeoutHandle = setTimeout((): void => {
this.RenewConnectionAndAudio(); this.DestroyConnection(false);
if (this.IsActive) {
this.CreateConnection(true);
if (this.IsAudioPlaying) {
this.PlayAudioOnConnection();
}
}
}, this.RenewInMs); }, this.RenewInMs);
} }
@@ -83,7 +95,6 @@ export default class AudioManager {
} else { } else {
throw new Error("Audio is not playing."); throw new Error("Audio is not playing.");
} }
} }
public PauseAudio(): void { public PauseAudio(): void {
@@ -110,7 +121,7 @@ export default class AudioManager {
} }
public DestroyConnection(resetValidationParameters: boolean = true): void { public DestroyConnection(resetValidationParameters: boolean = true): void {
this.CheckIfNull(this.VoiceConnection); if (this.CheckIfNull(this.VoiceConnection)) return;
this.VoiceConnection!.destroy(); this.VoiceConnection!.destroy();
if (resetValidationParameters) { if (resetValidationParameters) {
@@ -120,43 +131,29 @@ export default class AudioManager {
} }
public SetMaxListeners(maxListeners: number): void { public SetMaxListeners(maxListeners: number): void {
if (this.CheckIfNull(this.VoiceConnection)) return;
this.VoiceConnection!.setMaxListeners(maxListeners); this.VoiceConnection!.setMaxListeners(maxListeners);
} }
public SetVolume(volumeInPercent: number): void { public SetVolume(volumeInPercent: number): void {
if (volumeInPercent < 0 || volumeInPercent > 100) throw new Error("Volume must be between 0 and 100."); if (volumeInPercent < 0 || volumeInPercent > 100)
this.CheckIfNull(this.VoiceConnection); throw new Error("Volume must be between 0 and 100.");
if (this.CheckIfNull(this.VoiceConnection)) return;
this.AudioResource!.volume!.setVolume(volumeInPercent / 100); this.AudioResource!.volume!.setVolume(volumeInPercent / 100);
} }
public Dispose(): void { public Dispose(): void {
if (this.IsActive != undefined) {
this.DestroyConnection();
this.ConnectionData = undefined;
this.VoiceConnection = undefined; this.VoiceConnection = undefined;
}
if (this.IsAudioPlaying != undefined) {
this.AudioData = undefined;
this.AudioResource = undefined;
this.AudioPlayer = undefined; this.AudioPlayer = undefined;
} this.AudioResource = undefined;
this.IsActive = undefined;
if (this.TimeoutHandle) { this.IsAudioPlaying = undefined;
clearTimeout(this.TimeoutHandle); this.ConnectionData = undefined;
this.AudioData = undefined;
this.TimeoutHandle = undefined; this.TimeoutHandle = undefined;
} this.RenewInMs = undefined;
}
private RenewConnectionAndAudio(): void {
this.DestroyConnection(false);
if (this.IsActive) {
this.CreateConnection(true);
if (this.IsAudioPlaying) {
this.PlayAudioOnConnection();
}
}
} }
private CheckIfNull<T>(value: T | null): boolean { private CheckIfNull<T>(value: T | null): boolean {
+1 -2
View File
@@ -1,3 +1,2 @@
export {default as AudioManager} from './audioManager'; export {default as AudioManager} from './audioManager';
export type {VoiceConnectionDataModel} from './types/VoiceConnectionDataModel'; export type {VoiceConnectionDataModel, VoiceAudioDataModel} from './typings';
export type {VoiceAudioDataModel} from './types/VoiceAudioDataModel';
-12
View File
@@ -1,12 +0,0 @@
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;
}
@@ -17,3 +17,16 @@ export type VoiceConnectionDataModel = {
*/ */
VoiceAdapter: DiscordGatewayAdapterCreator; 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;
}