Add TextBasedChannel{full}.getMessages()

This commit is contained in:
Amish Shah
2016-08-23 13:51:56 +01:00
parent d710713c09
commit 576a7f2488
6 changed files with 80 additions and 2 deletions

File diff suppressed because one or more lines are too long

View File

@@ -292,6 +292,31 @@ class RESTMethods {
});
}
getChannelMessages(channel, payload = {}) {
return new Promise((resolve, reject) => {
const params = [];
if (payload.limit) {
params.push(`limit=${payload.limit}`);
}
if (payload.around) {
params.push(`around=${payload.around}`);
} else if (payload.before) {
params.push(`before=${payload.before}`);
} else if (payload.after) {
params.push(`after=${payload.after}`);
}
let request = Constants.Endpoints.channelMessages(channel.id);
if (params.length > 0) {
request += `?${params.join('&')}`;
}
this.rest.makeRequest('get', request, true)
.then(resolve)
.catch(reject);
});
}
updateGuildRole(role, _data) {
return new Promise((resolve, reject) => {
/*

View File

@@ -49,6 +49,10 @@ class DMChannel extends Channel {
_cacheMessage() {
return;
}
getMessages() {
return;
}
}
TextBasedChannel.applyToClass(DMChannel, true);

View File

@@ -126,6 +126,9 @@ class GroupDMChannel extends Channel {
return;
}
getMessages() {
return;
}
}
TextBasedChannel.applyToClass(GroupDMChannel, true);

View File

@@ -34,6 +34,10 @@ class TextChannel extends GuildChannel {
_cacheMessage() {
return;
}
getMessages() {
return;
}
}
TextBasedChannel.applyToClass(TextChannel, true);

View File

@@ -41,6 +41,44 @@ class TextBasedChannel {
return this.client.rest.methods.sendMessage(this, content, true);
}
/**
* The parameters to pass in when requesting previous messages from a channel. `around`, `before` and
* `after` are mutually exclusive. All the parameters are optional.
* ```js
* {
* limit: 30, // the message limit, defaults to 50
* before: '123', // gets messages before the given message ID
* after: '123', // gets messages after the given message ID
* around: '123', // gets messages around the given message ID
* }
* ```
* @typedef {Object} ChannelLogsQueryOptions
*/
/**
* Gets the past messages sent in this channel. Resolves with a Collection mapping message ID's to Message objects.
* @param {ChannelLogsQueryOptions} [options={}] the query parameters to pass in
* @returns {Promise<Collection<String, Message>, Error>}
* @example
* // get messages
* channel.getMessages({limit: 10})
* .then(messages => console.log(`Received ${messages.size} messages`))
* .catch(console.log);
*/
getMessages(options = {}) {
return new Promise((resolve, reject) => {
this.client.rest.methods.getChannelMessages(this, options)
.then(data => {
const messages = new Collection();
for (const message of data) {
messages.set(message.id, message);
}
resolve(messages);
})
.catch(reject);
});
}
_cacheMessage(message) {
const maxSize = this.client.options.max_message_cache;
if (maxSize === 0) {
@@ -63,7 +101,11 @@ function applyProp(structure, prop) {
}
exports.applyToClass = (structure, full = false) => {
const props = full ? ['sendMessage', 'sendTTSMessage', '_cacheMessage'] : ['sendMessage', 'sendTTSMessage'];
const props = ['sendMessage', 'sendTTSMessage'];
if (full) {
props.push('_cacheMessage');
props.push('getMessages');
}
for (const prop of props) {
applyProp(structure, prop);
}