Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Discord Bot that Plays an Audio File When Someone Joins

I would like the bot to join a voice channel when someone joins and then play a sound file.

So far, I've gotten the bot to join the voice channel but it just doesn't play the sound and no errors show so I don't really know what is going wrong.

const Discord = require('discord.js');
const bot = new Discord.Client();
bot.login('BOT TOKEN');

bot.on('voiceStateUpdate', (oldMember, newMember) => {
  let newUserChannel = newMember.voiceChannel
  let oldUserChannel = oldMember.voiceChannel
  let textChannel = oldMember.guild.channels.get('TEXTCHANNEL ID')

  if(oldUserChannel === undefined && newUserChannel !== undefined) {

    if (newMember.id === 'MEMEBER ID')         //Member 1
    {   
       newUserChannel.join()
       .then(connection => {
          console.log("Joined voice channel!");
          const dispatcher = connection.playFile("C:\Users\NAME\Documents\Welcome_Bot\music\bossman.mp3");

          dispatcher.on("end", end => {newUserChannel.leave()});
       })
        .catch(console.error);

    }
      else if (newMember.id === 'MEMEBER ID')       //Member 2
      {
       textChannel.send('Hello Member 2')
      }
        else if (newMember.id === 'MEMEBER ID')      //Member 3
        {
          textChannel.send('Hello Member 3')
        }
           else                                      //Random
           {
             textChannel.send("Hello") 
           } 
  } 
});
like image 968
Sean Carson Avatar asked Apr 16 '19 18:04

Sean Carson


People also ask

Is there a Discord bot that plays audio files?

Groovy is one of the most popular Discord music bots. There are a variety of Groovy bot commands that you can use. With the Groovy music bot, you can play songs through website links or file uploads or search for specific songs. You can also create a song queue.


1 Answers

I believe your issue is that the bot isn't actually joining the channel before you call the dispatcher. The bot needs to be told to enter the channel before you call the dispatcher. Havn't worked on audio in a while but I believe that connection.join() right above your dispatcher would work. You may need to .then() to ensure it dosn't initilize the dispatcher before the bot joins the channel.

like image 69
Ethan Mitchell Avatar answered Nov 14 '22 23:11

Ethan Mitchell