diff options
| -rw-r--r-- | AudioBackend.js | 136 | ||||
| -rw-r--r-- | README.md | 4 | ||||
| -rw-r--r-- | bot.js | 39 | ||||
| -rw-r--r-- | commands/about.js | 29 | ||||
| -rw-r--r-- | commands/control.js | 27 | ||||
| -rw-r--r-- | commands/help.js | 30 | ||||
| -rw-r--r-- | commands/ping.js | 25 | ||||
| -rw-r--r-- | package.json | 1 |
8 files changed, 192 insertions, 99 deletions
diff --git a/AudioBackend.js b/AudioBackend.js index 4ca9af3..cbf0779 100644 --- a/AudioBackend.js +++ b/AudioBackend.js @@ -1,6 +1,6 @@ /************************************************************************** * - * DLMP3 Bot: A Discord bot that plays local mp3 audio tracks. + * DLMP3 Bot: A Discord bot that plays local MP3 audio tracks. * (C) Copyright 2022 * Programmed by Andrew Lee * @@ -19,74 +19,74 @@ * ***************************************************************************/ -const { createAudioPlayer, createAudioResource, joinVoiceChannel, VoiceConnectionStatus } = require('@discordjs/voice'); +import { createAudioPlayer, createAudioResource, joinVoiceChannel, VoiceConnectionStatus } from '@discordjs/voice' +import { MessageEmbed } from 'discord.js' +import config from './config.json' assert {type: 'json'} +import fs from 'fs' + const player = createAudioPlayer(); -const fs = require('fs'); -const { join } = require('node:path'); -let audio; +export 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(); + + +export function voiceInit(bot) { + 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(bot); + connection.subscribe(player); + }) +} + +export function playAudio(bot) { + let files = fs.readdirSync('music'); + + // Eventually this system will need a rework so it won't repeat the same files. + + while (true) { + audio = files[Math.floor(Math.random() * files.length)]; + console.log('Searching .mp3 file...'); + if (audio.endsWith('.mp3')) { + break; + } + } + + let resource = createAudioResource('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]}); + }
\ No newline at end of file @@ -1,11 +1,9 @@ # DLMP3 Bot (Discord.JS Local MP3) -A Discord bot that plays local mp3 audio tracks. Written in Discord.JS. +A Discord bot that plays local MP3 audio tracks. Written in Discord.JS. [Video Tutorial](https://www.youtube.com/watch?v=7X3FAhYW31I) -(Originally for Alee's birthday) - If there's anything wrong or wanting to add a feature, feel free to make a fork and put a pull request. # Configuration @@ -1,6 +1,6 @@ /************************************************************************** * - * DLMP3 Bot: A Discord bot that plays local mp3 audio tracks. + * DLMP3 Bot: A Discord bot that plays local MP3 audio tracks. * (C) Copyright 2022 * Programmed by Andrew Lee * @@ -18,25 +18,31 @@ * along with this program. If not, see <https://www.gnu.org/licenses/>. * ***************************************************************************/ -const { Client, MessageEmbed, Collection, version } = require('discord.js'); -const fs = require('fs'); -const { getVoiceConnection } = require('@discordjs/voice'); -const bot = new Client({intents: ['GUILDS', 'GUILD_MESSAGES', 'GUILD_VOICE_STATES']}); -const config = require('./config.json'); -const AudioBackend = require('./AudioBackend'); -let audio; +import { Client, MessageEmbed, Collection, version } from "discord.js" +import { voiceInit } from "./AudioBackend.js" +import fs from "fs" +import config from './config.json' assert {type: 'json'} + +export const bot = new Client({intents: ['GUILDS', 'GUILD_MESSAGES', 'GUILD_VOICE_STATES']}); let fileData; let txtFile = true; bot.login(config.token); +/** + * Project Ideas: + * Play directly the MP3 file + * New queue system + * List MP3 files + */ + // Slash Command Handler bot.commands = new Collection(); const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js')); for (const file of commandFiles) { - const command = require(`./commands/${file}`); + const { default: command } = await import(`./commands/${file}`); bot.commands.set(command.data.name, command); } @@ -73,9 +79,7 @@ bot.once('ready', () => { if (!statusChannel) return console.error('The status channel does not exist! Skipping.'); statusChannel.send({ embeds: [readyEmbed]}); - //voiceInit(); - - AudioBackend(bot, config); + voiceInit(bot); }); @@ -87,13 +91,18 @@ bot.on('interactionCreate', async interaction => { if (!command) return; try { - await command.execute(interaction, bot, player, audio); + await command.execute(interaction, bot); } catch (error) { console.error(error); - await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true }); + if (error == 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!\nShare this to the bot owner!\n\nDetails:\`\`\`${error}\`\`\``, ephemeral: true }); + } } }); +/* bot.on('messageCreate', async msg => { if (msg.author.bot) return; if (!msg.guild) return; @@ -198,4 +207,4 @@ bot.on('messageCreate', async msg => { process.exit(0); } -}); +});*/ diff --git a/commands/about.js b/commands/about.js index 033c44f..b7771db 100644 --- a/commands/about.js +++ b/commands/about.js @@ -1,14 +1,35 @@ -const { SlashCommandBuilder } = require('@discordjs/builders'); -const { MessageEmbed, version, MessageActionRow, MessageButton } = require("discord.js"); +/************************************************************************** + * + * 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/>. + * + ***************************************************************************/ -module.exports = { +import { SlashCommandBuilder } from '@discordjs/builders' +import { MessageEmbed, version, MessageActionRow, MessageButton } from 'discord.js' + +export default { data: new SlashCommandBuilder() .setName('about') .setDescription('Information about the bot'), async execute(interaction, bot) { const aboutEmbed = new MessageEmbed() .setAuthor({name:`About ${bot.user.username}`, iconURL:bot.user.avatarURL()}) - .addField('Information', 'A Discord bot that plays local mp3 audio tracks.') + .addField('Information', 'A Discord bot that plays local MP3 audio tracks.') .addField('Original Creator', 'Andrew Lee (Alee#4277)') .addField('Frameworks', `Discord.JS ${version} + Voice`) .addField('License', 'GNU General Public License v3.0') diff --git a/commands/control.js b/commands/control.js index f5846e2..42ce177 100644 --- a/commands/control.js +++ b/commands/control.js @@ -1,8 +1,29 @@ -const { SlashCommandBuilder } = require('@discordjs/builders'); -const { MessageEmbed, MessageActionRow, MessageButton } = require("discord.js"); +/************************************************************************** + * + * 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/>. + * + ***************************************************************************/ +import { SlashCommandBuilder } from '@discordjs/builders' +import { MessageEmbed, MessageActionRow, MessageButton } from 'discord.js' -module.exports = { + +export const command = { data: new SlashCommandBuilder() .setName('control') .setDescription('Controlling the music'), diff --git a/commands/help.js b/commands/help.js index c8742cb..f56715c 100644 --- a/commands/help.js +++ b/commands/help.js @@ -1,11 +1,33 @@ -const { SlashCommandBuilder } = require('@discordjs/builders'); -const { MessageEmbed } = require("discord.js"); +/************************************************************************** + * + * 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/>. + * + ***************************************************************************/ -module.exports = { +import { SlashCommandBuilder } from '@discordjs/builders' +import { MessageEmbed } from "discord.js"; +import { audio } from '../AudioBackend.js' + +export default { data: new SlashCommandBuilder() .setName('help') .setDescription('Lists the commands'), - async execute(interaction, bot, audio) { + async execute(interaction, bot) { const helpEmbed = new MessageEmbed() .setAuthor({name:`${bot.user.username} Help`, iconURL:bot.user.avatarURL()}) .setDescription(`Currently playing \`${audio}\`.`) diff --git a/commands/ping.js b/commands/ping.js index c5b4885..274a1e5 100644 --- a/commands/ping.js +++ b/commands/ping.js @@ -1,6 +1,27 @@ -const { SlashCommandBuilder } = require('@discordjs/builders'); +/************************************************************************** + * + * 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/>. + * + ***************************************************************************/ -module.exports = { +import { SlashCommandBuilder } from '@discordjs/builders' + +export const command = { data: new SlashCommandBuilder() .setName('ping') .setDescription('Replies with Pong!'), diff --git a/package.json b/package.json index 7d78323..65e11d1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,7 @@ { "name": "dlmp3", "version": "1.0.0", + "type": "module", "main": "bot.js", "license": "GPL-3.0", "scripts": { |
