Reputation: 1022
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.
Upvotes: 21
Views: 119363
Reputation: 367
This is an semi old thread but I'm going to add code here that will hopefully help someone out and save them time. It took me way too long to figure this out but dispatcher.on('end')
didn't work for me. I think in later versions of discord.js they changed it from end
to finish
var voiceChannel = msg.member.voice.channel;
voiceChannel.join()
.then(connection => {
const dispatcher = connection.play(fileName);
dispatcher.on("finish", end => {
voiceChannel.leave();
deleteFile(fileName);
});
})
.catch(console.error);
Note that fileName is a string path for example: fileName = "/example.mp3". Hopefully that helps someone out there :)
Upvotes: 3
Reputation: 128
Update: If you want to detect if the Audio has stopped, you must subscribe to the speaking
event.
voiceChannel
.join()
.then((connection) => {
const dispatcher = connection.play("./audio_files/file.mp3");
dispatcher.on("speaking", (speaking) => {
if (!speaking) {
voiceChannel.leave();
}
});
})
Upvotes: 0
Reputation: 1022
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.
Upvotes: 36
Reputation: 48630
I went ahead an included Nicholas Johnson's Github bot code here, but I made slight modifications.
LockableClient
that extends the Discord Client
.{
"token" : "your-token-here"
}
const { Client } = require('discord.js')
/**
* A lockable client that can interact with the Discord API.
* @extends {Client}
*/
class LockableClient extends Client {
constructor(options) {
super(options)
this.locked = false
}
lock() {
this.setLocked(true)
}
unlock() {
this.setLocked(false)
}
setLocked(locked) {
return this.locked = locked
}
isLocked {
return this.locked
}
}
module.exports = LockableClient;
const auth = require('./auth.json')
const { LockableClient } = require('./lockable-client.js')
const bot = new LockableClient()
bot.on('message', message => {
if (!bot.isLocked() && message.content === 'Gotcha Bitch') {
bot.lock()
var voiceChannel = message.member.voiceChannel
voiceChannel.join().then(connection => {
const dispatcher = connection.playFile('./assets/audio/gab.mp3')
dispatcher.on('end', end => voiceChannel.leave());
}).catch(err => console.log(err))
bot.unlock()
}
})
bot.login(auth.token)
Upvotes: 3
Reputation: 89
thanks so much!
One thing I will say to help anyone else, is things like where it says ENTER CODE HERE on step 10, you put the code from step 11 IE:
dispatcher.on("end", end => voiceChannel.leave());
As a complete example, this is how I have used it in my message command IF block:
if (command === "COMMAND") {
var VC = message.member.voiceChannel;
if (!VC)
return message.reply("MESSAGE IF NOT IN A VOICE CHANNEL")
VC.join()
.then(connection => {
const dispatcher = connection.playFile('c:/PAtH/TO/MP3/FILE.MP3');
dispatcher.on("end", end => {VC.leave()});
})
.catch(console.error);
};
Upvotes: 6