From 83e4c8679c656ecb352ddc34d5dced9518ba240a Mon Sep 17 00:00:00 2001 From: Andrew Lee Date: Thu, 7 Jul 2022 23:49:37 -0400 Subject: New shuffling system (not bug free yet); Tweaks and fixes on other commands --- AudioBackend.js | 53 +++++++++++++++++++++++++++++++++++++++++++++------- README.md | 2 +- bot.js | 5 ++--- commands/play.js | 10 +++++++--- commands/shutdown.js | 35 ++++++++++++++++++++++++++++++++++ commands/skip.js | 4 ++-- commands/status.js | 6 ++++-- commands/stop.js | 35 ---------------------------------- package.json | 2 +- 9 files changed, 98 insertions(+), 54 deletions(-) create mode 100644 commands/shutdown.js delete mode 100644 commands/stop.js 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) { diff --git a/README.md b/README.md index 3078d46..66e9108 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ play (int) - Input a number for the selection for the audio file. pause - Pauses music. skip - Skips the audio track. leave - Leaves voice chat. -stop - Powers off the bot. +shutdown - Powers off the bot. ``` # Forking diff --git a/bot.js b/bot.js index 0871112..75a217c 100644 --- a/bot.js +++ b/bot.js @@ -23,14 +23,13 @@ import { voiceInit } from './AudioBackend.js' import { readdirSync } from 'node:fs' import config from './config.json' assert { type: 'json' } -export const bot = new Client({intents: ['GUILDS', 'GUILD_MESSAGES', 'GUILD_VOICE_STATES']}); +const bot = new Client({intents: ['GUILDS', 'GUILD_MESSAGES', 'GUILD_VOICE_STATES']}); bot.login(config.token); /** * Project Ideas: - * New queue system * Shuffle or "Play by order" mode * Audio streaming */ @@ -92,7 +91,7 @@ bot.on('interactionCreate', async interaction => { if (e == null) { await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true }); } else { - await interaction.reply({ content: `There was an error while executing this command! Share this to the bot owner!\n\nDetails:\`\`\`${e}\`\`\``, ephemeral: true }); + await interaction.reply({ content: `There was an error while executing this command!\nShare this to the bot owner!\n\nDetails:\`\`\`${e}\`\`\``, ephemeral: true }); } } }); diff --git a/commands/play.js b/commands/play.js index 2785288..d206c56 100644 --- a/commands/play.js +++ b/commands/play.js @@ -20,7 +20,7 @@ ***************************************************************************/ import { SlashCommandBuilder } from '@discordjs/builders' -import { isAudioStatePaused, inputAudio, audio, audioState, player } from '../AudioBackend.js' +import { isAudioStatePaused, inputAudio, audio, audioState, player, files } from '../AudioBackend.js' import { PermissionFlagsBits } from "discord-api-types/v10" export let integer; @@ -38,8 +38,12 @@ export default { async execute(interaction, bot) { integer = interaction.options.getInteger('int'); if (integer) { - await inputAudio(bot, integer); - return await interaction.reply({ content: `Now playing: ${audio}`, ephemeral: true }); + if (integer < files.length) { + await inputAudio(bot, integer); + return await interaction.reply({content:`Now playing: ${audio}`, ephemeral:true}); + } else { + return await interaction.reply({content:'Number is too big, choose a number that\'s less than ' + files.length + '.', ephemeral:true}) + } } if (isAudioStatePaused === true) { audioState(); diff --git a/commands/shutdown.js b/commands/shutdown.js new file mode 100644 index 0000000..4c32bc6 --- /dev/null +++ b/commands/shutdown.js @@ -0,0 +1,35 @@ +/************************************************************************** + * + * DLAP Bot: A Discord bot that plays local audio tracks. + * (C) Copyright 2022 + * Programmed by Andrew Lee + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + ***************************************************************************/ + +import { SlashCommandBuilder } from '@discordjs/builders' +import { stopBot } from "../AudioBackend.js" +import { PermissionFlagsBits } from "discord-api-types/v10" + +export default { + data: new SlashCommandBuilder() + .setName('shutdown') + .setDescription('Powers off the bot') + .setDefaultMemberPermissions(PermissionFlagsBits.Administrator), + async execute(interaction, bot) { + await interaction.reply({ content: 'Powering off...', ephemeral: true}) + return await stopBot(bot, interaction); + }, +}; \ No newline at end of file diff --git a/commands/skip.js b/commands/skip.js index 3e1a836..3003ca0 100644 --- a/commands/skip.js +++ b/commands/skip.js @@ -20,7 +20,7 @@ ***************************************************************************/ import { SlashCommandBuilder } from '@discordjs/builders' -import { audio, player, searchAudio } from "../AudioBackend.js" +import { audio, player, nextAudio } from "../AudioBackend.js" import { PermissionFlagsBits } from "discord-api-types/v10" export default { @@ -31,6 +31,6 @@ export default { async execute(interaction, bot) { await interaction.reply({content:`Skipping ${audio}`, ephemeral:true}); player.stop(); - return await searchAudio(bot, interaction); + return await nextAudio(bot, interaction); }, }; \ No newline at end of file diff --git a/commands/status.js b/commands/status.js index d7012ce..e012239 100644 --- a/commands/status.js +++ b/commands/status.js @@ -21,18 +21,20 @@ import { SlashCommandBuilder } from '@discordjs/builders' import { MessageEmbed } from "discord.js" -import { audio, playerState } from "../AudioBackend.js" +import {audio, audioArray, currentTrack, playerState} from "../AudioBackend.js" export default { data: new SlashCommandBuilder() .setName('status') .setDescription('Checks what audio file is playing currently'), async execute(interaction, bot) { + let nextAudio = currentTrack + nextAudio++ let controlEmbed = new MessageEmbed() .setAuthor({name: `${bot.user.username} Status`, iconURL: bot.user.avatarURL()}) .addField('State', playerState) .addField('Currently Playing', audio) - //.addField('Next Music', '(a possible feature when queue system is implemented?)') + .addField('Next Music', audioArray[nextAudio]) .setColor('#0066ff') interaction.reply({embeds:[controlEmbed], ephemeral:true}) }, diff --git a/commands/stop.js b/commands/stop.js deleted file mode 100644 index c57301e..0000000 --- a/commands/stop.js +++ /dev/null @@ -1,35 +0,0 @@ -/************************************************************************** - * - * DLAP Bot: A Discord bot that plays local audio tracks. - * (C) Copyright 2022 - * Programmed by Andrew Lee - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - * - ***************************************************************************/ - -import { SlashCommandBuilder } from '@discordjs/builders' -import { stopBot } from "../AudioBackend.js" -import { PermissionFlagsBits } from "discord-api-types/v10" - -export default { - data: new SlashCommandBuilder() - .setName('stop') - .setDescription('Powers off the bot') - .setDefaultMemberPermissions(PermissionFlagsBits.Administrator), - async execute(interaction, bot) { - await interaction.reply({ content: 'Powering off...', ephemeral: true}) - return await stopBot(bot, interaction); - }, -}; \ No newline at end of file diff --git a/package.json b/package.json index 91c8b01..6a93082 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "dlap", - "version": "1.2.0", + "version": "1.3.0", "type": "module", "main": "bot.js", "license": "GPL-3.0", -- cgit v1.2.3