diff options
| author | Andrew Lee <alee14498@protonmail.com> | 2022-12-01 21:56:03 -0500 |
|---|---|---|
| committer | Andrew Lee <alee14498@protonmail.com> | 2022-12-01 21:56:03 -0500 |
| commit | 894dd858c380e5ac8bcd882294f044c325ef379e (patch) | |
| tree | b275ce664f6cf48272d62835bd3010c0e71bc592 /backend | |
| parent | 56573af6e6f5a839536483ea51ad1fc163f33458 (diff) | |
| download | DLAP-894dd858c380e5ac8bcd882294f044c325ef379e.tar.gz DLAP-894dd858c380e5ac8bcd882294f044c325ef379e.tar.bz2 DLAP-894dd858c380e5ac8bcd882294f044c325ef379e.zip | |
Modularized audio backend
Diffstat (limited to 'backend')
| -rw-r--r-- | backend/AudioControl.js | 82 | ||||
| -rw-r--r-- | backend/PlayAudio.js | 128 | ||||
| -rw-r--r-- | backend/QueueSystem.js | 50 | ||||
| -rw-r--r-- | backend/Shutdown.js | 60 | ||||
| -rw-r--r-- | backend/VoiceInitialization.js | 57 |
5 files changed, 377 insertions, 0 deletions
diff --git a/backend/AudioControl.js b/backend/AudioControl.js new file mode 100644 index 0000000..f26d9d1 --- /dev/null +++ b/backend/AudioControl.js @@ -0,0 +1,82 @@ +/************************************************************************** + * + * 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 <https://www.gnu.org/licenses/>. + * + ***************************************************************************/ +import { readdirSync, readFileSync } from 'node:fs'; +import { shufflePlaylist, orderPlaylist } from './QueueSystem.js'; +import { playAudio, currentTrack, updatePlaylist } from './PlayAudio.js'; +import { player } from './VoiceInitialization.js'; + +const { shuffle } = JSON.parse(readFileSync('./config.json', 'utf-8')); +export const files = readdirSync('music'); +export let playerState; +export let isAudioStatePaused; + +let totalTrack = files.length; + +export async function nextAudio(bot) { + totalTrack--; + if (currentTrack >= totalTrack) { + console.log('All beats in the playlist has finished, repeating beats...'); + totalTrack = files.length; + return (shuffle === true) ? await shufflePlaylist(bot) : await orderPlaylist(bot); + } else { + updatePlaylist('next'); + return await playAudio(bot); + } +} + +export async function previousAudio(bot, interaction) { + totalTrack++; + if (currentTrack <= 0) { + return await interaction.reply({ content: 'You are at the beginning of the playlist, cannot go further than this', ephemeral: true }); + } else { + await interaction.reply({ content: 'Playing previous music', ephemeral: true }); + player.stop(); + updatePlaylist('back'); + return await playAudio(bot); + } +} + +export function toggleAudioState() { + if (isAudioStatePaused === true) { + audioState(0); + } else { + audioState(1); + } +} + +export function audioState(state) { + switch (state) { + case 0: + playerState = 'Playing'; + isAudioStatePaused = false; + player.unpause(); + break; + case 1: + playerState = 'Paused'; + isAudioStatePaused = true; + player.pause(); + break; + case 2: + playerState = 'Stopped'; + isAudioStatePaused = true; + break; + } +} diff --git a/backend/PlayAudio.js b/backend/PlayAudio.js new file mode 100644 index 0000000..77a33bc --- /dev/null +++ b/backend/PlayAudio.js @@ -0,0 +1,128 @@ +/************************************************************************** + * + * 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 <https://www.gnu.org/licenses/>. + * + ***************************************************************************/ +import { createAudioResource } from '@discordjs/voice'; +import { parseFile } from 'music-metadata'; +import { readdirSync, readFileSync, writeFile } from 'node:fs'; +import { EmbedBuilder } from 'discord.js'; +import { player } from './VoiceInitialization.js'; +import { audioState, files } from './AudioControl.js'; +import { integer } from '../commands/play.js'; +const { statusChannel, txtFile } = JSON.parse(readFileSync('./config.json', 'utf-8')); + +let fileData; +export let audio; +export let duration; +export let metadataEmpty = false; +export let audioTitle; +export let audioArtist; +export let audioYear; +export let audioAlbum; +export let currentTrack; + +const inputFiles = readdirSync('music'); +export async function playAudio(bot) { + const resource = createAudioResource('music/' + audio); + player.play(resource); + + console.log('Now playing: ' + audio); + + audioState(0); + + const audioFile = audio; + + try { + const { common, format } = await parseFile('music/' + audio); + metadataEmpty = false; + if (common.title && common.artist && common.year && common.album) { + audioTitle = common.title; + audioArtist = common.artist; + audioYear = common.year; + audioAlbum = common.album; + } else { + metadataEmpty = true; + } + const toHHMMSS = (numSecs) => { + const secNum = parseInt(numSecs, 10); + const hours = Math.floor(secNum / 3600).toString().padStart(2, '0'); + const minutes = Math.floor((secNum - (hours * 3600)) / 60).toString().padStart(2, '0'); + const seconds = secNum - (hours * 3600) - (minutes * 60).toString().padStart(2, '0'); + return `${hours}:${minutes}:${seconds}`; + }; + duration = toHHMMSS(format.duration); + } catch (e) { + console.error(e); + } + + audio = audio.split('.').slice(0, -1).join('.'); + + if (txtFile === true) { + fileData = 'Now Playing: ' + audio; + writeFile('./now-playing.txt', fileData, (err) => { + if (err) { console.log(err); } + }); + } + + const statusEmbed = new EmbedBuilder(); + if (metadataEmpty === true) { + statusEmbed.setTitle('Now Playing'); + statusEmbed.addFields( + { name: 'Title', value: audio }, + { name: 'Duration', value: duration } + ); + statusEmbed.setColor('#0066ff'); + } else { + statusEmbed.setTitle('Now Playing'); + statusEmbed.addFields( + { name: 'Title', value: audioTitle, inline: true }, + { name: 'Artist', value: audioArtist, inline: true }, + { name: 'Year', value: `${audioYear}` }, + { name: 'Duration', value: duration } + ); + statusEmbed.setFooter({ text: `Album: ${audioAlbum}\nFilename: ${audioFile}` }); + statusEmbed.setColor('#0066ff'); + } + 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 function updatePlaylist(i) { + switch (i) { + case 'next': + currentTrack++; + audio = files[currentTrack]; + break; + case 'back': + currentTrack--; + audio = files[currentTrack]; + break; + case 'reset': + currentTrack = 0; + audio = files[currentTrack]; + break; + case 'input': + audio = inputFiles[integer]; + break; + case 'stop': + audio = 'Not Playing'; + break; + } +} diff --git a/backend/QueueSystem.js b/backend/QueueSystem.js new file mode 100644 index 0000000..0cd6ded --- /dev/null +++ b/backend/QueueSystem.js @@ -0,0 +1,50 @@ +/************************************************************************** + * + * 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 <https://www.gnu.org/licenses/>. + * + ***************************************************************************/ +import { playAudio, updatePlaylist } from './PlayAudio.js'; +import { files } from './AudioControl.js'; + +function shuffleArray(array) { + // Durstenfeld Shuffle + 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 orderPlaylist(bot) { + console.log('Playing beats by order...'); + updatePlaylist('reset'); + console.log(files); + return await playAudio(bot); +} + +export async function shufflePlaylist(bot) { + console.log('Shuffling beats...'); + shuffleArray(files); + console.log('Playing beats by shuffle...'); + updatePlaylist('reset'); + console.log(files); + return await playAudio(bot); +} + +export async function inputAudio(bot) { + updatePlaylist('input'); + return await playAudio(bot); +} diff --git a/backend/Shutdown.js b/backend/Shutdown.js new file mode 100644 index 0000000..641ca1e --- /dev/null +++ b/backend/Shutdown.js @@ -0,0 +1,60 @@ +/************************************************************************** + * + * 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 <https://www.gnu.org/licenses/>. + * + ***************************************************************************/ +import { EmbedBuilder } from 'discord.js'; +import { player } from './VoiceInitialization.js'; +import { updatePlaylist } from './PlayAudio.js'; +import { audioState } from './AudioControl.js'; +import { readFileSync, writeFile } from 'node:fs'; +import { getVoiceConnection, VoiceConnectionStatus } from '@discordjs/voice'; +const { statusChannel, txtFile } = JSON.parse(readFileSync('./config.json', 'utf-8')); +let fileData; + +export async function destroyAudio(interaction) { + if (txtFile === true) { + fileData = 'Now Playing: Nothing'; + writeFile('now-playing.txt', fileData, (err) => { + if (err) { console.log(err); } + }); + } + + updatePlaylist('stop'); + audioState(2); + + const connection = getVoiceConnection(interaction.guild.id); + if (VoiceConnectionStatus.Ready) { + player.stop(); + return connection.destroy(); + } +} +export async function stopBot(bot, interaction) { + const statusEmbed = new EmbedBuilder() + .setAuthor({ name: bot.user.username, iconURL: bot.user.avatarURL() }) + .setDescription(`That's all folks! Powering down ${bot.user.username}...`) + .setColor('#0066ff'); + const channel = bot.channels.cache.get(statusChannel); + if (!channel) return console.error('The status channel does not exist! Skipping.'); + await channel.send({ embeds: [statusEmbed] }); + + console.log(`Powering off ${bot.user.username}...`); + await destroyAudio(interaction); + bot.destroy(); + return process.exit(0); +} diff --git a/backend/VoiceInitialization.js b/backend/VoiceInitialization.js new file mode 100644 index 0000000..61d4734 --- /dev/null +++ b/backend/VoiceInitialization.js @@ -0,0 +1,57 @@ +/************************************************************************** + * + * 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 <https://www.gnu.org/licenses/>. + * + ***************************************************************************/ +import { readFileSync } from 'node:fs'; +import { createAudioPlayer, joinVoiceChannel, VoiceConnectionStatus } from '@discordjs/voice'; +import { nextAudio } from './AudioControl.js'; +import { shufflePlaylist, orderPlaylist } from './QueueSystem.js'; + +const { voiceChannel, shuffle } = JSON.parse(readFileSync('./config.json', 'utf-8')); +export const player = createAudioPlayer(); +export async function voiceInit(bot) { + bot.channels.fetch(voiceChannel).then(async channel => { + const connection = joinVoiceChannel({ + channelId: channel.id, + guildId: channel.guild.id, + adapterCreator: channel.guild.voiceAdapterCreator + }); + + connection.on(VoiceConnectionStatus.Ready, async() => { + console.log('Ready to blast some beats!'); + return (shuffle === true) ? await shufflePlaylist(bot) : await orderPlaylist(bot); + }); + + connection.on(VoiceConnectionStatus.Destroyed, () => { + console.log('Destroyed the beats...'); + }); + + player.on('error', error => { + console.error(error); + nextAudio(bot); + }); + + player.on('idle', () => { + console.log('Beat has finished playing, now playing next beat...'); + nextAudio(bot); + }); + + return connection.subscribe(player); + }).catch(e => { console.error(e); }); +} |
