aboutsummaryrefslogtreecommitdiff
path: root/AudioBackend.js
diff options
context:
space:
mode:
Diffstat (limited to 'AudioBackend.js')
-rw-r--r--AudioBackend.js180
1 files changed, 89 insertions, 91 deletions
diff --git a/AudioBackend.js b/AudioBackend.js
index 64f85b1..ed30afe 100644
--- a/AudioBackend.js
+++ b/AudioBackend.js
@@ -1,9 +1,9 @@
/**************************************************************************
- *
+ *
* DLAP Bot: A Discord bot that plays local audio tracks.
* (C) Copyright 2022
- * Programmed by Andrew Lee
- *
+ * 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
@@ -16,7 +16,7 @@
*
* 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 {
@@ -25,14 +25,15 @@ import {
getVoiceConnection,
joinVoiceChannel,
VoiceConnectionStatus
-} from '@discordjs/voice'
-import { MessageEmbed } from 'discord.js'
-import config from './config.json' assert {type: 'json'}
-import { readdirSync, writeFile } from 'node:fs'
+} from '@discordjs/voice';
+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'));
export const player = createAudioPlayer();
export let audio;
-export let files = readdirSync('music');
+export const files = readdirSync('music');
let fileData;
export let audioArray;
export let currentTrack;
@@ -40,117 +41,114 @@ export let currentTrack;
export let playerState;
export let isAudioStatePaused;
-export async function voiceInit(bot) {
+export async function voiceInit (bot) {
bot.channels.fetch(config.voiceChannel).then(async channel => {
const connection = joinVoiceChannel({
- channelId: channel.id,
- guildId: channel.guild.id,
- adapterCreator: channel.guild.voiceAdapterCreator
+ channelId: channel.id,
+ guildId: channel.guild.id,
+ adapterCreator: channel.guild.voiceAdapterCreator
});
connection.on(VoiceConnectionStatus.Ready, async () => {
- console.log('Ready to blast some beats!');
- await shufflePlaylist(bot);
+ console.log('Ready to blast some beats!');
+ await shufflePlaylist(bot);
});
connection.on(VoiceConnectionStatus.Destroyed, () => {
- console.log('Destroyed the beats...');
+ console.log('Destroyed the beats...');
});
player.on('error', error => {
- console.error(error);
- nextAudio(bot);
+ console.error(error);
+ nextAudio(bot);
});
player.on('idle', () => {
- console.log("Beat has finished playing, now to the next beat...");
- nextAudio(bot);
- })
+ console.log('Beat has finished playing, now to the next beat...');
+ nextAudio(bot);
+ });
return connection.subscribe(player);
- }).catch(e => { console.error(e) })
+ }).catch(e => { console.error(e); });
}
-function shuffleArray(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]];
- }
+function shuffleArray (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) {
- console.log('Shuffling beats...');
- currentTrack = 0
- audioArray = files;
- shuffleArray(audioArray);
- console.log(audioArray);
- audio = audioArray[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];
+ return await playAudio(bot);
}
-export async function nextAudio(bot) {
- let totalTrack = files.length
- totalTrack--
+export async function nextAudio (bot) {
+ let totalTrack = files.length;
+ totalTrack--;
- if (currentTrack >= totalTrack) {
- console.log('All beats in the playlist has finished, reshuffling...');
- await shufflePlaylist(bot);
- } else {
- currentTrack++
- audio = audioArray[currentTrack];
- }
+ if (currentTrack >= totalTrack) {
+ console.log('All beats in the playlist has finished, reshuffling...');
+ await shufflePlaylist(bot);
+ } else {
+ currentTrack++;
+ audio = audioArray[currentTrack];
+ }
- return await playAudio(bot);
+ return await playAudio(bot);
}
-export async function inputAudio(bot, integer) {
- let inputFiles = readdirSync('music');
- audio = inputFiles[integer];
- return await playAudio(bot);
+export async function inputAudio (bot, integer) {
+ const inputFiles = readdirSync('music');
+ audio = inputFiles[integer];
+ return await playAudio(bot);
}
-export async function playAudio(bot) {
- let resource = createAudioResource('music/' + audio);
-
- player.play(resource);
+export async function playAudio (bot) {
+ const resource = createAudioResource('music/' + audio);
- console.log('Now playing: ' + audio);
+ player.play(resource);
- playerState = "Playing"
- isAudioStatePaused = false
+ console.log('Now playing: ' + audio);
- audio = audio.split('.').slice(0, -1).join('.');
+ playerState = 'Playing';
+ isAudioStatePaused = false;
- if (config.txtFile === true) {
- fileData = "Now Playing: " + audio
- writeFile("./now-playing.txt", fileData, (err) => {
- if (err)
- console.log(err);
- });
- }
+ audio = audio.split('.').slice(0, -1).join('.');
- const statusEmbed = new MessageEmbed()
- .addField('Now Playing', `${audio}`)
- .setColor('#0066ff')
+ if (config.txtFile === true) {
+ fileData = 'Now Playing: ' + audio;
+ writeFile('./now-playing.txt', fileData, (err) => {
+ if (err) { console.log(err); }
+ });
+ }
- 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]});
+ const statusEmbed = new MessageEmbed()
+ .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] });
}
-export async function destroyAudio(interaction) {
+export async function destroyAudio (interaction) {
if (config.txtFile === true) {
- fileData = "Now Playing: Nothing";
- writeFile("now-playing.txt", fileData, (err) => {
- if (err)
- console.log(err);
+ fileData = 'Now Playing: Nothing';
+ writeFile('now-playing.txt', fileData, (err) => {
+ if (err) { console.log(err); }
});
}
- audio = "Not Playing"
- playerState = "Stopped"
- isAudioStatePaused = true
+ audio = 'Not Playing';
+ playerState = 'Stopped';
+ isAudioStatePaused = true;
const connection = getVoiceConnection(interaction.guild.id);
if (VoiceConnectionStatus.Ready) {
@@ -159,27 +157,27 @@ export async function destroyAudio(interaction) {
}
}
-export function audioState() {
+export function audioState () {
if (isAudioStatePaused === false) {
- isAudioStatePaused = true
- playerState = "Paused"
+ isAudioStatePaused = true;
+ playerState = 'Paused';
} else if (isAudioStatePaused === true) {
- isAudioStatePaused = false
- playerState = "Playing"
+ isAudioStatePaused = false;
+ playerState = 'Playing';
}
}
-export async function stopBot(bot, interaction) {
+export async function stopBot (bot, interaction) {
const statusEmbed = new MessageEmbed()
- .setAuthor({name: bot.user.username, iconURL: bot.user.avatarURL()})
- .setDescription(`That\'s all folks! Powering down ${bot.user.username}...`)
- .setColor('#0066ff')
- let statusChannel = bot.channels.cache.get(config.statusChannel);
+ .setAuthor({ name: bot.user.username, iconURL: bot.user.avatarURL() })
+ .setDescription(`That's all folks! Powering down ${bot.user.username}...`)
+ .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: [statusEmbed]});
+ await statusChannel.send({ embeds: [statusEmbed] });
console.log(`Powering off ${bot.user.username}...`);
await destroyAudio(interaction);
bot.destroy();
return process.exit(0);
-} \ No newline at end of file
+}