aboutsummaryrefslogtreecommitdiff
path: root/AudioBackend.js
diff options
context:
space:
mode:
authorAndrew Lee <alee14498@protonmail.com>2022-07-11 23:14:58 -0400
committerAndrew Lee <alee14498@protonmail.com>2022-07-11 23:14:58 -0400
commit1cd0056d71ab84421b40538ff7b0bb76a2e6a766 (patch)
tree2aa0f2334d68cf3ed8809ce82b87e55b90fd4776 /AudioBackend.js
parenteb90e1a4ffbe5f0b82bea296a97786ca5cc81041 (diff)
downloadDLAP-1cd0056d71ab84421b40538ff7b0bb76a2e6a766.tar.gz
DLAP-1cd0056d71ab84421b40538ff7b0bb76a2e6a766.tar.bz2
DLAP-1cd0056d71ab84421b40538ff7b0bb76a2e6a766.zip
Fully implemented order playlist
Diffstat (limited to 'AudioBackend.js')
-rw-r--r--AudioBackend.js40
1 files changed, 23 insertions, 17 deletions
diff --git a/AudioBackend.js b/AudioBackend.js
index 8b02766..5948789 100644
--- a/AudioBackend.js
+++ b/AudioBackend.js
@@ -29,20 +29,19 @@ import {
import { MessageEmbed } from 'discord.js';
import { readdirSync, readFileSync, writeFile } from 'node:fs';
// import config from './config.json' assert {type: 'json'}
-const config = JSON.parse(readFileSync('./config.json'));
+const { voiceChannel, statusChannel, shuffle, txtFile } = JSON.parse(readFileSync('./config.json'));
export const player = createAudioPlayer();
export let audio;
export const files = readdirSync('music');
let fileData;
-export let audioArray;
export let currentTrack;
export let playerState;
export let isAudioStatePaused;
export async function voiceInit(bot) {
- bot.channels.fetch(config.voiceChannel).then(async channel => {
+ bot.channels.fetch(voiceChannel).then(async channel => {
const connection = joinVoiceChannel({
channelId: channel.id,
guildId: channel.guild.id,
@@ -51,7 +50,7 @@ export async function voiceInit(bot) {
connection.on(VoiceConnectionStatus.Ready, async() => {
console.log('Ready to blast some beats!');
- await shufflePlaylist(bot);
+ return (shuffle === true) ? await shufflePlaylist(bot) : await orderPlaylist(bot);
});
connection.on(VoiceConnectionStatus.Destroyed, () => {
@@ -79,26 +78,33 @@ function shuffleArray(array) {
}
}
+export async function orderPlaylist(bot) {
+ console.log('Playing beats by order...');
+ currentTrack = 0;
+ console.log(files);
+ audio = files[currentTrack];
+ return await playAudio(bot);
+}
+
export async function shufflePlaylist(bot) {
console.log('Shuffling beats...');
currentTrack = 0;
- audioArray = files;
- shuffleArray(audioArray);
- console.log(audioArray);
- audio = audioArray[currentTrack];
+ shuffleArray(files);
+ console.log(files);
+ console.log('Playing beats by shuffle...');
+ audio = files[currentTrack];
return await playAudio(bot);
}
export async function nextAudio(bot) {
let totalTrack = files.length;
totalTrack--;
-
if (currentTrack >= totalTrack) {
- console.log('All beats in the playlist has finished, reshuffling...');
- return await shufflePlaylist(bot);
+ console.log('All beats in the playlist has finished, repeating beats...');
+ return (shuffle === true) ? await shufflePlaylist(bot) : await orderPlaylist(bot);
} else {
currentTrack++;
- audio = audioArray[currentTrack];
+ audio = files[currentTrack];
return await playAudio(bot);
}
}
@@ -121,7 +127,7 @@ export async function playAudio(bot) {
audio = audio.split('.').slice(0, -1).join('.');
- if (config.txtFile === true) {
+ if (txtFile === true) {
fileData = 'Now Playing: ' + audio;
writeFile('./now-playing.txt', fileData, (err) => {
if (err) { console.log(err); }
@@ -132,13 +138,13 @@ export async function playAudio(bot) {
.addField('Now Playing', `${audio}`)
.setColor('#0066ff');
- const 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] });
+ const channel = bot.channels.cache.get(statusChannel);
+ if (!channel) return console.error('The status channel does not exist! Skipping.');
+ return await channel.send({ embeds: [statusEmbed] });
}
export async function destroyAudio(interaction) {
- if (config.txtFile === true) {
+ if (txtFile === true) {
fileData = 'Now Playing: Nothing';
writeFile('now-playing.txt', fileData, (err) => {
if (err) { console.log(err); }