Merge pull request #17 from FrauJulian/fraujulian
Fixed AudioTimeout (hopefully); Refactored; Updated Dependencies;
This commit is contained in:
@@ -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`
|
||||||
@@ -78,27 +79,35 @@ 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. |
|
||||||
| `CreateConnection` | `isRenew` (type of boolean, default value is false) | void | Method to join the voice connection. |
|
| `OverrideVoiceAudioDataModel` | `audioData` (type of **VoiceAudioDataModel**) | void | Method to override global audioData variable. |
|
||||||
| `PlayAudioOnConnection` | | void | Method to play audio on the existing voice connection. |
|
| `OverrideRenewInMs` | `renewInMs` (type of int, default value is 5400000) | void | Method to override global renewInMs variable. |
|
||||||
| `PauseAudio` | | void | Method to pause the audio. |
|
| `CreateConnection` | `isRenew` (type of boolean, default value is false) | void | Method to join the voice connection. |
|
||||||
| `ResumeAudio` | | void | Method to resume the audio. |
|
| `PlayAudioOnConnection` | | void | Method to play audio on the existing voice connection. |
|
||||||
| `StopAudioOnConnection` | | void | Method to stop the audio without destroying voice connection. |
|
| `PauseAudio` | | void | Method to pause the audio. |
|
||||||
| `CreateConnectionAndPlayAudio` | | void | Method to join the voice connection and play audio. |
|
| `ResumeAudio` | | void | Method to resume the audio. |
|
||||||
| `DestroyConnection` | | void | Method to destroy the voice connection. |
|
| `StopAudioOnConnection` | | void | Method to stop the audio without destroying voice connection. |
|
||||||
| `Dispose` | | void | Dispose all data in object. |
|
| `CreateConnectionAndPlayAudio` | | void | Method to join the voice connection and play audio. |
|
||||||
| `SetVolume` | `volume` (type of number, 0 - 100 percent) | void | Method to set the audio volume. | Method to set the volume of the audio. |
|
| `DestroyConnection` | | void | Method to destroy the voice connection. |
|
||||||
| `SetMaxListeners` | `maxListeners` (type of number) | void | Method to set the max listeners of the audio stream. |
|
| `Dispose` | | void | Dispose all data in object. |
|
||||||
|
| `SetVolume` | `volume` (type of number, 0 - 100 percent) | void | Method to set the audio volume. | Method to set the volume of the audio. |
|
||||||
|
| `SetMaxListeners` | `maxListeners` (type of number) | void | Method to set the max listeners of the audio stream. |
|
||||||
|
|
||||||
##### 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:
|
||||||
|
|
||||||
|
|||||||
Generated
+422
-1301
File diff suppressed because it is too large
Load Diff
+7
-7
@@ -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"
|
||||||
|
|||||||
+37
-40
@@ -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.VoiceConnection = undefined;
|
||||||
this.DestroyConnection();
|
this.AudioPlayer = undefined;
|
||||||
this.ConnectionData = undefined;
|
this.AudioResource = undefined;
|
||||||
this.VoiceConnection = undefined;
|
this.IsActive = undefined;
|
||||||
}
|
this.IsAudioPlaying = undefined;
|
||||||
|
this.ConnectionData = undefined;
|
||||||
if (this.IsAudioPlaying != undefined) {
|
this.AudioData = undefined;
|
||||||
this.AudioData = undefined;
|
this.TimeoutHandle = undefined;
|
||||||
this.AudioResource = undefined;
|
this.RenewInMs = undefined;
|
||||||
this.AudioPlayer = undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this.TimeoutHandle) {
|
|
||||||
clearTimeout(this.TimeoutHandle);
|
|
||||||
this.TimeoutHandle = 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
@@ -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';
|
|
||||||
|
|||||||
Vendored
-12
@@ -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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user