diff options
| author | Andrew Lee <alee14498@protonmail.com> | 2022-07-11 23:14:58 -0400 |
|---|---|---|
| committer | Andrew Lee <alee14498@protonmail.com> | 2022-07-11 23:14:58 -0400 |
| commit | 1cd0056d71ab84421b40538ff7b0bb76a2e6a766 (patch) | |
| tree | 2aa0f2334d68cf3ed8809ce82b87e55b90fd4776 | |
| parent | eb90e1a4ffbe5f0b82bea296a97786ca5cc81041 (diff) | |
| download | DLAP-1cd0056d71ab84421b40538ff7b0bb76a2e6a766.tar.gz DLAP-1cd0056d71ab84421b40538ff7b0bb76a2e6a766.tar.bz2 DLAP-1cd0056d71ab84421b40538ff7b0bb76a2e6a766.zip | |
Fully implemented order playlist
| -rw-r--r-- | AudioBackend.js | 40 | ||||
| -rw-r--r-- | README.md | 5 | ||||
| -rw-r--r-- | bot.js | 15 | ||||
| -rw-r--r-- | commands/reshuffle.js | 12 | ||||
| -rw-r--r-- | commands/status.js | 4 |
5 files changed, 47 insertions, 29 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); } @@ -6,12 +6,17 @@ A Discord bot that plays local audio tracks. Written in Discord.JS. If you want to add a feature or there's anything wrong, feel free to make a fork and put a pull request. +# Recommended Software +- Latest version of NodeJS (v18.5.0+) +- Linux (or WSL for Windows users) + # Configuration Make a new file called `config.json`. ``` { "token": "token_here", "txtFile": true/false, + "shuffle": true/false, "statusChannel": "channel_id", "voiceChannel": "voice_channel_id" "guildID": "guild_id", @@ -23,11 +23,11 @@ import { voiceInit } from './AudioBackend.js'; import { readdirSync, readFileSync } from 'node:fs'; import { webServer } from './WebStream.js'; // import config from './config.json' assert { type: 'json' } Not supported by ESLint yet -const config = JSON.parse(readFileSync('./config.json')); +const { token, statusChannel, voiceChannel, shuffle } = JSON.parse(readFileSync('./config.json')); const bot = new Client({ intents: ['GUILDS', 'GUILD_MESSAGES', 'GUILD_VOICE_STATES'] }); -bot.login(config.token); +bot.login(token); // webServer(); @@ -50,8 +50,9 @@ bot.once('ready', async() => { console.log('Bot is ready!'); console.log(`Logged in as ${bot.user.tag}!`); console.log(`Running on Discord.JS ${version}`); - console.log(`Voice Channel: ${config.voiceChannel}`); - console.log(`Status Channel: ${config.statusChannel}`); + console.log(`Voice Channel: ${voiceChannel}`); + console.log(`Status Channel: ${statusChannel}`); + console.log(`Shuffle enabled: ${shuffle}`); // Set bots' presence bot.user.setPresence({ @@ -71,9 +72,9 @@ bot.once('ready', async() => { .setDescription('Starting bot...') .setColor('#0066ff'); - const statusChannel = bot.channels.cache.get(config.statusChannel); - if (!statusChannel) return console.error('The status channel does not exist! Skipping.'); - await statusChannel.send({ embeds: [readyEmbed] }); + const channel = bot.channels.cache.get(statusChannel); + if (!channel) return console.error('The status channel does not exist! Skipping.'); + await channel.send({ embeds: [readyEmbed] }); return await voiceInit(bot); }); diff --git a/commands/reshuffle.js b/commands/reshuffle.js index 162a52e..e4e6745 100644 --- a/commands/reshuffle.js +++ b/commands/reshuffle.js @@ -22,6 +22,9 @@ import { SlashCommandBuilder } from '@discordjs/builders'; import { player, shufflePlaylist } from '../AudioBackend.js'; import { PermissionFlagsBits } from 'discord-api-types/v10'; +import { readFileSync } from 'node:fs'; +// import config from './config.json' assert {type: 'json'} +const { shuffle } = JSON.parse(readFileSync('./config.json')); export default { data: new SlashCommandBuilder() @@ -29,8 +32,11 @@ export default { .setDescription('Reshuffles the playlist') .setDefaultMemberPermissions(PermissionFlagsBits.Administrator), async execute(interaction, bot) { - await interaction.reply({ content: 'Reshuffling the playlist...', ephemeral: true }); - player.stop(); - return await shufflePlaylist(bot); + async function shuffleDetected(bot) { + await interaction.reply({ content: 'Reshuffling the playlist...', ephemeral: true }); + player.stop(); + await shufflePlaylist(bot); + } + return (shuffle === true) ? await shuffleDetected(bot) : await interaction.reply({ content: 'Shuffle mode is disabled, enable it on the configuration to access this command.', ephemeral: true }); } }; diff --git a/commands/status.js b/commands/status.js index d769cb5..2bb7703 100644 --- a/commands/status.js +++ b/commands/status.js @@ -21,7 +21,7 @@ import { SlashCommandBuilder } from '@discordjs/builders'; import { MessageEmbed } from 'discord.js'; -import { audio, audioArray, currentTrack, files, playerState } from '../AudioBackend.js'; +import { audio, currentTrack, files, playerState } from '../AudioBackend.js'; export default { data: new SlashCommandBuilder() @@ -31,7 +31,7 @@ export default { let audioID = currentTrack; audioID++; - let audioName = audioArray[audioID]; + let audioName = files[audioID]; if (audioName === undefined) { audioName = 'Playlist Finished'; } else { |
