Add client.getMessage() (#428)

* Add client.getMessage

* Add shortcuts

* build it..

* Add missing ~ in note docs

* ............
This commit is contained in:
Manuel Kraus
2016-06-16 00:31:02 +02:00
committed by abal
parent 0f4566dd23
commit 0e4612d7ab
11 changed files with 212 additions and 67 deletions

View File

@@ -309,6 +309,17 @@ Gets a list of previously sent messages in a channel.
- **error** - error if any occurred
- **messages** - `array` of Message_ objects sent in channel
getMessage(channel, messageID, `callback`)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Gets a message. This also works for messages that aren't cached but will only work for OAuth bot accounts.
- **channel** - The Channel_ to get the message from.
- **messageID** - The message id to get the message object from. A `String`
- **callback** - `function` taking the following:
- **error** - error if any occurred
- **message** - The `Message_`
getBans(server, `callback`)
~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -823,14 +834,14 @@ Set the nickname of a user on a server.
- **error** - error if any occurred.
setNote(user, note, `callback`)
~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~
Set the note of a user. This will only work for user accounts.
Set the note of a user. This will only work for user accounts.
- **user** - A `User Resolvable`_ to which the note is applied.
- **note** - `String`, content of the note, or `null` to clear.
- **callback** - `function` taking the following:
- **error** - error if any occurred.
- **user** - A `User Resolvable`_ to which the note is applied.
- **note** - `String`, content of the note, or `null` to clear.
- **callback** - `function` taking the following:
- **error** - error if any occurred.
Events
------

View File

@@ -515,6 +515,35 @@ var Client = (function (_EventEmitter) {
return this.internal.getChannelLogs(where, limit, options).then(dataCallback(callback), errorCallback(callback));
};
/**
* Gets a single message of a server
* @param {ChannelResolvable} channel to get the message from
* @param {function(err: Error, msg: Message} [callback] callback to the method
* @returns {Promise<Message, Error>} Resolves with a message if the request was successful, otherwise rejects with an error.
* @example
* // get message object off a snowflake and log its content - callback
* client.getMessage(channel, '192696158886428672', function(err, msg) {
* if(!err) {
* console.log(msg.content);
* } else {
* console.log("couldn't get the message");
* }
* }
* @example
* //get message object off a snowflake and log its content - promise
* client.getMessage(channel, '192696158886428672')
* .then(msg => {
* console.log(msg.content);
* })
* .catch(err => console.log("couldn't get the message"));
*/
Client.prototype.getMessage = function getMessage(channel, messageID) {
var callback = arguments.length <= 2 || arguments[2] === undefined ? function () /*err, msg*/{} : arguments[2];
return this.internal.getMessage(channel, messageID).then(dataCallback(callback), errorCallback(callback));
};
/**
* Gets the banned users of a server (if the client has permission to)
* @param {ServerResolvable} server server to get banned users of

View File

@@ -918,16 +918,41 @@ var InternalClient = (function () {
});
};
// def getMessage
InternalClient.prototype.getMessage = function getMessage(_channel, messageID) {
var _this20 = this;
return this.resolver.resolveChannel(_channel).then(function (channel) {
if (!_this20.user.bot) {
return Promise.reject(new Error("Only OAuth bot accounts can use this function"));
}
if (!(channel instanceof _StructuresTextChannel2["default"] || channel instanceof _StructuresPMChannel2["default"])) {
return Promise.reject(new Error("Provided channel is not a Text or PMChannel"));
}
var msg = channel.messages.get("id", messageID);
if (msg) {
return Promise.resolve(msg);
}
return _this20.apiRequest("get", _Constants.Endpoints.CHANNEL_MESSAGES(channel.id) + "/" + messageID, true).then(function (res) {
return channel.messages.add(new _StructuresMessage2["default"](res, channel, _this20.client));
});
});
};
// def getBans
InternalClient.prototype.getBans = function getBans(server) {
var _this20 = this;
var _this21 = this;
server = this.resolver.resolveServer(server);
return this.apiRequest("get", _Constants.Endpoints.SERVER_BANS(server.id), true).then(function (res) {
return res.map(function (ban) {
return _this20.users.add(new _StructuresUser2["default"](ban.user, _this20.client));
return _this21.users.add(new _StructuresUser2["default"](ban.user, _this21.client));
});
});
};
@@ -935,7 +960,7 @@ var InternalClient = (function () {
// def createChannel
InternalClient.prototype.createChannel = function createChannel(server, name) {
var _this21 = this;
var _this22 = this;
var type = arguments.length <= 2 || arguments[2] === undefined ? "text" : arguments[2];
@@ -947,23 +972,23 @@ var InternalClient = (function () {
}).then(function (res) {
var channel;
if (res.type === "text") {
channel = new _StructuresTextChannel2["default"](res, _this21.client, server);
channel = new _StructuresTextChannel2["default"](res, _this22.client, server);
} else {
channel = new _StructuresVoiceChannel2["default"](res, _this21.client, server);
channel = new _StructuresVoiceChannel2["default"](res, _this22.client, server);
}
return server.channels.add(_this21.channels.add(channel));
return server.channels.add(_this22.channels.add(channel));
});
};
// def deleteChannel
InternalClient.prototype.deleteChannel = function deleteChannel(_channel) {
var _this22 = this;
var _this23 = this;
return this.resolver.resolveChannel(_channel).then(function (channel) {
return _this22.apiRequest("del", _Constants.Endpoints.CHANNEL(channel.id), true).then(function () {
return _this23.apiRequest("del", _Constants.Endpoints.CHANNEL(channel.id), true).then(function () {
channel.server.channels.remove(channel);
_this22.channels.remove(channel);
_this23.channels.remove(channel);
});
});
};
@@ -1001,7 +1026,7 @@ var InternalClient = (function () {
// def moveMember
InternalClient.prototype.moveMember = function moveMember(user, channel) {
var _this23 = this;
var _this24 = this;
user = this.resolver.resolveUser(user);
return this.resolver.resolveChannel(channel).then(function (channel) {
@@ -1011,7 +1036,7 @@ var InternalClient = (function () {
if (channel.type !== "voice") {
throw new Error("Can't moveMember into a non-voice channel");
} else {
return _this23.apiRequest("patch", _Constants.Endpoints.SERVER_MEMBERS(server.id) + "/" + user.id, true, { channel_id: channel.id }).then(function (res) {
return _this24.apiRequest("patch", _Constants.Endpoints.SERVER_MEMBERS(server.id) + "/" + user.id, true, { channel_id: channel.id }).then(function (res) {
user.voiceChannel = channel;
return res;
});
@@ -1076,15 +1101,15 @@ var InternalClient = (function () {
// def createRole
InternalClient.prototype.createRole = function createRole(server, data) {
var _this24 = this;
var _this25 = this;
server = this.resolver.resolveServer(server);
return this.apiRequest("post", _Constants.Endpoints.SERVER_ROLES(server.id), true).then(function (res) {
var role = server.roles.add(new _StructuresRole2["default"](res, server, _this24.client));
var role = server.roles.add(new _StructuresRole2["default"](res, server, _this25.client));
if (data) {
return _this24.updateRole(role, data);
return _this25.updateRole(role, data);
}
return role;
});
@@ -1093,7 +1118,7 @@ var InternalClient = (function () {
// def updateRole
InternalClient.prototype.updateRole = function updateRole(role, data) {
var _this25 = this;
var _this26 = this;
role = this.resolver.resolveRole(role);
var server = this.resolver.resolveServer(role.server);
@@ -1132,7 +1157,7 @@ var InternalClient = (function () {
}
return this.apiRequest("patch", _Constants.Endpoints.SERVER_ROLES(server.id) + "/" + role.id, true, newData).then(function (res) {
return server.roles.update(role, new _StructuresRole2["default"](res, server, _this25.client));
return server.roles.update(role, new _StructuresRole2["default"](res, server, _this26.client));
});
};
@@ -1149,7 +1174,7 @@ var InternalClient = (function () {
//def addMemberToRole
InternalClient.prototype.addMemberToRole = function addMemberToRole(member, roles) {
var _this26 = this;
var _this27 = this;
member = this.resolver.resolveUser(member);
@@ -1166,7 +1191,7 @@ var InternalClient = (function () {
}
} else {
roles = roles.map(function (r) {
return _this26.resolver.resolveRole(r);
return _this27.resolver.resolveRole(r);
});
}
@@ -1229,7 +1254,7 @@ var InternalClient = (function () {
//def removeMemberFromRole
InternalClient.prototype.removeMemberFromRole = function removeMemberFromRole(member, roles) {
var _this27 = this;
var _this28 = this;
member = this.resolver.resolveUser(member);
@@ -1246,7 +1271,7 @@ var InternalClient = (function () {
}
} else {
roles = roles.map(function (r) {
return _this27.resolver.resolveRole(r);
return _this28.resolver.resolveRole(r);
});
}
@@ -1287,7 +1312,7 @@ var InternalClient = (function () {
// def createInvite
InternalClient.prototype.createInvite = function createInvite(chanServ, options) {
var _this28 = this;
var _this29 = this;
return this.resolver.resolveChannel(chanServ).then(function (channel) {
if (!options) {
@@ -1301,8 +1326,8 @@ var InternalClient = (function () {
options.xkcdpass = options.xkcd || false;
}
return _this28.apiRequest("post", _Constants.Endpoints.CHANNEL_INVITES(channel.id), true, options).then(function (res) {
return new _StructuresInvite2["default"](res, _this28.channels.get("id", res.channel.id), _this28.client);
return _this29.apiRequest("post", _Constants.Endpoints.CHANNEL_INVITES(channel.id), true, options).then(function (res) {
return new _StructuresInvite2["default"](res, _this29.channels.get("id", res.channel.id), _this29.client);
});
});
};
@@ -1320,7 +1345,7 @@ var InternalClient = (function () {
//def getInvite
InternalClient.prototype.getInvite = function getInvite(invite) {
var _this29 = this;
var _this30 = this;
invite = this.resolver.resolveInviteID(invite);
if (!invite) {
@@ -1328,11 +1353,11 @@ var InternalClient = (function () {
}
return this.apiRequest("get", _Constants.Endpoints.INVITE(invite), true).then(function (res) {
if (!_this29.channels.has("id", res.channel.id)) {
return new _StructuresInvite2["default"](res, null, _this29.client);
if (!_this30.channels.has("id", res.channel.id)) {
return new _StructuresInvite2["default"](res, null, _this30.client);
}
return _this29.apiRequest("post", _Constants.Endpoints.CHANNEL_INVITES(res.channel.id), true, { validate: invite }).then(function (res2) {
return new _StructuresInvite2["default"](res2, _this29.channels.get("id", res.channel.id), _this29.client);
return _this30.apiRequest("post", _Constants.Endpoints.CHANNEL_INVITES(res.channel.id), true, { validate: invite }).then(function (res2) {
return new _StructuresInvite2["default"](res2, _this30.channels.get("id", res.channel.id), _this30.client);
});
});
};
@@ -1340,22 +1365,22 @@ var InternalClient = (function () {
//def getInvites
InternalClient.prototype.getInvites = function getInvites(channel) {
var _this30 = this;
var _this31 = this;
if (!(channel instanceof _StructuresChannel2["default"])) {
var server = this.resolver.resolveServer(channel);
if (server) {
return this.apiRequest("get", _Constants.Endpoints.SERVER_INVITES(server.id), true).then(function (res) {
return res.map(function (data) {
return new _StructuresInvite2["default"](data, _this30.channels.get("id", data.channel.id), _this30.client);
return new _StructuresInvite2["default"](data, _this31.channels.get("id", data.channel.id), _this31.client);
});
});
}
}
return this.resolver.resolveChannel(channel).then(function (channel) {
return _this30.apiRequest("get", _Constants.Endpoints.CHANNEL_INVITES(channel.id), true).then(function (res) {
return _this31.apiRequest("get", _Constants.Endpoints.CHANNEL_INVITES(channel.id), true).then(function (res) {
return res.map(function (data) {
return new _StructuresInvite2["default"](data, _this30.channels.get("id", data.channel.id), _this30.client);
return new _StructuresInvite2["default"](data, _this31.channels.get("id", data.channel.id), _this31.client);
});
});
});
@@ -1364,7 +1389,7 @@ var InternalClient = (function () {
//def overwritePermissions
InternalClient.prototype.overwritePermissions = function overwritePermissions(channel, role, updated) {
var _this31 = this;
var _this32 = this;
return this.resolver.resolveChannel(channel).then(function (channel) {
if (!channel instanceof _StructuresServerChannel2["default"]) {
@@ -1377,7 +1402,7 @@ var InternalClient = (function () {
};
if (role instanceof String || typeof role === "string") {
role = _this31.resolver.resolveUser(role) || _this31.resolver.resolveRole(role);
role = _this32.resolver.resolveUser(role) || _this32.resolver.resolveRole(role);
}
if (role instanceof _StructuresUser2["default"]) {
@@ -1410,7 +1435,7 @@ var InternalClient = (function () {
}
}
return _this31.apiRequest("put", _Constants.Endpoints.CHANNEL_PERMISSIONS(channel.id) + "/" + data.id, true, data);
return _this32.apiRequest("put", _Constants.Endpoints.CHANNEL_PERMISSIONS(channel.id) + "/" + data.id, true, data);
});
};
@@ -1450,49 +1475,49 @@ var InternalClient = (function () {
//def sendTyping
InternalClient.prototype.sendTyping = function sendTyping(channel) {
var _this32 = this;
var _this33 = this;
return this.resolver.resolveChannel(channel).then(function (channel) {
return _this32.apiRequest("post", _Constants.Endpoints.CHANNEL(channel.id) + "/typing", true);
return _this33.apiRequest("post", _Constants.Endpoints.CHANNEL(channel.id) + "/typing", true);
});
};
//def startTyping
InternalClient.prototype.startTyping = function startTyping(channel) {
var _this33 = this;
var _this34 = this;
return this.resolver.resolveChannel(channel).then(function (channel) {
if (_this33.intervals.typing[channel.id]) {
if (_this34.intervals.typing[channel.id]) {
// typing interval already exists, leave it alone
throw new Error("Already typing in that channel");
}
_this33.intervals.typing[channel.id] = setInterval(function () {
return _this33.sendTyping(channel)["catch"](function (error) {
return _this33.emit("error", error);
_this34.intervals.typing[channel.id] = setInterval(function () {
return _this34.sendTyping(channel)["catch"](function (error) {
return _this34.emit("error", error);
});
}, 4000);
return _this33.sendTyping(channel);
return _this34.sendTyping(channel);
});
};
//def stopTyping
InternalClient.prototype.stopTyping = function stopTyping(channel) {
var _this34 = this;
var _this35 = this;
return this.resolver.resolveChannel(channel).then(function (channel) {
if (!_this34.intervals.typing[channel.id]) {
if (!_this35.intervals.typing[channel.id]) {
// typing interval doesn"t exist
throw new Error("Not typing in that channel");
}
clearInterval(_this34.intervals.typing[channel.id]);
_this34.intervals.typing[channel.id] = false;
clearInterval(_this35.intervals.typing[channel.id]);
_this35.intervals.typing[channel.id] = false;
});
};
@@ -1583,7 +1608,7 @@ var InternalClient = (function () {
//def updateChannel
InternalClient.prototype.updateChannel = function updateChannel(channel, data) {
var _this35 = this;
var _this36 = this;
return this.resolver.resolveChannel(channel).then(function (channel) {
if (!channel) {
@@ -1614,7 +1639,7 @@ var InternalClient = (function () {
data.bitrate *= 1000; // convert to bits before sending
}
return _this35.apiRequest("patch", _Constants.Endpoints.CHANNEL(channel.id), true, data).then(function (res) {
return _this36.apiRequest("patch", _Constants.Endpoints.CHANNEL(channel.id), true, data).then(function (res) {
channel.name = data.name;
channel.topic = data.topic;
channel.position = data.position;
@@ -1671,7 +1696,7 @@ var InternalClient = (function () {
};
InternalClient.prototype.createWS = function createWS(url) {
var _this36 = this;
var _this37 = this;
var self = this;
var client = self.client;
@@ -1710,14 +1735,14 @@ var InternalClient = (function () {
this.websocket.onclose = function (code) {
self.websocket = null;
self.state = _ConnectionState2["default"].DISCONNECTED;
self.disconnected(_this36.client.options.autoReconnect);
self.disconnected(_this37.client.options.autoReconnect);
};
this.websocket.onerror = function (e) {
client.emit("error", e);
self.websocket = null;
self.state = _ConnectionState2["default"].DISCONNECTED;
self.disconnected(_this36.client.options.autoReconnect);
self.disconnected(_this37.client.options.autoReconnect);
};
this.websocket.onmessage = function (e) {
@@ -1746,11 +1771,11 @@ var InternalClient = (function () {
self.user = self.users.add(new _StructuresUser2["default"](data.user, client));
_this36.forceFetchCount = {};
_this36.forceFetchQueue = [];
_this36.forceFetchLength = 1;
_this36.autoReconnectInterval = 1000;
_this36.sessionID = data.session_id;
_this37.forceFetchCount = {};
_this37.forceFetchQueue = [];
_this37.forceFetchLength = 1;
_this37.autoReconnectInterval = 1000;
_this37.sessionID = data.session_id;
data.guilds.forEach(function (server) {
if (!server.unavailable) {
@@ -2214,7 +2239,7 @@ var InternalClient = (function () {
data.id = data.id || user.id;
data.avatar = data.avatar || user.avatar;
data.discriminator = data.discriminator || user.discriminator;
_this36.email = data.email || _this36.email;
_this37.email = data.email || _this37.email;
var presenceUser = new _StructuresUser2["default"](data, client);
@@ -2272,7 +2297,7 @@ var InternalClient = (function () {
}
break;
case _Constants.PacketType.USER_NOTE_UPDATE:
if (_this36.user.bot) {
if (_this37.user.bot) {
return;
}
var user = self.users.get("id", data.id);
@@ -2376,7 +2401,7 @@ var InternalClient = (function () {
break;
case _Constants.PacketType.FRIEND_ADD:
if (_this36.user.bot) {
if (_this37.user.bot) {
return;
}
if (data.type === 1) {
@@ -2407,7 +2432,7 @@ var InternalClient = (function () {
}
break;
case _Constants.PacketType.FRIEND_REMOVE:
if (_this36.user.bot) {
if (_this37.user.bot) {
return;
}
var user = self.friends.get("id", data.id);

View File

@@ -76,6 +76,10 @@ var PMChannel = (function (_Channel) {
return this.client.getChannelLogs.apply(this.client, _UtilArgumentRegulariser.reg(this, arguments));
};
PMChannel.prototype.getMessage = function getMessage() {
return this.client.getMessage.apply(this.client, _UtilArgumentRegulariser.reg(this, arguments));
};
_createClass(PMChannel, [{
key: "lastMessage",
get: function get() {

View File

@@ -67,6 +67,10 @@ var TextChannel = (function (_ServerChannel) {
return this.client.getChannelLogs.apply(this.client, _UtilArgumentRegulariser.reg(this, arguments));
};
TextChannel.prototype.getMessage = function getMessage() {
return this.client.getMessage.apply(this.client, _UtilArgumentRegulariser.reg(this, arguments));
};
TextChannel.prototype.startTyping = function startTyping() {
return this.client.startTyping.apply(this.client, _UtilArgumentRegulariser.reg(this, arguments));
};

View File

@@ -98,6 +98,10 @@ var User = (function (_Equality) {
return this.client.getChannelLogs.apply(this.client, _UtilArgumentRegulariser.reg(this, arguments));
};
User.prototype.getMessage = function getMessage() {
return this.client.getMessage.apply(this.client, _UtilArgumentRegulariser.reg(this, arguments));
};
User.prototype.hasRole = function hasRole(role) {
return this.client.memberHasRole.apply(this.client, [this, role]);
};

View File

@@ -611,6 +611,34 @@ export default class Client extends EventEmitter {
.then(dataCallback(callback), errorCallback(callback));
}
/**
* Gets a single message of a server
* @param {ChannelResolvable} channel to get the message from
* @param {function(err: Error, msg: Message} [callback] callback to the method
* @returns {Promise<Message, Error>} Resolves with a message if the request was successful, otherwise rejects with an error.
* @example
* // get message object off a snowflake and log its content - callback
* client.getMessage(channel, '192696158886428672', function(err, msg) {
* if(!err) {
* console.log(msg.content);
* } else {
* console.log("couldn't get the message");
* }
* }
* @example
* //get message object off a snowflake and log its content - promise
* client.getMessage(channel, '192696158886428672')
* .then(msg => {
* console.log(msg.content);
* })
* .catch(err => console.log("couldn't get the message"));
*/
getMessage(channel, messageID, callback = (/*err, msg*/) => { }) {
return this.internal.getMessage(channel, messageID)
.then(dataCallback(callback), errorCallback(callback));
}
/**
* Gets the banned users of a server (if the client has permission to)
* @param {ServerResolvable} server server to get banned users of

View File

@@ -788,6 +788,34 @@ export default class InternalClient {
});
}
// def getMessage
getMessage(_channel, messageID) {
return this.resolver.resolveChannel(_channel)
.then(channel => {
if(!this.user.bot) {
return Promise.reject(new Error("Only OAuth bot accounts can use this function"));
}
if(!(channel instanceof TextChannel || channel instanceof PMChannel)) {
return Promise.reject(new Error("Provided channel is not a Text or PMChannel"));
}
var msg = channel.messages.get("id", messageID);
if(msg) {
return Promise.resolve(msg);
}
return this.apiRequest(
"get",
`${Endpoints.CHANNEL_MESSAGES(channel.id)}/${messageID}`,
true
)
.then(res => channel.messages.add(
new Message(res, channel, this.client)
));
})
}
// def getBans
getBans(server) {
server = this.resolver.resolveServer(server);

View File

@@ -55,4 +55,8 @@ export default class PMChannel extends Channel {
getLogs() {
return this.client.getChannelLogs.apply(this.client, reg(this, arguments));
}
getMessage() {
return this.client.getMessage.apply(this.client, reg(this, arguments));
}
}

View File

@@ -50,6 +50,10 @@ export default class TextChannel extends ServerChannel{
return this.client.getChannelLogs.apply(this.client, reg(this, arguments));
}
getMessage() {
return this.client.getMessage.apply(this.client, reg(this, arguments));
}
startTyping(){
return this.client.startTyping.apply(this.client, reg(this, arguments));
}

View File

@@ -114,6 +114,10 @@ export default class User extends Equality{
return this.client.getChannelLogs.apply(this.client, reg(this, arguments));
}
getMessage() {
return this.client.getMessage.apply(this.client, reg(this, arguments));
}
hasRole(role) {
return this.client.memberHasRole.apply(this.client, [this, role]);
}