New command; Every Discord interation is async

This commit is contained in:
Andrew Lee 2022-03-29 20:41:38 -04:00
parent 84b7017c70
commit fd89e36490
Signed by: andrew
GPG key ID: 4DCE67C47836D125
10 changed files with 88 additions and 36 deletions

View file

@ -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);
}

12
bot.js
View file

@ -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);
});

View file

@ -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] });
},
};

View file

@ -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]});
},
};

View file

@ -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]});
},
};

View file

@ -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);
},
};

View file

@ -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);
});

View file

@ -26,6 +26,6 @@ export default {
.setName('ping')
.setDescription('Pong!'),
async execute(interaction) {
return interaction.reply('Pong!');
return await interaction.reply('Pong!');
},
};

43
commands/play.js Normal file
View file

@ -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 <https://www.gnu.org/licenses/>.
*
***************************************************************************/
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});
},
};

View file

@ -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);
},
};