From 864126976fcf152728d05bdf3e771cec18ba8aa6 Mon Sep 17 00:00:00 2001 From: abalabahaha Date: Fri, 15 Apr 2016 18:16:57 -0700 Subject: [PATCH] Fix sendFile content --- lib/Client/Client.js | 11 ++++-- lib/Client/InternalClient.js | 77 +++++++++++++++++++----------------- src/Client/Client.js | 9 ++++- src/Client/InternalClient.js | 64 ++++++++++++++++-------------- 4 files changed, 91 insertions(+), 70 deletions(-) diff --git a/lib/Client/Client.js b/lib/Client/Client.js index dad84decf..0939e2938 100644 --- a/lib/Client/Client.js +++ b/lib/Client/Client.js @@ -537,16 +537,21 @@ var Client = (function (_EventEmitter) { * .catch(err => console.log("couldn't send file!")); */ - Client.prototype.sendFile = function sendFile(destination, attachment, name) { - var callback = arguments.length <= 3 || arguments[3] === undefined ? function () /*err, m*/{} : arguments[3]; + Client.prototype.sendFile = function sendFile(destination, attachment, name, content) { + var callback = arguments.length <= 4 || arguments[4] === undefined ? function () /*err, m*/{} : arguments[4]; + if (typeof content === "function") { + // content is the callback + callback = content; + content = undefined; // Will get resolved into original filename in internal + } if (typeof name === "function") { // name is the callback callback = name; name = undefined; // Will get resolved into original filename in internal } - return this.internal.sendFile(destination, attachment, name).then(dataCallback(callback), errorCallback(callback)); + return this.internal.sendFile(destination, attachment, name, content).then(dataCallback(callback), errorCallback(callback)); }; /** diff --git a/lib/Client/InternalClient.js b/lib/Client/InternalClient.js index 97f730b5c..805442d23 100644 --- a/lib/Client/InternalClient.js +++ b/lib/Client/InternalClient.js @@ -138,11 +138,15 @@ var InternalClient = (function () { if (useAuth) { ret.set("authorization", this.token); } - if (data) { - ret.send(data); - } if (file) { ret.attach("file", file.file, file.name); + if (data) { + for (var i in data) { + ret.field(i, data[i]); + } + } + } else if (data) { + ret.send(data); } ret.set('User-Agent', this.userAgentInfo.full); return new Promise(function (resolve, reject) { @@ -702,10 +706,40 @@ var InternalClient = (function () { }); }; + // def sendFile + + InternalClient.prototype.sendFile = function sendFile(where, _file, name, content) { + var _this17 = this; + + if (!name) { + if (_file instanceof String || typeof _file === "string") { + name = require("path").basename(_file); + } else if (_file && _file.path) { + // fs.createReadStream()'s have .path that give the path. Not sure about other streams though. + name = require("path").basename(_file.path); + } else { + name = "default.png"; // Just have to go with default filenames. + } + } + + return this.resolver.resolveChannel(where).then(function (channel) { + return _this17.resolver.resolveFile(_file).then(function (file) { + return _this17.apiRequest("post", _Constants.Endpoints.CHANNEL_MESSAGES(channel.id), true, { + content: content + }, { + name: name, + file: file + }).then(function (res) { + return channel.messages.add(new _StructuresMessage2["default"](res, channel, _this17.client)); + }); + }); + }); + }; + // def deleteMessage InternalClient.prototype.deleteMessage = function deleteMessage(_message) { - var _this17 = this; + var _this18 = this; var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; @@ -716,7 +750,7 @@ var InternalClient = (function () { var chain = options.wait ? delay(options.wait) : Promise.resolve(); return chain.then(function () { - return _this17.apiRequest("del", _Constants.Endpoints.CHANNEL_MESSAGE(message.channel.id, message.id), true); + return _this18.apiRequest("del", _Constants.Endpoints.CHANNEL_MESSAGE(message.channel.id, message.id), true); }).then(function () { return message.channel.messages.remove(message); }); @@ -725,7 +759,7 @@ var InternalClient = (function () { // def updateMessage InternalClient.prototype.updateMessage = function updateMessage(msg, _content) { - var _this18 = this; + var _this19 = this; var options = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2]; @@ -741,36 +775,7 @@ var InternalClient = (function () { content: content, tts: options.tts }).then(function (res) { - return message.channel.messages.update(message, new _StructuresMessage2["default"](res, message.channel, _this18.client)); - }); - }; - - // def sendFile - - InternalClient.prototype.sendFile = function sendFile(where, _file, name, content) { - var _this19 = this; - - if (!name) { - if (_file instanceof String || typeof _file === "string") { - name = require("path").basename(_file); - } else if (_file.path) { - // fs.createReadStream()'s have .path that give the path. Not sure about other streams though. - name = require("path").basename(_file.path); - } else { - name = "default.png"; // Just have to go with default filenames. - } - } - - return this.resolver.resolveChannel(where).then(function (channel) { - return _this19.resolver.resolveFile(_file).then(function (file) { - return _this19.apiRequest("post", _Constants.Endpoints.CHANNEL_MESSAGES(channel.id), true, null, { - name: name, - file: file, - content: content - }).then(function (res) { - return channel.messages.add(new _StructuresMessage2["default"](res, channel, _this19.client)); - }); - }); + return message.channel.messages.update(message, new _StructuresMessage2["default"](res, message.channel, _this19.client)); }); }; diff --git a/src/Client/Client.js b/src/Client/Client.js index aeb1cd957..7c7ba52a9 100644 --- a/src/Client/Client.js +++ b/src/Client/Client.js @@ -631,14 +631,19 @@ export default class Client extends EventEmitter { * .then(msg => console.log("sent file!")) * .catch(err => console.log("couldn't send file!")); */ - sendFile(destination, attachment, name, callback = (/*err, m*/) => { }) { + sendFile(destination, attachment, name, content, callback = (/*err, m*/) => { }) { + if (typeof content === "function") { + // content is the callback + callback = content; + content = undefined; // Will get resolved into original filename in internal + } if (typeof name === "function") { // name is the callback callback = name; name = undefined; // Will get resolved into original filename in internal } - return this.internal.sendFile(destination, attachment, name) + return this.internal.sendFile(destination, attachment, name, content) .then(dataCallback(callback), errorCallback(callback)); } diff --git a/src/Client/InternalClient.js b/src/Client/InternalClient.js index eba59b10a..da8058437 100644 --- a/src/Client/InternalClient.js +++ b/src/Client/InternalClient.js @@ -68,11 +68,15 @@ export default class InternalClient { if (useAuth) { ret.set("authorization", this.token); } - if (data) { - ret.send(data); - } if (file) { ret.attach("file", file.file, file.name); + if (data) { + for (var i in data) { + ret.field(i, data[i]); + } + } + } else if (data) { + ret.send(data); } ret.set('User-Agent', this.userAgentInfo.full); return new Promise((resolve, reject) => { @@ -569,6 +573,34 @@ export default class InternalClient { }); } + + // def sendFile + sendFile(where, _file, name, content) { + if (!name) { + if (_file instanceof String || typeof _file === "string") { + name = require("path").basename(_file); + } else if (_file && _file.path) { + // fs.createReadStream()'s have .path that give the path. Not sure about other streams though. + name = require("path").basename(_file.path); + } else { + name = "default.png"; // Just have to go with default filenames. + } + } + + return this.resolver.resolveChannel(where) + .then(channel => + this.resolver.resolveFile(_file) + .then(file => + this.apiRequest("post", Endpoints.CHANNEL_MESSAGES(channel.id), true, { + content + }, { + name, + file + }).then(res => channel.messages.add(new Message(res, channel, this.client))) + ) + ); + } + // def deleteMessage deleteMessage(_message, options = {}) { @@ -610,32 +642,6 @@ export default class InternalClient { )); } - // def sendFile - sendFile(where, _file, name, content) { - if (!name) { - if (_file instanceof String || typeof _file === "string") { - name = require("path").basename(_file); - } else if (_file.path) { - // fs.createReadStream()'s have .path that give the path. Not sure about other streams though. - name = require("path").basename(_file.path); - } else { - name = "default.png"; // Just have to go with default filenames. - } - } - - return this.resolver.resolveChannel(where) - .then(channel => - this.resolver.resolveFile(_file) - .then(file => - this.apiRequest("post", Endpoints.CHANNEL_MESSAGES(channel.id), true, null, { - name, - file, - content - }).then(res => channel.messages.add(new Message(res, channel, this.client))) - ) - ); - } - // def getChannelLogs getChannelLogs(_channel, limit = 50, options = {}) { return this.resolver.resolveChannel(_channel)