Skip to content

Commit

Permalink
Add shuffle mode and fixing README.MD documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
TEGRAXD committed Aug 22, 2024
1 parent 9e189fc commit 4e28679
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const client = new Client({
]
});

const shoukaku = new Shoukaku(new Connectors.DisordJS(client), nodes);
const shoukaku = new Shoukaku(new Connectors.DiscordJS(client), nodes);
const laurentina = new Laurentina(client, shoukaku, nodes);

// Bind shoukaku and laurentina to client
Expand Down
54 changes: 52 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ enum LoopMode {
QUEUE = "queue",
}

enum ShuffleMode {
OFF = "off",
ON = "on",
}

class AudioController extends EventEmitter {
private shoukaku: shoukaku.Shoukaku;
player: shoukaku.Player;
Expand All @@ -25,6 +30,7 @@ class AudioController extends EventEmitter {
state: PlaybackState;
currentTrack: shoukaku.Track | null;
loop: LoopMode;
shuffle: ShuffleMode;
private timeout: NodeJS.Timeout | null;

constructor(
Expand All @@ -45,6 +51,7 @@ class AudioController extends EventEmitter {
this.state = PlaybackState.STOPPED;
this.currentTrack = null;
this.loop = LoopMode.NONE;
this.shuffle = ShuffleMode.OFF;
this.timeout = null;

this.startTimer();
Expand Down Expand Up @@ -81,7 +88,13 @@ class AudioController extends EventEmitter {
throw new Error("No player available");
}

const next = this.queue.shift();
let next = null;

if (this.shuffle === ShuffleMode.ON) {
next = this.queue.splice(Math.floor(Math.random() * this.queue.length), 1).shift();
} else {
next = this.queue.shift();
}

if (!next) {
this.state = PlaybackState.STOPPED;
Expand Down Expand Up @@ -231,6 +244,16 @@ class AudioController extends EventEmitter {
}
}

/**
* Set the loop mode
* @param mode The loop mode to set
* @returns LoopMode
*/
setLoop(mode: LoopMode): LoopMode {
this.loop = mode;
return this.loop;
}

/**
* Toggle the loop mode
* @returns void
Expand All @@ -251,6 +274,33 @@ class AudioController extends EventEmitter {
return this.loop;
}

/**
* Set the shuffle mode
* @param mode The shuffle mode to set
* @returns ShuffleMode
*/
setShuffle(mode: ShuffleMode): ShuffleMode {
this.shuffle = mode;
return this.shuffle;
}

/**
* Toggle the shuffle mode
* @returns void
*/
toggleShuffle(): ShuffleMode {
switch (this.shuffle) {
case ShuffleMode.OFF:
this.shuffle = ShuffleMode.ON;
break;
case ShuffleMode.ON:
this.shuffle = ShuffleMode.OFF;
break;
}

return this.shuffle;
}

/**
* Get the current queue
* @returns shoukaku.Track[]
Expand Down Expand Up @@ -359,4 +409,4 @@ class Laurentina {

export * from "./types/track";

export { Laurentina, PlaybackState, LoopMode };
export { Laurentina, PlaybackState, LoopMode, ShuffleMode };

0 comments on commit 4e28679

Please sign in to comment.