How to Play Audio File Into Channel?

后端 未结 5 958
长发绾君心
长发绾君心 2020-12-23 23:17

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, an

5条回答
  •  攒了一身酷
    2020-12-23 23:52

    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.json

    {
      "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.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)
    

提交回复
热议问题