twlite / discord-ytdl-core

Simple ytdl wrapper for discord bots with custom ffmpeg args support.
https://www.npmjs.com/package/discord-ytdl-core
Apache License 2.0
52 stars 10 forks source link

Can't use on docker #24

Closed LoneExile closed 3 years ago

LoneExile commented 3 years ago

I can use it play music on my local

node test.js

but can't use in docker, bot join and leave voice channel

docker-compose -f docker-compose.test.yml

test.js

const ytdl = require('discord-ytdl-core')
const Discord = require('discord.js')
const client = new Discord.Client()
const BOT_TOKEN = require('./config/TOKEN')

client.on('ready', () => {
  console.log('ready')
})

client.on('message', (msg) => {
  if (msg.author.bot || !msg.guild) return
  if (msg.content === '#!play') {
    if (!msg.member.voice.channel)
      return msg.channel.send("You're not in a voice channel?")
    let stream = ytdl('https://www.youtube.com/watch?v=QnL5P0tFkwM', {
      filter: 'audioonly',
      opusEncoded: false,
      fmt: 'mp3',
      encoderArgs: ['-af', 'bass=g=10,dynaudnorm=f=200'],
    })
    //console.log(stream)

    msg.member.voice.channel
      .join()
      .then((connection) => {
        let dispatcher = connection
          .play(stream, {
            type: 'unknown',
          })
          .on('finish', () => {
            msg.guild.me.voice.channel.leave()
          })
      })
      .catch((e) => {
        console.log(e)
      })
  }
})
client.login(BOT_TOKEN)

Dockerfile

FROM node
WORKDIR /bot_app
COPY package.json . 
RUN apt-get update
RUN apt-get install ffmpeg -y
ARG NODE_ENV
RUN if [ "$NODE_ENV" = "development" ]; \
    then npm install; \
    else npm install --only=production; \
    fi
COPY . ./
ENV PORT 3000
EXPOSE $PORT

docker-compose.test.yml

version: '3'
services:
  node-app:
    build:
      context: .
      args:
        NODE_ENV: development
    volumes:
      - ./:/bot_app:ro
      - /app/node_modules
    environment:
      - NODE_ENV=development
    # package.js->:"test": "nodemon test.js"
    command: npm run test

dependencies

"dependencies": {
    "discord-ytdl-core": "^5.0.3",
    "discord.js": "^12.2.0",
    "express": "^4.17.1",
    "mongoose": "^5.12.12",
    "node-fetch": "^2.6.1",
    "ytdl-core": "^4.8.2"
  },
  "devDependencies": {
    "nodemon": "^2.0.7"
  }