Robert Halal
Robert Halal

Reputation: 29

Discord music bot doesn't leave the server when the song ends

I am working on a discord bot now named Amadeus. Amadeus plays music when he receives a given command... well... he will. Right now while I'm still working on him he only plays one song. While he connects to the voice channel and plays his song perfectly fine - he does not leave the voice channel once his song is concluded. For some reason, I guess it never detects that the song ends? Any help appreciated

const connection = message.member.voice.channel.name;
const channel = message.member.voice.channel;
message.channel.send("Now playing Scythe OST in the " + connection + " channel.");
channel.join().then(connection => {
    const dispatcher = connection.play('./Scythe Digital Edition - Soundtrack/01 Europa 1920.mp3');
    dispatcher.on("end", end => {
      channel.leave()
    });
  })
  .catch(console.error);

Upvotes: 1

Views: 1101

Answers (1)

Jakye
Jakye

Reputation: 6625

As @Tenclea suggested, if you take a look at the Discord.js Documentation > Topics you can see that dispatcher.on("finish", () => {}) is used.

const channel = message.member.voice.channel;
message.channel.send(`Now playing Scythe OST in the ${channel.name} channel.`);
channel.join().then(connection => {
    const dispatcher = connection.play("./Scythe Digital Edition - Soundtrack/01 Europa 1920.mp3");
    dispatcher.on("finish", () => channel.leave());
}).catch(e => console.log(e))

dispatcher.on("end", () => {}) will work on Discord JS version 11.

Upvotes: 1

Related Questions