How do you play an audio file from a Discord bot? Needs to play a local file, be in JS, and upon a certain message being sent it will join the user who typed the message, and will play the file to that channel.
GitHub Project: LINK
In order to do this there are a few things you have to make sure of first.
From there the steps are quite simple. After making your project index.js
you will start typing some code. Here are the steps:
var Discord = require('discord.js');
var bot = new Discord.Client();
3. Create a Boolean variable to make sure that the system doesn't overload of requests;
var isReady = true;
bot.on('message', message =>{ENTER CODE HERE});
if (isReady && message.content === 'MESSAGE'){ENTER CODE HERE}
isReady = false;
var voiceChannel = message.member.voice.channel;
voiceChannel.join().then(connection =>{ENTER CODE HERE}).catch(err => console.log(err));
const dispatcher = connection.play('./audiofile.mp3');
dispatcher.on("end", end => {ENTER CODE HERE});
voiceChannel.leave();
bot.login('CLIENT TOKEN HERE');
After you are all finished with this, make sure to check for any un-closed brackets or parentheses. i made this because it took my hours until I finally found a good solution so I just wanted to share it with anybody who is out there looking for something like this.