From fd89e36490da711ba611c1f7c8dda93a3de054f0 Mon Sep 17 00:00:00 2001 From: Andrew Lee Date: Tue, 29 Mar 2022 20:41:38 -0400 Subject: New command; Every Discord interation is async --- AudioBackend.js | 33 +++++++++++++++++++++------------ bot.js | 12 ++++++------ commands/about.js | 2 +- commands/control.js | 16 ++++++++-------- commands/help.js | 4 ++-- commands/join.js | 6 +++--- commands/list.js | 2 +- commands/ping.js | 2 +- commands/play.js | 43 +++++++++++++++++++++++++++++++++++++++++++ commands/stop.js | 4 ++-- 10 files changed, 88 insertions(+), 36 deletions(-) create mode 100644 commands/play.js diff --git a/AudioBackend.js b/AudioBackend.js index 6507716..5960f3e 100644 --- a/AudioBackend.js +++ b/AudioBackend.js @@ -32,10 +32,11 @@ import fs from 'fs' export const player = createAudioPlayer(); export let audio; +export let files = fs.readdirSync('music'); let fileData; -export function voiceInit(bot) { - bot.channels.fetch(config.voiceChannel).then(channel => { +export async function voiceInit(bot) { + bot.channels.fetch(config.voiceChannel).then(async channel => { const connection = joinVoiceChannel({ channelId: channel.id, guildId: channel.guild.id, @@ -52,17 +53,15 @@ export function voiceInit(bot) { player.on('idle', () => { console.log("Music has finished playing, shuffling the beats...") - playAudio(bot); + searchAudio(bot); }) - playAudio(bot); - connection.subscribe(player); + await searchAudio(bot); + return connection.subscribe(player); }).catch(e => { console.error("The voice channel does not exist!\\n(Have you looked at your configuration?)") }) } -export function playAudio(bot) { - let files = fs.readdirSync('music'); - +export async function searchAudio(bot){ //TODO: Eventually this system will need a rework so it won't repeat the same files. while (true) { @@ -73,9 +72,19 @@ export function playAudio(bot) { } } + return await playAudio(bot); +} + +export async function inputAudio(bot, integer) { + audio = files[integer] + + return await playAudio(bot); +} + +export async function playAudio(bot) { let resource = createAudioResource('music/' + audio); - player.play(resource); + await player.play(resource); console.log('Now playing: ' + audio); @@ -93,7 +102,7 @@ export function playAudio(bot) { let statusChannel = bot.channels.cache.get(config.statusChannel); if (!statusChannel) return console.error('The status channel does not exist! Skipping.'); - statusChannel.send({embeds: [statusEmbed]}); + return await statusChannel.send({embeds: [statusEmbed]}); } @@ -110,7 +119,7 @@ export function destroyAudio(interaction) { audio = "Not Playing"; player.stop(); const connection = getVoiceConnection(interaction.guild.id); - connection.destroy(); + return connection.destroy(); } export async function stopBot(bot, interaction) { @@ -125,5 +134,5 @@ export async function stopBot(bot, interaction) { console.log('Powering off...'); destroyAudio(interaction); bot.destroy(); - process.exit(0); + return process.exit(0); } \ No newline at end of file diff --git a/bot.js b/bot.js index f0a9c63..6978ea7 100644 --- a/bot.js +++ b/bot.js @@ -45,7 +45,7 @@ for (const file of commandFiles) { bot.commands.set(command.data.name, command); } -bot.once('ready', () => { +bot.once('ready', async () => { console.log('Bot is ready!'); console.log(`Logged in as ${bot.user.tag}!`); console.log(`Running on Discord.JS ${version}`) @@ -67,15 +67,15 @@ bot.once('ready', () => { // Send bots' status to channel const readyEmbed = new MessageEmbed() - .setAuthor({ name: bot.user.username, iconURL: bot.user.avatarURL()} ) - .setDescription('Starting bot...') - .setColor('#0066ff') + .setAuthor({name: bot.user.username, iconURL: bot.user.avatarURL()}) + .setDescription('Starting bot...') + .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: [readyEmbed]}); + await statusChannel.send({embeds: [readyEmbed]}); - voiceInit(bot); + await voiceInit(bot); }); diff --git a/commands/about.js b/commands/about.js index 8177d81..55ef011 100644 --- a/commands/about.js +++ b/commands/about.js @@ -46,6 +46,6 @@ export default { .setURL('https://github.com/Alee14/DLMP3'), ); - return interaction.reply({ embeds:[aboutEmbed], components:[srcOrig] }); + return await interaction.reply({ embeds:[aboutEmbed], components:[srcOrig] }); }, }; \ No newline at end of file diff --git a/commands/control.js b/commands/control.js index 7f5e9cb..f221898 100644 --- a/commands/control.js +++ b/commands/control.js @@ -21,7 +21,7 @@ import { SlashCommandBuilder } from '@discordjs/builders' import { MessageEmbed, MessageActionRow, MessageButton } from 'discord.js' -import { audio, player, playAudio, destroyAudio, voiceInit, stopBot } from '../AudioBackend.js' +import {audio, player, destroyAudio, voiceInit, stopBot, searchAudio} from '../AudioBackend.js' import config from '../config.json' assert {type: 'json'} export let controlEmbed @@ -60,7 +60,7 @@ export default { new MessageButton() .setStyle('SECONDARY') .setLabel('>>') - .setCustomId('more'), + .setCustomId('next'), ); const controlButtons2 = new MessageActionRow() @@ -68,7 +68,7 @@ export default { new MessageButton() .setStyle('SECONDARY') .setLabel('<<') - .setCustomId('less'), + .setCustomId('back'), new MessageButton() .setStyle('DANGER') .setLabel('Leave') @@ -86,7 +86,7 @@ export default { collector.on('collect', async ctlButton => { if (ctlButton.customId === 'join') { await ctlButton.reply({content:'Joining voice channel', ephemeral:true}) - voiceInit(bot); + await voiceInit(bot); } if (ctlButton.customId === 'play') { await ctlButton.reply({content:'Resuming music', ephemeral:true}) @@ -99,12 +99,12 @@ export default { if (ctlButton.customId === 'skip') { await ctlButton.reply({content:`Skipping \`${audio}\`...`, ephemeral:true}) player.pause(); - playAudio(bot); + await searchAudio(bot); } - if (ctlButton.customId === 'more') { + if (ctlButton.customId === 'next') { await interaction.editReply({ components: [controlButtons2] }); } - if (ctlButton.customId === 'less') { + if (ctlButton.customId === 'back') { await interaction.editReply({ components: [controlButtons] }); } if (ctlButton.customId === 'leave') { @@ -120,6 +120,6 @@ export default { collector.on('end', collected => console.log(`Collected ${collected.size} items`)); - return interaction.reply({embeds:[controlEmbed], components:[controlButtons]}); + return await interaction.reply({embeds:[controlEmbed], components:[controlButtons]}); }, }; \ No newline at end of file diff --git a/commands/help.js b/commands/help.js index b1ba51f..e23b105 100644 --- a/commands/help.js +++ b/commands/help.js @@ -20,7 +20,7 @@ ***************************************************************************/ import { SlashCommandBuilder } from '@discordjs/builders' -import { MessageEmbed } from "discord.js"; +import { MessageEmbed } from 'discord.js'; import { audio } from '../AudioBackend.js' export default { @@ -36,6 +36,6 @@ export default { .setFooter({text:'© Copyright 2020-2022 Andrew Lee. Licensed with GPL-3.0.'}) .setColor('#0066ff') - return interaction.reply({ embeds: [helpEmbed]}); + return await interaction.reply({ embeds: [helpEmbed]}); }, }; \ No newline at end of file diff --git a/commands/join.js b/commands/join.js index 8778aae..72aae2e 100644 --- a/commands/join.js +++ b/commands/join.js @@ -20,7 +20,7 @@ ***************************************************************************/ import { SlashCommandBuilder } from '@discordjs/builders' -import { voiceInit } from "../AudioBackend.js"; +import { voiceInit } from '../AudioBackend.js'; import config from '../config.json' assert {type: 'json'} export default { @@ -29,7 +29,7 @@ export default { .setDescription('Joins voice chat.'), async execute(interaction, bot) { if (![config.botOwner].includes(interaction.user.id)) return await interaction.reply({ content: "You do not have permissions to execute this command.", ephemeral: true }); - await interaction.reply('Joining voice channel') - voiceInit(bot); + await interaction.reply({ content: 'Joining voice channel', ephemeral: true }) + return await voiceInit(bot); }, }; \ No newline at end of file diff --git a/commands/list.js b/commands/list.js index 4ec7156..673b779 100644 --- a/commands/list.js +++ b/commands/list.js @@ -29,7 +29,7 @@ export default { .setName('list') .setDescription('Lists the available audio tracks'), async execute(interaction) { - interaction.reply('Listing the available audio tracks...'); + await interaction.reply('Listing the available audio tracks...'); fs.readdirSync(musicFolder).forEach(file => { console.log(file); }); diff --git a/commands/ping.js b/commands/ping.js index cbf5370..14071ac 100644 --- a/commands/ping.js +++ b/commands/ping.js @@ -26,6 +26,6 @@ export default { .setName('ping') .setDescription('Pong!'), async execute(interaction) { - return interaction.reply('Pong!'); + return await interaction.reply('Pong!'); }, }; \ No newline at end of file diff --git a/commands/play.js b/commands/play.js new file mode 100644 index 0000000..df5be96 --- /dev/null +++ b/commands/play.js @@ -0,0 +1,43 @@ +/************************************************************************** + * + * 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 . + * + ***************************************************************************/ + +import { SlashCommandBuilder } from '@discordjs/builders' +import { inputAudio, audio } from '../AudioBackend.js' +import config from '../config.json' assert {type: 'json'} + +export let integer; + +export default { + data: new SlashCommandBuilder() + .setName('play') + .setDescription('Plays the audio by number') + .addIntegerOption(option => + option.setName('int') + .setDescription('Input a number for the selection for the audio file.') + .setRequired(true), + ), + async execute(interaction, bot) { + if (![config.botOwner].includes(interaction.user.id)) return await interaction.reply({ content: "You do not have permissions to execute this command.", ephemeral: true }); + integer = interaction.options.getInteger('int'); + await inputAudio(bot, integer); + return await interaction.reply({ content: `Now playing: ${audio}`, ephemeral: true}); + }, +}; \ No newline at end of file diff --git a/commands/stop.js b/commands/stop.js index f1bd8eb..c753a1b 100644 --- a/commands/stop.js +++ b/commands/stop.js @@ -29,7 +29,7 @@ export default { .setDescription('Powers off the bot'), async execute(interaction, bot) { if (![config.botOwner].includes(interaction.user.id)) return await interaction.reply({ content: "You do not have permissions to execute this command.", ephemeral: true }); - await interaction.reply('Powering off...') - await stopBot(bot, interaction); + await interaction.reply({ content: 'Powering off...', ephemeral: true}) + return await stopBot(bot, interaction); }, }; \ No newline at end of file -- cgit v1.2.3