<commit_before>//
// Copyright (c) 2016-2017 DrSmugleaf
//

"use strict"
const commando = require("discord.js-commando")
const constants = require("../../util/constants")
const request = require("request")
const validurl = require("valid-url")
const winston = require("winston")
const ytdl = require("ytdl-core")

module.exports = class Play extends commando.Command {
  constructor(client) {
    super(client, {
      name: "play",
      aliases: ["play"],
      group: "youtube",
      memberName: "play",
      description: "Play a video's sound from youtube to your voice channel",
      examples: ["play https://www.youtube.com/watch?v=dQw4w9WgXcQ"],
      guildOnly: true,
      args: [
        {
          key: "url",
          prompt: "What video do you want to hear?",
          type: "string",
          validate: url => function() {
            if(validurl.isWebUri(url)) {
              request(url, function(e, res) {
                return !e && res.statusCode == 200
              })
            }
          }
        }
      ]
    })
  }

  // async joinVoice(msg) {
  //   const voiceChannel = msg.member.voiceChannel
  //
  //   if(voiceChannel.connection === msg.client.voiceConnection) {
  //     winston.info(1)
  //     return msg.client.voiceConnection
  //   } else {
  //     voiceChannel.join().then(voiceConnection => {
  //       winston.info(2)
  //       return voiceConnection
  //     }).catch(winston.error)
  //   }
  // }

  async joinVoice(msg) {
    const voiceChannel = msg.member.voiceChannel

    return new Promise(function(resolve, reject) {
      if(voiceChannel.connection == msg.client.voiceConnection) {
        resolve(msg.client.voiceConnection)
      } else {
        voiceChannel.join().then(voiceConnection => {
          resolve(voiceConnection)
        }).catch(reject)
      }
    })
  }

  async run(msg, args) {
    if(!msg.member.voiceChannel) {
      return msg.reply(constants.responses.NOT_A_VOICE_CHANNEL["english"])
    }

    const url = args.url

    // var voiceConnection = this.joinVoice(msg)
    var stream = ytdl(url, {filter: "audioonly"})
    // winston.info(voiceConnection)
    // var dispatcher = voiceConnection.playStream(
    //   stream,
    //   constants.youtube.STREAMOPTIONS
    // )

    this.joinVoice(msg).then((voiceConnection) => {
      voiceConnection.playStream(
        stream,
        constants.youtube.STREAMOPTIONS
      ).on("end", () => {
        voiceConnection.disconnect()
      })
    }).catch(winston.error)

    // dispatcher.on("end", () => {
    //   voiceConnection.disconnect()
    // })
    stream.on("info", (info) => {
      console.log(info.title)
    })
    return msg.reply(`Now playing ${url}`)
  }
}<commit_msg>Fixed youtube !play not working if the bot wasn't already in the channel<commit_after>//
// Copyright (c) 2016-2017 DrSmugleaf
//

"use strict"
const commando = require("discord.js-commando")
const constants = require("../../util/constants")
const request = require("request")
const validurl = require("valid-url")
const winston = require("winston")
const ytdl = require("ytdl-core")

module.exports = class Play extends commando.Command {
  constructor(client) {
    super(client, {
      name: "play",
      aliases: ["play"],
      group: "youtube",
      memberName: "play",
      description: "Play a video's sound from youtube to your voice channel",
      examples: ["play https://www.youtube.com/watch?v=dQw4w9WgXcQ"],
      guildOnly: true,
      args: [
        {
          key: "url",
          prompt: "What video do you want to hear?",
          type: "string",
          validate: url => function() {
            if(validurl.isWebUri(url)) {
              request(url, function(e, res) {
                return !e && res.statusCode == 200
              })
            }
          }
        }
      ]
    })
  }

  async joinVoice(msg) {
    const voiceChannel = msg.member.voiceChannel
    const voiceConnection = msg.client.voiceConnection

    return new Promise(function(resolve, reject) {
      if(voiceConnection && voiceConnection == voiceChannel.connection) {
        resolve(msg.client.voiceConnection)
      } else {
        voiceChannel.join().then(voiceConnection => {
          resolve(voiceConnection)
        }).catch(reject)
      }
    })
  }

  async run(msg, args) {
    if(!msg.member.voiceChannel) {
      return msg.reply(constants.responses.NOT_A_VOICE_CHANNEL["english"])
    }

    const url = args.url
    const stream = ytdl(url, {filter: "audioonly"})

    this.joinVoice(msg).then(voiceConnection => {
      voiceConnection.playStream(
        stream,
        constants.youtube.STREAMOPTIONS
      ).on("end", () => {
        voiceConnection.disconnect()
      })
    }).catch(winston.error)

    stream.on("info", (info) => {
      return msg.reply(`Now playing ${info.title}`)
    })
  }
}
{'fraction_non_alphanumeric': 0.1432688349114862, 'fraction_numerical': 0.005763688760806916, 'mean_word_length': 2.32580424366872, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1336644', 'n_tokens_mistral': 1504, 'n_tokens_neox': 1447, 'n_words': 290}