admin管理员组

文章数量:1433485

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\Wele_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") 
           } 
  } 
});

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\Wele_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") 
           } 
  } 
});
Share Improve this question edited Apr 16, 2019 at 18:52 acarlstein 1,8382 gold badges15 silver badges21 bronze badges asked Apr 16, 2019 at 18:45 Sean CarsonSean Carson 811 gold badge1 silver badge3 bronze badges 10
  • 1 I am suspecting about the link to your sound audio. Should it be something like "http://....*.mp3" instead? otherwise the web will not understand your localpath – duc mai Commented Apr 16, 2019 at 19:16
  • 1 @ducmai To play a local file, the current setup is fine. The Discord.js docs example uses an absolute path like the code above. – slothiful Commented Apr 16, 2019 at 19:44
  • Is your username actually NAME? That's what's in your path. If not, that would be why nothing is played. – slothiful Commented Apr 16, 2019 at 19:46
  • @slothiful I just put place holders into the question – Sean Carson Commented Apr 16, 2019 at 19:49
  • 1 You could update to d.js v12(master), that has a full voice rewrite but al sso brings breaking changes, npm i discordjs/discord.js to get v12 – PLASMA chicken Commented Apr 20, 2019 at 9:50
 |  Show 5 more ments

1 Answer 1

Reset to default 1

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.

本文标签: javascriptDiscord Bot that Plays an Audio File When Someone JoinsStack Overflow