mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-12 01:23:31 +01:00
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:
committed by
Schuyler Cebulskie
parent
a359f344d8
commit
5ed8098af8
@@ -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;
|
||||
|
||||
@@ -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');
|
||||
|
||||
21
src/client/actions/MessageReactionRemoveAll.js
Normal file
21
src/client/actions/MessageReactionRemoveAll.js
Normal 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;
|
||||
@@ -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() {
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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;
|
||||
@@ -26,7 +26,7 @@ class Message {
|
||||
if (data) this.setup(data);
|
||||
}
|
||||
|
||||
setup(data) {
|
||||
setup(data) { // eslint-disable-line complexity
|
||||
/**
|
||||
* The ID of the message (unique in the channel it was sent)
|
||||
* @type {string}
|
||||
@@ -389,20 +389,23 @@ class Message {
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a reaction to the message
|
||||
* @param {Emoji|ReactionEmoji|string} emoji Emoji to react with
|
||||
* Add a reaction to the message
|
||||
* @param {string|Emoji|ReactionEmoji} emoji Emoji to react with
|
||||
* @returns {Promise<MessageReaction>}
|
||||
*/
|
||||
react(emoji) {
|
||||
if (emoji.identifier) {
|
||||
emoji = emoji.identifier;
|
||||
} else if (typeof emoji === 'string') {
|
||||
if (!emoji.includes('%')) emoji = encodeURIComponent(emoji);
|
||||
} else {
|
||||
return Promise.reject('The emoji must be a string or an Emoji/ReactionEmoji.');
|
||||
}
|
||||
emoji = this.client.resolver.resolveEmojiIdentifier(emoji);
|
||||
if (!emoji) throw new TypeError('Emoji must be a string or Emoji/ReactionEmoji');
|
||||
|
||||
return this.client.rest.methods.addMessageReaction(this.channel.id, this.id, emoji);
|
||||
return this.client.rest.methods.addMessageReaction(this, emoji);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove all reactions from a message
|
||||
* @returns {Promise<Message>}
|
||||
*/
|
||||
clearReactions() {
|
||||
return this.client.rest.methods.removeMessageReactions(this);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -523,6 +526,10 @@ class Message {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
_clearReactions() {
|
||||
this.reactions.clear();
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Message;
|
||||
|
||||
@@ -64,7 +64,7 @@ class MessageReaction {
|
||||
user = this.message.client.resolver.resolveUserID(user);
|
||||
if (!user) return Promise.reject('Couldn\'t resolve the user ID to remove from the reaction.');
|
||||
return message.client.rest.methods.removeMessageReaction(
|
||||
message.channel.id, message.id, this.emoji.identifier, user
|
||||
message, this.emoji.identifier, user
|
||||
);
|
||||
}
|
||||
|
||||
@@ -77,7 +77,7 @@ class MessageReaction {
|
||||
fetchUsers(limit = 100) {
|
||||
const message = this.message;
|
||||
return message.client.rest.methods.getMessageReactionUsers(
|
||||
message.channel.id, message.id, this.emoji.identifier, limit
|
||||
message, this.emoji.identifier, limit
|
||||
).then(users => {
|
||||
this.users = new Collection();
|
||||
for (const rawUser of users) {
|
||||
|
||||
@@ -207,6 +207,7 @@ exports.Events = {
|
||||
MESSAGE_BULK_DELETE: 'messageDeleteBulk',
|
||||
MESSAGE_REACTION_ADD: 'messageReactionAdd',
|
||||
MESSAGE_REACTION_REMOVE: 'messageReactionRemove',
|
||||
MESSAGE_REACTION_REMOVE_ALL: 'messageReactionRemoveAll',
|
||||
USER_UPDATE: 'userUpdate',
|
||||
USER_NOTE_UPDATE: 'userNoteUpdate',
|
||||
PRESENCE_UPDATE: 'presenceUpdate',
|
||||
@@ -245,6 +246,7 @@ exports.WSEvents = {
|
||||
MESSAGE_DELETE_BULK: 'MESSAGE_DELETE_BULK',
|
||||
MESSAGE_REACTION_ADD: 'MESSAGE_REACTION_ADD',
|
||||
MESSAGE_REACTION_REMOVE: 'MESSAGE_REACTION_REMOVE',
|
||||
MESSAGE_REACTION_REMOVE_ALL: 'MESSAGE_REACTION_REMOVE_ALL',
|
||||
USER_UPDATE: 'USER_UPDATE',
|
||||
USER_NOTE_UPDATE: 'USER_NOTE_UPDATE',
|
||||
PRESENCE_UPDATE: 'PRESENCE_UPDATE',
|
||||
|
||||
Reference in New Issue
Block a user