aboutsummaryrefslogtreecommitdiff
path: root/AudioBackend.js
diff options
context:
space:
mode:
authorAndrew Lee <alee14498@protonmail.com>2022-07-07 23:49:37 -0400
committerAndrew Lee <alee14498@protonmail.com>2022-07-07 23:49:37 -0400
commit83e4c8679c656ecb352ddc34d5dced9518ba240a (patch)
tree0f220abe0de074880e5f28d4e0779dc6b98d7c07 /AudioBackend.js
parent8d2042206df47b0f172ecc829b5f2c518b89a6a5 (diff)
downloadDLAP-83e4c8679c656ecb352ddc34d5dced9518ba240a.tar.gz
DLAP-83e4c8679c656ecb352ddc34d5dced9518ba240a.tar.bz2
DLAP-83e4c8679c656ecb352ddc34d5dced9518ba240a.zip
New shuffling system (not bug free yet); Tweaks and fixes on other commands
Diffstat (limited to 'AudioBackend.js')
-rw-r--r--AudioBackend.js53
1 files changed, 46 insertions, 7 deletions
diff --git a/AudioBackend.js b/AudioBackend.js
index 9873d9a..d1f54c4 100644
--- a/AudioBackend.js
+++ b/AudioBackend.js
@@ -34,6 +34,8 @@ export const player = createAudioPlayer();
export let audio;
export let files = readdirSync('music');
let fileData;
+export let audioArray;
+export let currentTrack;
export let playerState;
export let isAudioStatePaused;
@@ -55,20 +57,57 @@ export async function voiceInit(bot) {
});
player.on('idle', () => {
- console.log("Beat has finished playing, shuffling the beats...");
- searchAudio(bot);
+ console.log("Beat has finished playing, now to the next beat...");
+ nextAudio(bot);
})
- await searchAudio(bot);
+ await shufflePlaylist(bot);
return connection.subscribe(player);
}).catch(e => { console.error(e) })
}
-export async function searchAudio(bot){
- //TODO: Eventually this system will need a rework so it won't repeat the same files.
+function shuffleArray(array) {
+ let currentIndex = array.length, randomIndex;
- audio = files[Math.floor(Math.random() * files.length)];
- return await playAudio(bot);
+ // While there remain elements to shuffle.
+ while (currentIndex !== 0) {
+
+ // Pick a remaining element.
+ randomIndex = Math.floor(Math.random() * currentIndex);
+ currentIndex--;
+
+ // And swap it with the current element.
+ [array[currentIndex], array[randomIndex]] = [
+ array[randomIndex], array[currentIndex]];
+ }
+
+ return array;
+}
+
+async function shufflePlaylist(bot) {
+ console.log('Shuffling beats...');
+ currentTrack = 0
+ audioArray = files;
+ shuffleArray(audioArray);
+ console.log(audioArray);
+ audio = audioArray[currentTrack]
+ return await playAudio(bot);
+}
+
+export async function nextAudio(bot) {
+ let totalTrack = files.length
+ totalTrack--
+ console.log(totalTrack)
+ if (currentTrack > totalTrack) {
+ console.log('All beats in the playlist has finished, reshuffling...')
+ await shufflePlaylist();
+ } else {
+ console.log(files.length)
+ currentTrack++
+ audio = audioArray[currentTrack];
+ }
+
+ return await playAudio(bot);
}
export async function inputAudio(bot, integer) {