diff --git a/lib/Client/Client.js b/lib/Client/Client.js index 211692c9a..0c1d79d97 100644 --- a/lib/Client/Client.js +++ b/lib/Client/Client.js @@ -643,6 +643,42 @@ var Client = (function (_EventEmitter) { }); }; + //def startTyping + + Client.prototype.startTyping = function startTyping(channel) { + var callback = arguments.length <= 1 || arguments[1] === undefined ? function (err) {} : arguments[1]; + + var self = this; + return new Promise(function (resolve, reject) { + + self.internal.startTyping(channel).then(function () { + callback(null); + resolve(); + })["catch"](function (e) { + callback(e); + reject(e); + }); + }); + }; + + //def stopTyping + + Client.prototype.stopTyping = function stopTyping(channel) { + var callback = arguments.length <= 1 || arguments[1] === undefined ? function (err) {} : arguments[1]; + + var self = this; + return new Promise(function (resolve, reject) { + + self.internal.stopTyping(channel).then(function () { + callback(null); + resolve(); + })["catch"](function (e) { + callback(e); + reject(e); + }); + }); + }; + //def joinVoiceChannel Client.prototype.joinVoiceChannel = function joinVoiceChannel(channel) { diff --git a/lib/Client/InternalClient.js b/lib/Client/InternalClient.js index eaa4a8fba..6ba29e536 100644 --- a/lib/Client/InternalClient.js +++ b/lib/Client/InternalClient.js @@ -46,6 +46,7 @@ var InternalClient = (function () { this.channels = new Cache(); this.servers = new Cache(); this.private_channels = new Cache(); + this.typingIntervals = []; this.voiceConnection = null; this.resolver = new Resolver(this); this.readyTime = null; @@ -934,6 +935,53 @@ var InternalClient = (function () { }); }; + //def startTyping + + InternalClient.prototype.startTyping = function startTyping(channel) { + var self = this; + return new Promise(function (resolve, reject) { + + self.resolver.resolveChannel(channel).then(next)["catch"](reject); + + function next(channel) { + + if (self.typingIntervals[channel.id]) { + // typing interval already exists, leave it alone + reject(new Error("Already typing in that channel")); + return; + } + + self.sendTyping(channel); + + self.typingIntervals[channel.id] = setInterval(function () { + return self.sendTyping(channel); + }, 4000); + } + }); + }; + + //def stopTyping + + InternalClient.prototype.stopTyping = function stopTyping(channel) { + var self = this; + return new Promise(function (resolve, reject) { + + self.resolver.resolveChannel(channel).then(next)["catch"](reject); + + function next(channel) { + + if (!self.typingIntervals[channel.id]) { + // typing interval doesn't exist + reject(new Error("Not typing in that channel")); + return; + } + + clearInterval(self.typingIntervals[channel.id]); + self.typingIntervals[channel.id] = false; + } + }); + }; + //def setTopic InternalClient.prototype.setTopic = function setTopic(chann) { diff --git a/lib/Structures/ServerChannel.js b/lib/Structures/ServerChannel.js index d16f4bc82..0349d33f9 100644 --- a/lib/Structures/ServerChannel.js +++ b/lib/Structures/ServerChannel.js @@ -99,10 +99,14 @@ var ServerChannel = (function (_Channel) { return this.permissionsOf(user); }; - ServerChannel.prototype.toString = function toString() { + ServerChannel.prototype.mention = function mention() { return "<#" + this.id + ">"; }; + ServerChannel.prototype.toString = function toString() { + return this.mention(); + }; + ServerChannel.prototype.setName = function setName() { return this.client.setChannelName.apply(this.client, reg(this, arguments)); }; diff --git a/src/Client/Client.js b/src/Client/Client.js index 73a52ac48..0d1330579 100644 --- a/src/Client/Client.js +++ b/src/Client/Client.js @@ -641,6 +641,42 @@ class Client extends EventEmitter { }) } + + //def startTyping + startTyping(channel, callback = function (err) { }) { + var self = this; + return new Promise((resolve, reject) => { + + self.internal.startTyping(channel) + .then(() => { + callback(null); + resolve(); + }) + .catch(e => { + callback(e); + reject(e); + }); + + }); + } + + //def stopTyping + stopTyping(channel, callback = function (err) { }) { + var self = this; + return new Promise((resolve, reject) => { + + self.internal.stopTyping(channel) + .then(() => { + callback(null); + resolve(); + }) + .catch(e => { + callback(e); + reject(e); + }); + + }); + } //def joinVoiceChannel joinVoiceChannel(channel, callback=function(err){}){ diff --git a/src/Client/InternalClient.js b/src/Client/InternalClient.js index 2b030d6f8..153af83a0 100644 --- a/src/Client/InternalClient.js +++ b/src/Client/InternalClient.js @@ -40,6 +40,7 @@ class InternalClient { this.channels = new Cache(); this.servers = new Cache(); this.private_channels = new Cache(); + this.typingIntervals = []; this.voiceConnection = null; this.resolver = new Resolver(this); this.readyTime = null; @@ -997,6 +998,55 @@ class InternalClient { }); } + + //def startTyping + startTyping(channel){ + var self = this; + return new Promise((resolve, reject) => { + + self.resolver.resolveChannel(channel).then(next).catch(reject); + + function next(channel) { + + if(self.typingIntervals[channel.id]){ + // typing interval already exists, leave it alone + reject(new Error("Already typing in that channel")); + return; + } + + self.sendTyping(channel); + + self.typingIntervals[channel.id] = setInterval( + () => self.sendTyping(channel), 4000 + ); + + } + + }); + } + + //def stopTyping + stopTyping(channel){ + var self = this; + return new Promise((resolve, reject) => { + + self.resolver.resolveChannel(channel).then(next).catch(reject); + + function next(channel) { + + if(!self.typingIntervals[channel.id]){ + // typing interval doesn't exist + reject(new Error("Not typing in that channel")); + return; + } + + clearInterval(self.typingIntervals[channel.id]); + self.typingIntervals[channel.id] = false; + + } + + }); + } //def setTopic setTopic(chann, topic = "") { diff --git a/src/Structures/ServerChannel.js b/src/Structures/ServerChannel.js index 3a51cd9cc..a67aa96ab 100644 --- a/src/Structures/ServerChannel.js +++ b/src/Structures/ServerChannel.js @@ -60,9 +60,13 @@ class ServerChannel extends Channel{ permsOf(user){ return this.permissionsOf(user); } + + mention(){ + return `<#${this.id}>`; + } toString(){ - return `<#${this.id}>`; + return this.mention(); } setName(){ diff --git a/test/bot.1.js b/test/bot.1.js index 4a2a43151..b3f242572 100644 --- a/test/bot.1.js +++ b/test/bot.1.js @@ -6,62 +6,13 @@ client.on("warn", (m) => console.log("[warn]", m)); var start = Date.now(); client.on("message", m => { - if(m.content === "death"){ - m.channel.delete(); - } - if (m.content.startsWith("join: ")) { - var invite = m.content.split(" ")[1]; - client.joinServer(invite).then(console.log).catch(console.log); - } - if (m.content === "&init") { - for (var channel of m.channel.server.channels) { - if (channel instanceof Discord.VoiceChannel) { - client.reply(m, channel.name + " - " + channel.id); - client.joinVoiceChannel(channel).catch(error); - break; - } - } - } - if (m.content.startsWith("$$$ stop")) { - if (client.internal.voiceConnection) { - client.internal.voiceConnection.stopPlaying(); - } - return; - }if (m.content.startsWith("$$$ leave")) { - client.internal.leaveVoiceChannel(); - return; - } - if (m.content.startsWith("$$$")) { - var chan; - var rest = m.content.split(" "); - rest.splice(0, 1); - rest = rest.join(" "); - if (client.internal.voiceConnection) { - client.reply(m, "ok, I'll play that for you"); - var connection = client.internal.voiceConnection; - connection.playFile("C:/users/amish/desktop/" + rest); - } - } if (m.content.startsWith("pipeit")) { - var chan; - var rest = m.content.split(" "); - rest.splice(0, 1); - rest = rest.join(" "); - if (client.internal.voiceConnection) { - var connection = client.internal.voiceConnection; - var request = require("request"); - connection.playRawStream(request(rest)).then(intent => { - client.reply(m, "playing!").then((msg) => { - - intent.on("end", () => { - client.updateMessage(msg, "that song has finished now."); - }); - - }); - - }); - } + if (m.content === "$$") { + client.startTyping(m.channel); + } else if (m.content === "!!") { + client.stopTyping(m.channel); } + }); function error(e) {