Add pinning (#434)

Added methods:
pinMessage()
unpinMessage()
getPinnedMessages()

Added params for getChannelLogs:
around
This commit is contained in:
Manuel Kraus
2016-06-18 04:21:52 +02:00
committed by abal
parent 9c9768f772
commit ba51df918c
10 changed files with 399 additions and 63 deletions

View File

@@ -639,6 +639,91 @@ export default class Client extends EventEmitter {
.then(dataCallback(callback), errorCallback(callback));
}
/**
* Pins a message to a channel.
* @param {MessageResolvable} message to pin.
* @returns {Promise<null, Error>} resolves null if successful, otherwise rejects with an error.
* @example
* // pin message - callback
* client.pinMessage(msg, (err) => {
* if(!err) {
* console.log("Successfully pinned message")
* } else {
* console.log("Couldn't pin the message: " + err);
* }
* });
* @example
* // pin message - promise
* client.pinMessage(msg)
* .then(() => {
* console.log("Successfully pinned message");
* })
* .catch(err => console.log("Couldn't pin the message: " + err));
*/
pinMessage(msg, callback = (/*err*/) => { }) {
return this.internal.pinMessage(msg)
.then(dataCallback(callback), errorCallback(callback));
}
/**
* Unpins a message to a server.
* @param {MessageResolvable} message to unpin.
* @returns {Promise<null, Error>} resolves null if successful, otherwise rejects with an error.
* @example
* // unpin message - callback
* client.unpinMessage(msg, (err) => {
* if(!err) {
* console.log("Successfully unpinned message")
* } else {
* console.log("Couldn't pin the message: " + err);
* }
* });
* @example
* // unpin message - promise
* client.unpinMessage(msg)
* .then(() => {
* console.log("Successfully unpinned message");
* })
* .catch(err => console.log("Couldn't unpin the message: " + err));
*/
unpinMessage(msg, callback = (/*err*/) => { }) {
return this.internal.unpinMessage(msg)
.then(dataCallback(callback), errorCallback(callback));
}
/**
* Gets all pinned messages of a channel.
* @param {TextChannelResolvable} where to get the pins from.
* @returns {Promise<Array<Message>, Error>} Resolves with an array of messages if successful, otherwise rejects with an error.
* @example
* // log all pinned messages - callback
* client.getPinnedMessages(channel, (err, messages) => {
* if(!err) {
* for(var message of messages) {
* console.log(message.content);
* }
* } else {
* console.log("Couldn't fetch pins: " + err);
* }
* });
* @example
* // log all pinned messages - promise
* client.getPinnedMessages(channel)
* .then(messages => {
* for(var message of messages) {
* console.log(message.content);
* }
* })
* .catch(err => console.log("Couldn't fetch pins: " + err));
*/
getPinnedMessages(channel, callback = (/*err, messages*/) => { }) {
return this.internal.getPinnedMessages(channel)
.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

@@ -776,6 +776,12 @@ export default class InternalClient {
qsObject.after = res.id;
}
}
if (options.around) {
const res = this.resolver.resolveMessage(options.around);
if (res) {
qsObject.around = res.id
}
}
return this.apiRequest(
"get",
@@ -813,7 +819,56 @@ export default class InternalClient {
.then(res => channel.messages.add(
new Message(res, channel, this.client)
));
})
});
}
// def pinMessage
pinMessage(msg) {
var message = this.resolver.resolveMessage(msg);
if(!message) {
return Promise.reject(new Error("Supplied message did not resolve to a message"));
}
return this.apiRequest(
"put",
`${Endpoints.CHANNEL_PIN(msg.channel.id, msg.id)}`,
true
);
}
// def unpinMessage
unpinMessage(msg) {
var message = this.resolver.resolveMessage(msg);
if(!message) {
return Promise.reject(new Error("Supplied message did not resolve to a message"));
}
if(!message.pinned) {
return Promise.reject(new Error("Supplied message is not pinned"));
}
return this.apiRequest(
"del",
`${Endpoints.CHANNEL_PIN(msg.channel.id, msg.id)}`,
true
);
}
// def getPinnedMessages
getPinnedMessages(_channel) {
return this.resolver.resolveChannel(_channel)
.then(channel => {
return this.apiRequest(
"get",
`${Endpoints.CHANNEL_PINS(channel.id)}`,
true
)
.then(res => res.map(
msg => channel.messages.add(new Message(msg, channel, this.client))
));
});
}
// def getBans