Audio stuff is now on its own file

This commit is contained in:
Andrew Lee 2022-03-26 21:07:35 -04:00
parent 99c86c8078
commit ae48ba2014
Signed by: andrew
GPG key ID: 4DCE67C47836D125
4 changed files with 108 additions and 73 deletions

92
AudioBackend.js Normal file
View file

@ -0,0 +1,92 @@
/**************************************************************************
*
* DLMP3 Bot: A Discord bot that plays local mp3 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/>.
*
***************************************************************************/
const { createAudioPlayer, createAudioResource, joinVoiceChannel, VoiceConnectionStatus } = require('@discordjs/voice');
const player = createAudioPlayer();
const fs = require('fs');
const { join } = require('node:path');
let audio;
let fileData;
let txtFile = true;
const { MessageEmbed } = require('discord.js');
module.exports = (bot, config) => {
function voiceInit() {
bot.channels.fetch(config.voiceChannel).then(channel => {
const connection = joinVoiceChannel({
channelId: channel.id,
guildId: channel.guild.id,
adapterCreator: channel.guild.voiceAdapterCreator
});
connection.on(VoiceConnectionStatus.Ready, () => {
console.log('Ready to blast music!');
});
connection.on(VoiceConnectionStatus.Destroyed, () => {
console.log('Destroying beats...');
});
player.on('idle', () => {
console.log("Music has finished playing, shuffling music...")
playAudio();
})
playAudio();
connection.subscribe(player);
})
}
function playAudio() {
let files = fs.readdirSync(join(__dirname,'music'));
while (true) {
audio = files[Math.floor(Math.random() * files.length)];
console.log('Searching .mp3 file...');
if (audio.endsWith('.mp3')) {
break;
}
}
let resource = createAudioResource(join(__dirname, 'music/' + audio));
player.play(resource);
console.log('Now playing: ' + audio);
if (txtFile === true) {
fileData = "Now Playing: " + audio;
fs.writeFile("./now-playing.txt", fileData, (err) => {
if (err)
console.log(err);
});
}
const statusEmbed = new MessageEmbed()
.addField('Now Playing', `${audio}`)
.setColor('#0066ff')
let statusChannel = bot.channels.cache.get(config.statusChannel);
if (!statusChannel) return console.error('The status channel does not exist! Skipping.');
statusChannel.send({embeds: [statusEmbed]});
}
voiceInit();
}

86
bot.js
View file

@ -20,76 +20,16 @@
***************************************************************************/
const { Client, MessageEmbed, Collection, version } = require('discord.js');
const fs = require('fs');
const { join } = require('node:path');
const { createAudioPlayer, createAudioResource, joinVoiceChannel, VoiceConnectionStatus, getVoiceConnection } = require('@discordjs/voice');
const { getVoiceConnection } = require('@discordjs/voice');
const bot = new Client({intents: ['GUILDS', 'GUILD_MESSAGES', 'GUILD_VOICE_STATES']});
const config = require('./config.json');
const player = createAudioPlayer();
const AudioBackend = require('./AudioBackend');
let audio;
let fileData;
let txtFile = true;
bot.login(config.token);
function voiceInit() {
bot.channels.fetch(config.voiceChannel).then(channel => {
const connection = joinVoiceChannel({
channelId: channel.id,
guildId: channel.guild.id,
adapterCreator: channel.guild.voiceAdapterCreator
});
connection.on(VoiceConnectionStatus.Ready, () => {
console.log('Ready to blast music!');
});
connection.on(VoiceConnectionStatus.Destroyed, () => {
console.log('Destroying beats...');
});
player.on('idle', () => {
console.log("Music has finished playing, shuffling music...")
playAudio();
})
playAudio();
connection.subscribe(player);
})
}
function playAudio() {
let files = fs.readdirSync(join(__dirname,'music'));
while (true) {
audio = files[Math.floor(Math.random() * files.length)];
console.log('Searching .mp3 file...');
if (audio.endsWith('.mp3')) {
break;
}
}
let resource = createAudioResource(join(__dirname, 'music/' + audio));
player.play(resource);
console.log('Now playing: ' + audio);
if (txtFile === true) {
fileData = "Now Playing: " + audio;
fs.writeFile("now-playing.txt", fileData, (err) => {
if (err)
console.log(err);
});
}
const statusEmbed = new MessageEmbed()
.addField('Now Playing', `${audio}`)
.setColor('#0066ff')
let statusChannel = bot.channels.cache.get(config.statusChannel);
if (!statusChannel) return console.error('The status channel does not exist! Skipping.');
statusChannel.send({embeds: [statusEmbed]});
}
// Slash Command Handler
bot.commands = new Collection();
@ -105,9 +45,11 @@ bot.once('ready', () => {
console.log(`Logged in as ${bot.user.tag}!`);
console.log(`Running on Discord.JS ${version}`)
console.log(`Prefix: ${config.prefix}`);
console.log(`Owner ID: ${config.botOwner}`);
console.log(`Voice Channel: ${config.voiceChannel}`);
console.log(`Status Channel: ${config.statusChannel}\n`);
console.log(`Status Channel: ${config.statusChannel}`);
console.log(`Owner ID: ${config.botOwner}`);
console.log(`Guild ID: ${config.guildID}`);
console.log(`Client ID: ${config.clientID}\n`);
// Set bots' presence
bot.user.setPresence({
@ -131,7 +73,9 @@ bot.once('ready', () => {
if (!statusChannel) return console.error('The status channel does not exist! Skipping.');
statusChannel.send({ embeds: [readyEmbed]});
voiceInit();
//voiceInit();
AudioBackend(bot, config);
});
@ -194,23 +138,23 @@ bot.on('messageCreate', async msg => {
if (command === 'join') {
msg.reply('Joining voice channel.');
voiceInit();
//voiceInit();
}
if (command === 'resume') {
msg.reply('Resuming music.');
player.unpause();
//player.unpause();
}
if (command === 'pause') {
msg.reply('Pausing music.');
player.pause();
//player.pause();
}
if (command === 'skip') {
msg.reply('Skipping `' + audio + '`...');
player.pause()
playAudio();
//player.pause()
//playAudio();
}
if (command === 'leave') {
@ -224,7 +168,7 @@ bot.on('messageCreate', async msg => {
});
}
audio = "Not Playing";
player.stop();
//player.stop();
const connection = getVoiceConnection(msg.guild.id);
connection.destroy();

View file

@ -1,7 +1,5 @@
const { SlashCommandBuilder } = require('@discordjs/builders');
const { MessageEmbed } = require("discord.js");
const { AudioResource } = require("@discordjs/voice");
const config = require("../config.json");
module.exports = {
data: new SlashCommandBuilder()

View file

@ -7,6 +7,7 @@
"dev": "nodemon bot.js"
},
"dependencies": {
"@discordjs/builders": "^0.12.0",
"@discordjs/opus": "^0.3.2",
"@discordjs/rest": "^0.3.0",
"@discordjs/voice": "^0.5.5",