diff options
| author | Andrew Lee <alee14498@protonmail.com> | 2022-03-26 21:07:35 -0400 |
|---|---|---|
| committer | Andrew Lee <alee14498@protonmail.com> | 2022-03-26 21:07:35 -0400 |
| commit | ae48ba2014bca4a10adf8cf8b6fb0d9e6d4ff401 (patch) | |
| tree | 38fbe09fa9c7b93d708ed477ea84f7cc0a8ec17a | |
| parent | 99c86c80788990e5b8adafeb3b9ba5f863fba855 (diff) | |
| download | DLAP-ae48ba2014bca4a10adf8cf8b6fb0d9e6d4ff401.tar.gz DLAP-ae48ba2014bca4a10adf8cf8b6fb0d9e6d4ff401.tar.bz2 DLAP-ae48ba2014bca4a10adf8cf8b6fb0d9e6d4ff401.zip | |
Audio stuff is now on its own file
| -rw-r--r-- | AudioBackend.js | 92 | ||||
| -rw-r--r-- | bot.js | 86 | ||||
| -rw-r--r-- | commands/help.js | 2 | ||||
| -rw-r--r-- | package.json | 1 |
4 files changed, 108 insertions, 73 deletions
diff --git a/AudioBackend.js b/AudioBackend.js new file mode 100644 index 0000000..4ca9af3 --- /dev/null +++ b/AudioBackend.js @@ -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(); +}
\ No newline at end of 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(); diff --git a/commands/help.js b/commands/help.js index 67ed896..c8742cb 100644 --- a/commands/help.js +++ b/commands/help.js @@ -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() diff --git a/package.json b/package.json index b15340d..7d78323 100644 --- a/package.json +++ b/package.json @@ -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", |
