How to Play Audio File Into Channel?

我只是一个虾纸丫 提交于 2019-12-20 09:46:00

问题


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.


回答1:


GitHub Project: LINK

In order to do this there are a few things you have to make sure of first.

  1. Have FFMPEG installed & the environment path set for it in Windows [link]
  2. Have Microsoft Visual Studio (VS) installed [link]
  3. Have Node.js installed.[link]
  4. Have Discord.js installed in VS. [link]

From there the steps are quite simple. After making your project index.js you will start typing some code. Here are the steps:

  1. Add the Discord.js dependency to the project;

    var Discord = require('discord.js');

  2. Create out client variable called bot;

    var bot = new Discord.Client();

  3. Create a Boolean variable to make sure that the system doesn't overload of requests;

    var isReady = true;

  4. Next make the function to intercept the correct message;

    bot.on('message', message =>{ENTER CODE HERE});

  5. Create an if statement to check if the message is correct & if the bot is ready;

    if (isReady && message.content === 'MESSAGE'){ENTER CODE HERE}

  6. Set the bot to unready so that it cannot process events until it finishes;

    isReady = false;

  7. Create a variable for the channel that the message-sender is currently in;

    var voiceChannel = message.member.voiceChannel;

  8. Join that channel and keep track of all errors;

    voiceChannel.join().then(connection =>{ENTER CODE HERE}).catch(err => console.log(err));

  9. Create a refrence to and play the audio file;

    const dispatcher = connection.playFile('./audiofile.mp3');

  10. Slot to wait until the audio file is done playing;

    dispatcher.on("end", end => {ENTER CODE HERE});

  11. Leave channel after audio is done playing;

    voiceChannel.leave();

  12. Login to the application;

    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.




回答2:


There's no need for the bloat of Visual Studio .. way overkill .. you just need node.js and the dependencies via npm.




回答3:


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);
};



回答4:


I went ahead an included Nicholas Johnson's Github bot code here, but I made slight modifications.

  1. He appears to be creating a lock; so I created a LockableClient that extends the Discord Client.
  2. Never include an authorization token in the code

auth.js

{
  "token" : "your-token-here"
}

lockable-client.js

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;

index.js

const auth = require('./auth.js')
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)


来源:https://stackoverflow.com/questions/41580798/how-to-play-audio-file-into-channel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!