mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-11 09:03:29 +01:00
Start work on adding reaction support
This commit is contained in:
@@ -6,6 +6,8 @@ class ActionsManager {
|
||||
this.register('MessageDelete');
|
||||
this.register('MessageDeleteBulk');
|
||||
this.register('MessageUpdate');
|
||||
this.register('MessageReactionAdd');
|
||||
this.register('MessageReactionRemove');
|
||||
this.register('ChannelCreate');
|
||||
this.register('ChannelDelete');
|
||||
this.register('ChannelUpdate');
|
||||
|
||||
38
src/client/actions/MessageReactionAdd.js
Normal file
38
src/client/actions/MessageReactionAdd.js
Normal file
@@ -0,0 +1,38 @@
|
||||
const Action = require('./Action');
|
||||
const Constants = require('../../util/Constants');
|
||||
|
||||
/*
|
||||
{ user_id: 'id',
|
||||
message_id: 'id',
|
||||
emoji: { name: '<27>', id: null },
|
||||
channel_id: 'id' } }
|
||||
*/
|
||||
|
||||
class MessageReactionAdd extends Action {
|
||||
handle(data) {
|
||||
const user = this.client.users.get(data.user_id);
|
||||
if (!user) return false;
|
||||
|
||||
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;
|
||||
|
||||
if (!data.emoji) return false;
|
||||
|
||||
const reaction = message._addReaction(data.emoji, user);
|
||||
|
||||
if (reaction) {
|
||||
this.client.emit(Constants.Events.MESSAGE_REACTION_ADD, reaction, user);
|
||||
}
|
||||
|
||||
return {
|
||||
message,
|
||||
reaction,
|
||||
user,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = MessageReactionAdd;
|
||||
38
src/client/actions/MessageReactionRemove.js
Normal file
38
src/client/actions/MessageReactionRemove.js
Normal file
@@ -0,0 +1,38 @@
|
||||
const Action = require('./Action');
|
||||
const Constants = require('../../util/Constants');
|
||||
|
||||
/*
|
||||
{ user_id: 'id',
|
||||
message_id: 'id',
|
||||
emoji: { name: '<27>', id: null },
|
||||
channel_id: 'id' } }
|
||||
*/
|
||||
|
||||
class MessageReactionRemove extends Action {
|
||||
handle(data) {
|
||||
const user = this.client.users.get(data.user_id);
|
||||
if (!user) return false;
|
||||
|
||||
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;
|
||||
|
||||
if (!data.emoji) return false;
|
||||
|
||||
const reaction = message._removeReaction(data.emoji, user);
|
||||
|
||||
if (reaction) {
|
||||
this.client.emit(Constants.Events.MESSAGE_REACTION_REMOVE, reaction, user);
|
||||
}
|
||||
|
||||
return {
|
||||
message,
|
||||
reaction,
|
||||
user,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = MessageReactionRemove;
|
||||
@@ -1,6 +1,7 @@
|
||||
const Constants = require('../../util/Constants');
|
||||
const Collection = require('../../util/Collection');
|
||||
const splitMessage = require('../../util/SplitMessage');
|
||||
const parseEmoji = require('../../util/ParseEmoji');
|
||||
|
||||
const requireStructure = name => require(`../../structures/${name}`);
|
||||
const User = requireStructure('User');
|
||||
@@ -720,6 +721,36 @@ class RESTMethods {
|
||||
.catch(reject);
|
||||
});
|
||||
}
|
||||
|
||||
addMessageReaction(channelID, messageID, emoji) {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.rest.makeRequest('put', Constants.Endpoints.selfMessageReaction(channelID, messageID, emoji), true)
|
||||
.then(() => {
|
||||
resolve(this.rest.client.actions.MessageReactionAdd.handle({
|
||||
user_id: this.rest.client.user.id,
|
||||
message_id: messageID,
|
||||
emoji: parseEmoji(emoji),
|
||||
channel_id: channelID,
|
||||
}).reaction);
|
||||
})
|
||||
.catch(reject);
|
||||
});
|
||||
}
|
||||
|
||||
removeMessageReaction(channelID, messageID, emoji) {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.rest.makeRequest('delete', Constants.Endpoints.selfMessageReaction(channelID, messageID, emoji), true)
|
||||
.then(() => {
|
||||
resolve(this.rest.client.actions.MessageReactionRemove.handle({
|
||||
user_id: this.rest.client.user.id,
|
||||
message_id: messageID,
|
||||
emoji: parseEmoji(emoji),
|
||||
channel_id: channelID,
|
||||
}).reaction);
|
||||
})
|
||||
.catch(reject);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = RESTMethods;
|
||||
|
||||
@@ -44,6 +44,8 @@ class WebSocketPacketManager {
|
||||
this.register(Constants.WSEvents.GUILD_SYNC, 'GuildSync');
|
||||
this.register(Constants.WSEvents.RELATIONSHIP_ADD, 'RelationshipAdd');
|
||||
this.register(Constants.WSEvents.RELATIONSHIP_REMOVE, 'RelationshipRemove');
|
||||
this.register(Constants.WSEvents.MESSAGE_REACTION_ADD, 'MessageReactionAdd');
|
||||
this.register(Constants.WSEvents.MESSAGE_REACTION_REMOVE, 'MessageReactionRemove');
|
||||
}
|
||||
|
||||
get client() {
|
||||
|
||||
11
src/client/websocket/packets/handlers/MessageReactionAdd.js
Normal file
11
src/client/websocket/packets/handlers/MessageReactionAdd.js
Normal file
@@ -0,0 +1,11 @@
|
||||
const AbstractHandler = require('./AbstractHandler');
|
||||
|
||||
class MessageReactionAddHandler extends AbstractHandler {
|
||||
handle(packet) {
|
||||
const client = this.packetManager.client;
|
||||
const data = packet.d;
|
||||
client.actions.MessageReactionAdd.handle(data);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = MessageReactionAddHandler;
|
||||
@@ -0,0 +1,11 @@
|
||||
const AbstractHandler = require('./AbstractHandler');
|
||||
|
||||
class MessageReactionRemove extends AbstractHandler {
|
||||
handle(packet) {
|
||||
const client = this.packetManager.client;
|
||||
const data = packet.d;
|
||||
client.actions.MessageReactionRemove.handle(data);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = MessageReactionRemove;
|
||||
Reference in New Issue
Block a user