From 16d24104502cfbc7e4cd6a003306d9c7de90d4f2 Mon Sep 17 00:00:00 2001 From: hydrabolt Date: Sun, 27 Sep 2015 17:37:46 +0100 Subject: [PATCH] Added setting of channel topics --- lib/Client.js | 38 +++++++++++++++++++++++++++++++++++++- src/Client.js | 42 ++++++++++++++++++++++++++++++++++++++++++ test/bot.1.js | 4 ++++ 3 files changed, 83 insertions(+), 1 deletion(-) diff --git a/lib/Client.js b/lib/Client.js index ecacab82a..32025062d 100644 --- a/lib/Client.js +++ b/lib/Client.js @@ -1141,6 +1141,42 @@ var Client = (function () { } return this.getPMChannel("id", data.id); } + }, { + key: "setTopic", + value: function setTopic(channel, topic) { + var callback = arguments.length <= 2 || arguments[2] === undefined ? function (err) {} : arguments[2]; + + var self = this; + + return new Promise(function (resolve, reject) { + + self.resolveDestination(channel).then(next)["catch"](error); + + function error(e) { + callback(e); + reject(e); + } + + function next(destination) { + + var asChan = self.getChannel("id", destination); + + request.patch(Endpoints.CHANNELS + "/" + destination).set("authorization", self.token).send({ + name: asChan.name, + position: 0, + topic: topic + }).end(function (err, res) { + if (err) { + error(err); + } else { + asChan.topic = res.body.topic; + resolve(); + callback(); + } + }); + } + }); + } //def addServer }, { @@ -1437,7 +1473,7 @@ var Client = (function () { } else { channId = destination; } - if (channId) resolve(channId); + if (channId) resolve(channId);else reject(); }); } }, { diff --git a/src/Client.js b/src/Client.js index 483f9ec3d..08a0ecbde 100644 --- a/src/Client.js +++ b/src/Client.js @@ -1044,6 +1044,46 @@ class Client { return this.getPMChannel("id", data.id); } + setTopic(channel, topic, callback = function(err){}){ + + var self = this; + + return new Promise(function(resolve, reject){ + + self.resolveDestination(channel).then(next).catch(error); + + function error(e){ + callback(e); + reject(e); + } + + function next(destination){ + + var asChan = self.getChannel("id", destination); + + request + .patch(`${Endpoints.CHANNELS}/${destination}`) + .set("authorization", self.token) + .send({ + name : asChan.name, + position : 0, + topic : topic + }) + .end(function(err, res){ + if(err){ + error(err); + }else{ + asChan.topic = res.body.topic; + resolve(); + callback(); + } + }); + } + + }); + + } + //def addServer addServer(data) { @@ -1178,6 +1218,8 @@ class Client { } if (channId) resolve(channId); + else + reject(); }); } diff --git a/test/bot.1.js b/test/bot.1.js index b615255c1..1e425423f 100644 --- a/test/bot.1.js +++ b/test/bot.1.js @@ -30,6 +30,10 @@ mybot.on("message", function (message) { mybot.on("ready", function () { console.log("im ready"); + + for(var chann of mybot.channels){ + mybot.setTopic(chann, "HELLO I AM A BOT BOOP BOOP"); + } }); mybot.on("debug", function(info){