Skip to content

Commit

Permalink
Update: Changing Stop, Skip, Pause, Resume, and Add Playback State
Browse files Browse the repository at this point in the history
  • Loading branch information
TEGRAXD committed Jul 22, 2024
1 parent 239bdc3 commit e214563
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 17 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Laurentina

[![Build](https://img.shields.io/github/actions/workflow/status/TEGRAXD/laurentina/tests.yml)](https://github.com/TEGRAXD/laurentina)
[![Version](https://img.shields.io/badge/version-1.0.1-blue)](https://www.npmjs.com/package/laurentina)
[![Version](https://img.shields.io/badge/version-1.0.2-blue)](https://www.npmjs.com/package/laurentina)
[![Github Stars](https://img.shields.io/github/stars/TEGRAXD/Laurentina?style=flat-square)](https://github.com/TEGRAXD/laurentina)
[![License](https://img.shields.io/github/license/TEGRAXD/laurentina)](https://github.com/TEGRAXD/laurentina?tab=readme-ov-file#license)

Laurentina is audio controller and queue wrapper for Shoukaku and Discord.js to manage music playback.
Laurentina is Audio Controller and Queue Wrapper for Shoukaku and Discord.js to Manage Music Playback.

![](static/laurentina.png)
> © Arknights
Expand All @@ -20,11 +20,11 @@ Laurentina is audio controller and queue wrapper for Shoukaku and Discord.js to
- Resume
- Stop
- Loop
- Queue
- Get Queue
- Clear Queue

## Version
1.0.1

1.0.2

## Contributor
```
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "laurentina",
"version": "1.0.1",
"version": "1.0.2",
"description": "Audio controller and queue wrapper for Shoukaku and Discord.js to manage music playback.",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
Expand Down
49 changes: 38 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@ import discord from "discord.js";
import shoukaku from "shoukaku";
import { LavalinkNode } from "./types/lavalink_node";

enum PlaybackState {
PLAYING = "playing",
PAUSED = "paused",
STOPPED = "stopped",
}

class AudioController extends EventEmitter {
private shoukaku: shoukaku.Shoukaku;
player: shoukaku.Player;
guildID: discord.Snowflake;
channelID: discord.Snowflake;
textChannel: discord.Snowflake;
private queue: shoukaku.Track[];
playing: boolean;
state: PlaybackState;
currentTrack: shoukaku.Track | null;
loop: "none" | "single" | "queue";
private timeout: NodeJS.Timeout | null;
Expand All @@ -30,7 +36,7 @@ class AudioController extends EventEmitter {
this.channelID = channelID;
this.textChannel = textChannelID;
this.queue = [];
this.playing = false;
this.state = PlaybackState.STOPPED;
this.currentTrack = null;
this.loop = "none";
this.timeout = null;
Expand All @@ -54,7 +60,8 @@ class AudioController extends EventEmitter {

this.add(track, addToQueueCallback);

if (!this.playing) {
// if (!this.playing) {
if (this.state === PlaybackState.STOPPED) {
await this.playNext(playCallback);
}
}
Expand All @@ -65,20 +72,20 @@ class AudioController extends EventEmitter {
*/
private async playNext(callback?: (track: shoukaku.Track) => Promise<void>): Promise<void> {
if (!this.player) {
this.playing = false;
this.state = PlaybackState.STOPPED;
throw new Error("No player available");
}

const next = this.queue.shift();

if (!next) {
this.playing = false;
this.state = PlaybackState.STOPPED;
this.currentTrack = null;
this.startTimer();
return;
}

this.playing = true;
this.state = PlaybackState.PLAYING;
this.currentTrack = next;

this.clearTimer();
Expand Down Expand Up @@ -106,7 +113,9 @@ class AudioController extends EventEmitter {
});
} catch (error) {
console.error(error);
this.playing = false;
// this.playing = false;

this.state = PlaybackState.STOPPED;
this.currentTrack = null;
await this.playNext(); // Move to the next track in case of error
}
Expand Down Expand Up @@ -173,9 +182,16 @@ class AudioController extends EventEmitter {
* Skip the current track
* @returns void
*/
skip() {
async skip(callback?: (track: shoukaku.Track) => Promise<void>): Promise<void> {
if (this.player) {
this.state = PlaybackState.STOPPED;
this.player.stopTrack();

if (callback && this.currentTrack) await callback(this.currentTrack);

this.currentTrack = null;

await this.playNext();
}
}

Expand All @@ -184,7 +200,8 @@ class AudioController extends EventEmitter {
* @returns void
*/
pause() {
if (this.player) {
if (this.state == PlaybackState.PLAYING && this.player) {
this.state = PlaybackState.PAUSED;
this.player.setPaused(true);
}
}
Expand All @@ -194,7 +211,8 @@ class AudioController extends EventEmitter {
* @returns void
*/
resume() {
if (this.player) {
if (this.state == PlaybackState.PAUSED && this.player) {
this.state = PlaybackState.PLAYING;
this.player.setPaused(false);
}
}
Expand All @@ -205,6 +223,7 @@ class AudioController extends EventEmitter {
*/
stop() {
if (this.player) {
this.state = PlaybackState.STOPPED;
this.player.stopTrack();
}
}
Expand Down Expand Up @@ -235,6 +254,14 @@ class AudioController extends EventEmitter {
return this.queue;
}

/**
* Clear the queue
* @returns void
*/
clearQueue() {
this.queue = [];
}

/**
* start the timer to leave the voice channel after 5 minutes of inactivity
* @returns void
Expand Down Expand Up @@ -327,4 +354,4 @@ class Laurentina {

export * from "./types/track";

export { Laurentina };
export { Laurentina, PlaybackState };

0 comments on commit e214563

Please sign in to comment.