diff --git a/PvPCraft.js b/PvPCraft.js index f843de4..bdd75c7 100644 --- a/PvPCraft.js +++ b/PvPCraft.js @@ -15,7 +15,7 @@ const git = require("git-rev"); const ravenClient = require("raven"); const PvPClient = require("pvpclient"); const ConfigsDB = require("./lib/ConfigDB.js"); -const Eris = require("eris"); +const Eris = require("../eris"); for (let thing in Eris) { if (Eris.hasOwnProperty(thing) && typeof Eris[thing] === "function") { Eris[thing].prototype.toJSON = function toJSON() { @@ -337,6 +337,7 @@ class PvPCraft { firstShardID: this.shardId, lastShardID: this.shardId, maxShards: this.shardCount, + workerPort: 80, defaultImageFormat: "png", }); } diff --git a/lib/Player.js b/lib/Player.js index 3fda45d..ea56b6f 100644 --- a/lib/Player.js +++ b/lib/Player.js @@ -46,7 +46,8 @@ class Player extends EventEmitter { this.client = e.client; this.config = e.config; this.guild = e.textChannel.guild; - this.initialVoice = e.voiceChannel; + this.guildID = this.guild.id; + this.voice = e.voiceChannel; this.text = e.textChannel; this.raven = e.raven; this.key = e.apiKey; @@ -92,7 +93,7 @@ class Player extends EventEmitter { this.connection.removeListener("warn", this._maybeLog); this.connection.removeListener("debug", this._maybeLog); if (leaveChannel === true) { - this.client.leaveVoiceChannel(this.connection.channelID); + this.client.leaveVoiceChannel(this.channelID); } } } @@ -104,7 +105,7 @@ class Player extends EventEmitter { setVolume(volume) { this.volume = volume / 100; this.config.set("volume", this.volume, {server: this.guild.id}); - this.connection.setVolume(this.volume); + this.client.setVolume(this.guildID, this.volume); } /** @@ -115,14 +116,6 @@ class Player extends EventEmitter { return 100 * this.volume; } - get voice() { - if (this.connection) { - return this.guild.channels.get(this.connection.channelID); - } else { - return this.initialVoice; - } - } - /** * Checks to see if the bot has permission to join and speak in the voice channel * @param {GuildChannel} voice voice channel to check permissions of @@ -139,18 +132,13 @@ class Player extends EventEmitter { * @returns {Promise} */ init(voice) { - this.initialVoice = voice; + this.voice = voice; return new Promise((resolve, reject) => { this._maybeLog(`Trying to init into ${voice.name}`); if (!this.canInit(voice)) return reject("Insufficient permissions to join / speak in voice channel."); this.client.joinVoiceChannel(voice.id).then((connection) => { this._maybeLog(`Joined ${voice.name}`); this.ready = true; - this.connection = connection; - connection.on("end", this.playNextEnd); - connection.on("error", this.playNextError); - connection.on("warn", this._maybeLog); - connection.on("debug", this._maybeLog); resolve(connection); }).catch(error => { this._maybeLog(`Failed to join ${voice.name}`); @@ -372,10 +360,6 @@ class Player extends EventEmitter { async playNext(from, possibleError) { this._maybeLog("PlayNext called from", from, possibleError, "time played", utils.secondsToTime((this.paused ? this.pauseTime - this.startTime : Date.now() - this.startTime) / 1000)); - if (this.connection.playing) { - this.slowSender.sendMessage(this.text, "Skipping the current song as next one is being played."); - return this.connection.stopPlaying("playNext", "Already encoding"); - } if (from === "error") { this.slowSender.sendMessage(this.text, `Finished Playing ${videoUtils.prettyPrint(this.currentVideoInfo)} because of ${possibleError}`); if (this.raven) { @@ -404,7 +388,7 @@ class Player extends EventEmitter { this.playNext("playNext0", error); return; } - if (this.connection) { + let source; let fetchError; try { @@ -434,7 +418,7 @@ class Player extends EventEmitter { this.startTime = Date.now(); try { this.currentOptions = options; - if (this.connection.playing) { + if (this.playing) { this.slowSender.sendMessage(this.text, "Skipping the current song as next one is being played"); return this.connection.stopPlaying("nextTick", "Already encoding"); } @@ -444,19 +428,19 @@ class Player extends EventEmitter { } else { url = source.url; } - this.connection.play(url, options); + console.log("Calling Internal Play"); + this.client.play(this.guildID, url, options); } catch (error) { if (this.raven) this.raven.captureException(error, { extra: { source, options, - voiceChannel: this.voice, - voiceConnection: this.connection + voiceChannel: this.voice } }); } this.slowSender.sendMessage(this.text, `Playing ${videoUtils.prettyPrint(this.currentVideoInfo)} Container:${source.container} Encoding:${source.encoding}`); - } + } diff --git a/modules/music.js b/modules/music.js index 6a0f010..8314266 100644 --- a/modules/music.js +++ b/modules/music.js @@ -46,6 +46,8 @@ class music { this.musicDB = new MusicDB(this.r, {key}); this.leaveChecker = false; this.boundChannels = []; + this.voiceEnd = this.endListener.bind(this, "playNextEnd"); + this.voiceError = this.endListener.bind(this, "playNextError") } static getCommands() { @@ -53,6 +55,7 @@ class music { } onReady() { + this.attachListeners(); this._slowSender.onReady(); if (!this.leaveChecker) { this.leaveChecker = setInterval(this.leaveUnused.bind(this), 60000); @@ -87,6 +90,7 @@ class music { throw error; }).then((player) => { if (player.currentVideo == null) { + console.log("play Next"); player.playNext(); } }); @@ -95,6 +99,24 @@ class music { }) } + attachListeners() { + this.client.on("voice_end", this.voiceEnd); + this.client.on("voice_error", this.voiceError); + } + + removeListeners() { + this.client.removeListener("voice_end", this.voiceEnd); + this.client.removeListener("voice_error", this.voiceError); + } + + endListener(functionNameToCall, event) { + if (event.guildID) { + if (this.boundChannels.hasOwnProperty(event.guildID)) { + this.boundChannels[functionNameToCall](event); + } + } + } + init(id, msg, command, perms, debug = false) { let returnPromise = new Promise((resolve, reject) => { let voiceChannel = msg.channel.guild.channels.get(msg.member.voiceState.channelID); @@ -136,7 +158,7 @@ class music { let channel = this.boundChannels[id]; if (channel.connection && channel.ready - && channel.connection.playing !== true + && channel.playing !== true && (Date.now() - channel.lastPlay > 600000) && channel.voice.voiceMembers.size < 2) { channel.text.createMessage("Leaving voice channel due to inactivity.") @@ -172,6 +194,7 @@ class music { } delete this.boundChannels[i]; } + this.removeListeners(); } /** @@ -318,7 +341,7 @@ class music { if (command.command === "pause" && perms.check(msg, "music.pause")) { if (this.boundChannels.hasOwnProperty(id) && this.boundChannels[id].hasOwnProperty("connection")) { - if (this.boundChannels[id].connection.playing && !this.boundChannels[id].connection.paused) { + if (this.boundChannels[id].playing && !this.boundChannels[id].paused) { this.boundChannels[id].pause(); command.replyAutoDeny(`Paused Playback use ${command.prefix}resume to resume it.`) } else { @@ -333,7 +356,7 @@ class music { if (command.command === "resume" && perms.check(msg, "music.resume")) { if (this.boundChannels.hasOwnProperty(id) && this.boundChannels[id].hasOwnProperty("connection")) { - if (this.boundChannels[id].connection.paused) { + if (this.boundChannels[id].paused) { this.boundChannels[id].resume(msg); command.replyAutoDeny("Playback resumed.") } else { @@ -520,7 +543,7 @@ class music { possiblySendUserNotInVoice(msg, command) { if (msg.member.voiceState.channelID) { let player = this.boundChannels[msg.channel.guild.id]; - if (!player || !player.connection || msg.member.voiceState.channelID === player.connection.channelID) { + if (!player || !player.connection || msg.member.voiceState.channelID === player.channelID) { return false; } else { command.createMessageAutoDeny("Sorry but you must be in the same voice channel as the bot to use this command."); diff --git a/package.json b/package.json index 2cf0e6c..984d024 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "colors": "^1.1.2", "deepmerge": "latest", "ebml": "^2.2.0", - "eris": "github:macdja38/eris#voice-error", + "eris": "github:macdja38/eris#external-voice", "erlpack": "github:hammerandchisel/erlpack", "eventemitter3": "^2.0.2", "git-rev": "^0.2.1",