diff options
| author | Andrew Lee <alee14498@protonmail.com> | 2022-07-09 16:56:04 -0400 |
|---|---|---|
| committer | Andrew Lee <alee14498@protonmail.com> | 2022-07-09 16:56:04 -0400 |
| commit | 32f8d995abd20b1f2b82dc98a108b0a762dc6b1b (patch) | |
| tree | 49c71a7917554b7c47e19d4b3b32aad5fd45cdc9 /AudioBackend.js | |
| parent | 2471564098b1de1a635d90f381ef67cf2cb43511 (diff) | |
| download | DLAP-32f8d995abd20b1f2b82dc98a108b0a762dc6b1b.tar.gz DLAP-32f8d995abd20b1f2b82dc98a108b0a762dc6b1b.tar.bz2 DLAP-32f8d995abd20b1f2b82dc98a108b0a762dc6b1b.zip | |
Using Durstenfeld shuffle, replacing Fisher-Yates
Diffstat (limited to 'AudioBackend.js')
| -rw-r--r-- | AudioBackend.js | 65 |
1 files changed, 28 insertions, 37 deletions
diff --git a/AudioBackend.js b/AudioBackend.js index bdd3738..694b5d7 100644 --- a/AudioBackend.js +++ b/AudioBackend.js @@ -67,21 +67,10 @@ export async function voiceInit(bot) { } function shuffleArray(array) { - let currentIndex = array.length, randomIndex; - - // 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; + for (let i = array.length - 1; i > 0; i--) { + const j = Math.floor(Math.random() * (i + 1)); + [array[i], array[j]] = [array[j], array[i]]; + } } export async function shufflePlaylist(bot) { @@ -97,8 +86,9 @@ export async function shufflePlaylist(bot) { export async function nextAudio(bot) { let totalTrack = files.length totalTrack-- + if (currentTrack > totalTrack) { - console.log('All beats in the playlist has finished, reshuffling...') + console.log('All beats in the playlist has finished, reshuffling...'); await shufflePlaylist(); } else { currentTrack++ @@ -109,37 +99,38 @@ export async function nextAudio(bot) { } export async function inputAudio(bot, integer) { - audio = files[integer]; - return await playAudio(bot); + let inputFiles = readdirSync('music'); + audio = inputFiles[integer]; + return await playAudio(bot); } export async function playAudio(bot) { - let resource = createAudioResource('music/' + audio); + let resource = createAudioResource('music/' + audio); - player.play(resource); + player.play(resource); - console.log('Now playing: ' + audio); + console.log('Now playing: ' + audio); - playerState = "Playing" - isAudioStatePaused = false + playerState = "Playing" + isAudioStatePaused = false - audio = audio.split('.').slice(0, -1).join('.'); + audio = audio.split('.').slice(0, -1).join('.'); - if (config.txtFile === true) { - fileData = "Now Playing: " + audio - writeFile("./now-playing.txt", fileData, (err) => { - if (err) - console.log(err); - }); - } + if (config.txtFile === true) { + fileData = "Now Playing: " + audio + writeFile("./now-playing.txt", fileData, (err) => { + if (err) + console.log(err); + }); + } - const statusEmbed = new MessageEmbed() - .addField('Now Playing', `${audio}`) - .setColor('#0066ff') + const statusEmbed = new MessageEmbed() + .addField('Now Playing', `${audio}`) + .setColor('#0066ff') - let statusChannel = bot.channels.cache.get(config.statusChannel); - if (!statusChannel) return console.error('The status channel does not exist! Skipping.'); - return await statusChannel.send({embeds: [statusEmbed]}); + let statusChannel = bot.channels.cache.get(config.statusChannel); + if (!statusChannel) return console.error('The status channel does not exist! Skipping.'); + return await statusChannel.send({embeds: [statusEmbed]}); } |
