Clean up reactions, add remove all reactions (#890)

* Clean up reactions, add remove all reactions

* Reorganize reactions

* Re-add Gawdl3y's precious little inline

* Update Message.js
This commit is contained in:
Programmix
2016-11-12 23:29:26 -08:00
committed by Schuyler Cebulskie
parent a359f344d8
commit 5ed8098af8
9 changed files with 97 additions and 26 deletions

View File

@@ -8,6 +8,8 @@ const Message = require(`../structures/Message`);
const Guild = require(`../structures/Guild`);
const Channel = require(`../structures/Channel`);
const GuildMember = require(`../structures/GuildMember`);
const Emoji = require(`../structures/Emoji`);
const ReactionEmoji = require(`../structures/ReactionEmoji`);
/**
* The DataResolver identifies different objects and tries to resolve a specific piece of information from them, e.g.
@@ -264,6 +266,27 @@ class ClientDataResolver {
return Promise.reject(new TypeError('The resource must be a string or Buffer.'));
}
/**
* Data that can be resolved to give an emoji identifier. This can be:
* * A string
* * An Emoji
* * A ReactionEmoji
* @typedef {string|Emoji|ReactionEmoji} EmojiIdentifierResolvable
*/
/**
* Resolves an EmojiResolvable to an emoji identifier
* @param {EmojiIdentifierResolvable} emoji The emoji resolvable to resolve
* @returns {string}
*/
resolveEmojiIdentifier(emoji) {
if (emoji instanceof Emoji || emoji instanceof ReactionEmoji) return emoji.identifier;
if (typeof emoji === 'string') {
if (!emoji.includes('%')) return encodeURIComponent(emoji);
}
return null;
}
}
module.exports = ClientDataResolver;

View File

@@ -8,6 +8,7 @@ class ActionsManager {
this.register('MessageUpdate');
this.register('MessageReactionAdd');
this.register('MessageReactionRemove');
this.register('MessageReactionRemoveAll');
this.register('ChannelCreate');
this.register('ChannelDelete');
this.register('ChannelUpdate');

View File

@@ -0,0 +1,21 @@
const Action = require('./Action');
const Constants = require('../../util/Constants');
class MessageReactionRemoveAll extends Action {
handle(data) {
const channel = this.client.channels.get(data.channel_id);
if (!channel || channel.type === 'voice') return false;
const message = channel.messages.get(data.message_id);
if (!message) return false;
message._clearReactions();
this.client.emit(Constants.Events.MESSAGE_REACTION_REMOVE_ALL, message);
return {
message,
};
}
}
module.exports = MessageReactionRemoveAll;

View File

@@ -582,35 +582,40 @@ class RESTMethods {
);
}
addMessageReaction(channelID, messageID, emoji) {
return this.rest.makeRequest('put', Constants.Endpoints.selfMessageReaction(channelID, messageID, emoji), true)
addMessageReaction(message, emoji) {
return this.rest.makeRequest('put', Constants.Endpoints.selfMessageReaction(message.channel.id, message.id, emoji), true)
.then(() =>
this.rest.client.actions.MessageReactionAdd.handle({
user_id: this.rest.client.user.id,
message_id: messageID,
message_id: message.id,
emoji: parseEmoji(emoji),
channel_id: channelID,
channel_id: message.channel.id,
}).reaction
);
}
removeMessageReaction(channelID, messageID, emoji, userID) {
let endpoint = Constants.Endpoints.selfMessageReaction(channelID, messageID, emoji);
if (userID !== this.rest.client.user.id) {
endpoint = Constants.Endpoints.userMessageReaction(channelID, messageID, emoji, null, userID);
removeMessageReaction(message, emoji, user) {
let endpoint = Constants.Endpoints.selfMessageReaction(message.channel.id, message.id, emoji);
if (user.id !== this.rest.client.user.id) {
endpoint = Constants.Endpoints.userMessageReaction(message.channel.id, message.id, emoji, null, user.id);
}
return this.rest.makeRequest('delete', endpoint, true).then(() =>
this.rest.client.actions.MessageReactionRemove.handle({
user_id: userID,
message_id: messageID,
user_id: user.id,
message_id: message.id,
emoji: parseEmoji(emoji),
channel_id: channelID,
channel_id: message.channel.id,
}).reaction
);
}
getMessageReactionUsers(channelID, messageID, emoji, limit = 100) {
return this.rest.makeRequest('get', Constants.Endpoints.messageReaction(channelID, messageID, emoji, limit), true);
removeMessageReactions(message) {
this.rest.makeRequest('delete', Constants.Endpoints.messageReactions(message.channel.id, message.id), true)
.then(() => message);
}
getMessageReactionUsers(message, emoji, limit = 100) {
return this.rest.makeRequest('get', Constants.Endpoints.messageReaction(message.channel.id, message.id, emoji, limit), true);
}
getMyApplication() {

View File

@@ -47,6 +47,7 @@ class WebSocketPacketManager {
this.register(Constants.WSEvents.RELATIONSHIP_REMOVE, 'RelationshipRemove');
this.register(Constants.WSEvents.MESSAGE_REACTION_ADD, 'MessageReactionAdd');
this.register(Constants.WSEvents.MESSAGE_REACTION_REMOVE, 'MessageReactionRemove');
this.register(Constants.WSEvents.MESSAGE_REACTION_REMOVE_ALL, 'MessageReactionRemoveAll');
}
get client() {

View File

@@ -0,0 +1,11 @@
const AbstractHandler = require('./AbstractHandler');
class MessageReactionRemoveAll extends AbstractHandler {
handle(packet) {
const client = this.packetManager.client;
const data = packet.d;
client.actions.MessageReactionRemoveAll.handle(data);
}
}
module.exports = MessageReactionRemoveAll;