Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use external jda voice worker #80

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion PvPCraft.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -337,6 +337,7 @@ class PvPCraft {
firstShardID: this.shardId,
lastShardID: this.shardId,
maxShards: this.shardCount,
workerPort: 80,
defaultImageFormat: "png",
});
}
Expand Down
38 changes: 11 additions & 27 deletions lib/Player.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
}
Expand All @@ -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);
}

/**
Expand All @@ -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
Expand All @@ -139,18 +132,13 @@ class Player extends EventEmitter {
* @returns {Promise<VoiceConnection>}
*/
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}`);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -404,7 +388,7 @@ class Player extends EventEmitter {
this.playNext("playNext0", error);
return;
}
if (this.connection) {

let source;
let fetchError;
try {
Expand Down Expand Up @@ -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");
}
Expand All @@ -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}`);
}

}


Expand Down
31 changes: 27 additions & 4 deletions modules/music.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,16 @@ 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() {
return ["init", "play", "skip", "list", "time", "pause", "resume", "volume", "shuffle", "next", "destroy", "clear", "logchannel", "link"];
}

onReady() {
this.attachListeners();
this._slowSender.onReady();
if (!this.leaveChecker) {
this.leaveChecker = setInterval(this.leaveUnused.bind(this), 60000);
Expand Down Expand Up @@ -87,6 +90,7 @@ class music {
throw error;
}).then((player) => {
if (player.currentVideo == null) {
console.log("play Next");
player.playNext();
}
});
Expand All @@ -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);
Expand Down Expand Up @@ -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.")
Expand Down Expand Up @@ -172,6 +194,7 @@ class music {
}
delete this.boundChannels[i];
}
this.removeListeners();
}

/**
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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.");
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down