From 0dfc747b1b1d3c5d132233a8e24216acc745955e Mon Sep 17 00:00:00 2001 From: hydrabolt Date: Sat, 7 Nov 2015 22:02:33 +0000 Subject: [PATCH] Basic fixes, added test script --- lib/Client/InternalClient.js | 28 ++++++++-- lib/Voice/VoiceConnection.js | 5 +- src/Client/InternalClient.js | 27 +++++++-- src/Voice/VoiceConnection.js | 5 +- test/bot.1.js | 105 +++++++++++++++-------------------- 5 files changed, 95 insertions(+), 75 deletions(-) diff --git a/lib/Client/InternalClient.js b/lib/Client/InternalClient.js index 027d7cc2e..e2b46fff3 100644 --- a/lib/Client/InternalClient.js +++ b/lib/Client/InternalClient.js @@ -48,7 +48,27 @@ var InternalClient = (function () { this.resolver = new Resolver(this); } - //def joinVoiceChannel() + //def leaveVoiceChannel + + InternalClient.prototype.leaveVoiceChannel = function leaveVoiceChannel(chann) { + var self = this; + return new Promise(function (resolve, reject) { + var channel = self.resolver.resolveVoiceChannel(chann); + + if (channel) { + if (self.voiceConnections[channel]) { + var chan = self.voiceConnections[channel]; + chan.stopPlaying(); + self.voiceConnections.remove(chan); + resolve(); + } + } else { + reject(new Error("voice channel does not exist")); + } + }); + }; + + //def joinVoiceChannel InternalClient.prototype.joinVoiceChannel = function joinVoiceChannel(chann) { var self = this; @@ -57,9 +77,7 @@ var InternalClient = (function () { var channel = self.resolver.resolveVoiceChannel(chann); if (channel) { - if (!self.voiceConnections[channel]) { - - self.voiceConnections[channel] = {}; + if (!self.voiceConnections.get("id", channel.id)) { var session, token, @@ -76,7 +94,7 @@ var InternalClient = (function () { token = data.d.token; endpoint = data.d.endpoint; - var chan = self.voiceConnections[channel] = new VoiceConnection(channel, self.client, session, token, server, endpoint); + var chan = self.voiceConnections.add(new VoiceConnection(channel, self.client, session, token, server, endpoint)); chan.on("ready", resolve); chan.on("error", reject); diff --git a/lib/Voice/VoiceConnection.js b/lib/Voice/VoiceConnection.js index a20ae8bcc..ca74fd834 100644 --- a/lib/Voice/VoiceConnection.js +++ b/lib/Voice/VoiceConnection.js @@ -25,6 +25,7 @@ var VoiceConnection = (function (_EventEmitter) { if (!Opus) { console.log("HEY! WATCH OUT\n\n discord.js needs node-opus, you don't have it installed."); } + this.id = channel.id; this.voiceChannel = channel; this.client = client; this.session = session; @@ -64,13 +65,13 @@ var VoiceConnection = (function (_EventEmitter) { if (self.playingIntent) { self.stopPlaying(); } - + self.playing = true; var retStream = new StreamIntent(); var onWarning = false; self.playingIntent = retStream; function send() { - if (!self.playingIntent) { + if (!self.playingIntent || !self.playing) { self.setSpeaking(false); retStream.emit("end"); return; diff --git a/src/Client/InternalClient.js b/src/Client/InternalClient.js index 55c59e559..2ee3fb8da 100644 --- a/src/Client/InternalClient.js +++ b/src/Client/InternalClient.js @@ -44,7 +44,26 @@ class InternalClient { this.resolver = new Resolver(this); } - //def joinVoiceChannel() + //def leaveVoiceChannel + leaveVoiceChannel(chann){ + var self = this; + return new Promise((resolve, reject) => { + var channel = self.resolver.resolveVoiceChannel(chann); + + if(channel){ + if(self.voiceConnections[channel]){ + var chan = self.voiceConnections[channel]; + chan.stopPlaying(); + self.voiceConnections.remove(chan); + resolve(); + } + }else{ + reject(new Error("voice channel does not exist")); + } + }); + } + + //def joinVoiceChannel joinVoiceChannel(chann){ var self = this; return new Promise((resolve, reject) => { @@ -52,9 +71,7 @@ class InternalClient { var channel = self.resolver.resolveVoiceChannel(chann); if(channel){ - if(!self.voiceConnections[channel]){ - - self.voiceConnections[channel] = {}; + if(!self.voiceConnections.get("id", channel.id)){ var session, token, server = channel.server, endpoint, fired = 0; @@ -67,7 +84,7 @@ class InternalClient { token = data.d.token; endpoint = data.d.endpoint; - var chan = self.voiceConnections[channel] = new VoiceConnection(channel, self.client, session, token, server, endpoint); + var chan = self.voiceConnections.add(new VoiceConnection(channel, self.client, session, token, server, endpoint)); chan.on("ready", resolve); chan.on("error", reject); diff --git a/src/Voice/VoiceConnection.js b/src/Voice/VoiceConnection.js index 7e4e3fd9f..7990acfe5 100644 --- a/src/Voice/VoiceConnection.js +++ b/src/Voice/VoiceConnection.js @@ -17,6 +17,7 @@ class VoiceConnection extends EventEmitter{ if(!Opus){ console.log("HEY! WATCH OUT\n\n discord.js needs node-opus, you don't have it installed."); } + this.id = channel.id; this.voiceChannel = channel; this.client = client; this.session = session; @@ -57,13 +58,13 @@ class VoiceConnection extends EventEmitter{ if (self.playingIntent) { self.stopPlaying(); } - + self.playing = true; var retStream = new StreamIntent(); var onWarning = false; self.playingIntent = retStream; function send() { - if (!self.playingIntent) { + if (!self.playingIntent || !self.playing) { self.setSpeaking(false); retStream.emit("end"); return; diff --git a/test/bot.1.js b/test/bot.1.js index cbf0d1afd..35fbc54d8 100644 --- a/test/bot.1.js +++ b/test/bot.1.js @@ -1,69 +1,52 @@ var Discord = require("../"); -var Member = require("../lib/Member.js"); -var mybot = new Discord.Client({ - compress : true, - catchup : "all" -}); -var fs = require("fs"); -var request = require("request").defaults({ encoding: null }); - -Discord.patchStrings(); - -var server, channel, message, sentMessage = false; - -mybot.on("message", function (message) { - - console.log("Everyone mentioned? " + doned); - doned++; - if (message.content.substr(0, 3) !== "$$$") { - return; - } - - // we can go ahead :) - - var user; - if (message.mentions.length > 0) { - user = message.mentions[0]; - } else { - user = message.sender; - } - - mybot.reply(message, "Hello! It has been " + ((Date.now() - message.timestamp) - this.timeoffset) + "ms since you sent that."); -}); - -var doned = 0; - -mybot.once("ready", function () { - console.log("im ready"); - - for (var server of mybot.servers) { - if (server.name === "test-server") { - mybot.leaveServer(server); +var client = new Discord.Client(); +client.on("debug", (m) => console.log("[debug]",m)); +client.on("warn", (m) => console.log("[warn]", m)); +var start = Date.now(); +client.on("message", m => { + if(m.content === "&init"){ + for(var channel of m.channel.server.channels){ + if(channel instanceof Discord.VoiceChannel){ + client.joinVoiceChannel(channel).catch(error) + .then(connection => { + connection.playFile("C:/users/amish/desktop/Developers.mp3"); + }); + break; + } + } + } + if(m.content.startsWith("$$$ stop")){ + for(var channel of m.channel.server.channels){ + if(channel instanceof Discord.VoiceChannel){ + chan = channel; + break; + } + } + if(client.internal.voiceConnections.get("id", chan.id)){ + var connection = client.internal.voiceConnections.get("id", chan.id); + connection.stopPlaying(); + } + return; + } + if(m.content.startsWith("$$$")){ + var chan; + for(var channel of m.channel.server.channels){ + if(channel instanceof Discord.VoiceChannel){ + chan = channel; + break; + } + } + if(client.internal.voiceConnections.get("id", chan.id)){ + var connection = client.internal.voiceConnections.get("id", chan.id); + connection.playFile("C:/users/amish/desktop/Developers.mp3"); } } - }); -mybot.on("messageUpdate", function(newMessage, oldMessage){ - mybot.reply(newMessage, JSON.stringify(newMessage.embeds)); -}) - -mybot.on("serverUpdate", function (oldserver, newserver) { - console.log("server changed! " + mybot.servers.length); -}) - - -mybot.on("channelUpdate", function (oldChan, newChan) { - -}); - - -function dump(msg) { - console.log("dump", msg); +function error(e){ + console.log(e.stack); + process.exit(0); } -function error(err) { - console.log(err); -} -mybot.login(process.env["ds_email"], process.env["ds_password"]).catch(error); \ No newline at end of file +client.login(process.env["discordEmail"], process.env["discordPass"]).catch((e)=>console.log(e)); \ No newline at end of file