mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-15 11:03:30 +01:00
Start work on adding reaction support
This commit is contained in:
@@ -6,6 +6,8 @@ class ActionsManager {
|
|||||||
this.register('MessageDelete');
|
this.register('MessageDelete');
|
||||||
this.register('MessageDeleteBulk');
|
this.register('MessageDeleteBulk');
|
||||||
this.register('MessageUpdate');
|
this.register('MessageUpdate');
|
||||||
|
this.register('MessageReactionAdd');
|
||||||
|
this.register('MessageReactionRemove');
|
||||||
this.register('ChannelCreate');
|
this.register('ChannelCreate');
|
||||||
this.register('ChannelDelete');
|
this.register('ChannelDelete');
|
||||||
this.register('ChannelUpdate');
|
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 Constants = require('../../util/Constants');
|
||||||
const Collection = require('../../util/Collection');
|
const Collection = require('../../util/Collection');
|
||||||
const splitMessage = require('../../util/SplitMessage');
|
const splitMessage = require('../../util/SplitMessage');
|
||||||
|
const parseEmoji = require('../../util/ParseEmoji');
|
||||||
|
|
||||||
const requireStructure = name => require(`../../structures/${name}`);
|
const requireStructure = name => require(`../../structures/${name}`);
|
||||||
const User = requireStructure('User');
|
const User = requireStructure('User');
|
||||||
@@ -720,6 +721,36 @@ class RESTMethods {
|
|||||||
.catch(reject);
|
.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;
|
module.exports = RESTMethods;
|
||||||
|
|||||||
@@ -44,6 +44,8 @@ class WebSocketPacketManager {
|
|||||||
this.register(Constants.WSEvents.GUILD_SYNC, 'GuildSync');
|
this.register(Constants.WSEvents.GUILD_SYNC, 'GuildSync');
|
||||||
this.register(Constants.WSEvents.RELATIONSHIP_ADD, 'RelationshipAdd');
|
this.register(Constants.WSEvents.RELATIONSHIP_ADD, 'RelationshipAdd');
|
||||||
this.register(Constants.WSEvents.RELATIONSHIP_REMOVE, 'RelationshipRemove');
|
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() {
|
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;
|
||||||
@@ -3,6 +3,7 @@ const Embed = require('./MessageEmbed');
|
|||||||
const Collection = require('../util/Collection');
|
const Collection = require('../util/Collection');
|
||||||
const Constants = require('../util/Constants');
|
const Constants = require('../util/Constants');
|
||||||
const escapeMarkdown = require('../util/EscapeMarkdown');
|
const escapeMarkdown = require('../util/EscapeMarkdown');
|
||||||
|
const MessageReaction = require('./MessageReaction');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents a Message on Discord
|
* Represents a Message on Discord
|
||||||
@@ -148,6 +149,42 @@ class Message {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this._edits = [];
|
this._edits = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A collection of Reactions to this Message, mapped by the reaction "id".
|
||||||
|
* @type {Collection<string, MessageReaction>}
|
||||||
|
*/
|
||||||
|
this.reactions = new Collection();
|
||||||
|
}
|
||||||
|
|
||||||
|
_addReaction(emoji, user) {
|
||||||
|
const emojiID = emoji.id || emoji;
|
||||||
|
let reaction;
|
||||||
|
if (this.reactions.has(emojiID)) {
|
||||||
|
reaction = this.reactions.get(emojiID);
|
||||||
|
} else {
|
||||||
|
reaction = new MessageReaction(this, emoji, 0);
|
||||||
|
this.reactions.set(emojiID, reaction);
|
||||||
|
}
|
||||||
|
if (!reaction.users.has(user.id)) {
|
||||||
|
reaction.users.set(user.id, user);
|
||||||
|
reaction.count++;
|
||||||
|
return reaction;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
_removeReaction(emoji, user) {
|
||||||
|
const emojiID = emoji.id || emoji;
|
||||||
|
if (this.reactions.has(emojiID)) {
|
||||||
|
const reaction = this.reactions.get(emojiID);
|
||||||
|
if (reaction.users.has(user.id)) {
|
||||||
|
reaction.users.delete(user.id);
|
||||||
|
reaction.count--;
|
||||||
|
return reaction;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
patch(data) { // eslint-disable-line complexity
|
patch(data) { // eslint-disable-line complexity
|
||||||
@@ -266,7 +303,11 @@ class Message {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
addReaction(emoji) {
|
||||||
|
return this.client.rest.methods.addMessageReaction(this.channel.id, this.id, emoji);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
* An array of cached versions of the message, including the current version.
|
* An array of cached versions of the message, including the current version.
|
||||||
* Sorted from latest (first) to oldest (last).
|
* Sorted from latest (first) to oldest (last).
|
||||||
* @type {Message[]}
|
* @type {Message[]}
|
||||||
|
|||||||
41
src/structures/MessageReaction.js
Normal file
41
src/structures/MessageReaction.js
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
const Collection = require('../util/Collection');
|
||||||
|
const parseEmoji = require('../util/ParseEmoji');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a reaction to a message
|
||||||
|
*/
|
||||||
|
class MessageReaction {
|
||||||
|
constructor(message, emoji, count) {
|
||||||
|
/**
|
||||||
|
* The message that this reaction refers to
|
||||||
|
* @type {Message}
|
||||||
|
*/
|
||||||
|
this.message = message;
|
||||||
|
/**
|
||||||
|
* The emoji of this reaction, if this is a string it is a plain unicode emoji.
|
||||||
|
* @type {Emoji}
|
||||||
|
*/
|
||||||
|
this.emoji = emoji;
|
||||||
|
/**
|
||||||
|
* The number of people that have given the same reaction.
|
||||||
|
* @type {number}
|
||||||
|
*/
|
||||||
|
this.count = count || 0;
|
||||||
|
/**
|
||||||
|
* The users that have given this reaction, mapped by their ID.
|
||||||
|
* @type {Collection<string, User>}
|
||||||
|
*/
|
||||||
|
this.users = new Collection();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If the client has given this reaction to a message, it is removed.
|
||||||
|
* @returns {Promise<MessageReaction>}
|
||||||
|
*/
|
||||||
|
remove() {
|
||||||
|
const message = this.message;
|
||||||
|
return message.client.rest.methods.removeMessageReaction(message.channel.id, message.id, parseEmoji(this.emoji));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = MessageReaction;
|
||||||
@@ -115,6 +115,15 @@ const Endpoints = exports.Endpoints = {
|
|||||||
channelMessage: (channelID, messageID) => `${Endpoints.channelMessages(channelID)}/${messageID}`,
|
channelMessage: (channelID, messageID) => `${Endpoints.channelMessages(channelID)}/${messageID}`,
|
||||||
channelWebhooks: (channelID) => `${Endpoints.channel(channelID)}/webhooks`,
|
channelWebhooks: (channelID) => `${Endpoints.channel(channelID)}/webhooks`,
|
||||||
|
|
||||||
|
// message reactions
|
||||||
|
messageReactions: (channelID, messageID) => `${Endpoints.channelMessage(channelID, messageID)}/reactions`,
|
||||||
|
messageReaction:
|
||||||
|
(channel, msg, emoji, limit) =>
|
||||||
|
`${Endpoints.messageReactions(channel, msg)}/${emoji}` +
|
||||||
|
`${limit ? `?limit=${limit}` : ''}`,
|
||||||
|
selfMessageReaction: (channel, msg, emoji, limit) =>
|
||||||
|
`${Endpoints.messageReaction(channel, msg, emoji, limit)}/@me`,
|
||||||
|
|
||||||
// webhooks
|
// webhooks
|
||||||
webhook: (webhookID, token) => `${API}/webhooks/${webhookID}${token ? `/${token}` : ''}`,
|
webhook: (webhookID, token) => `${API}/webhooks/${webhookID}${token ? `/${token}` : ''}`,
|
||||||
};
|
};
|
||||||
@@ -187,6 +196,8 @@ exports.Events = {
|
|||||||
MESSAGE_DELETE: 'messageDelete',
|
MESSAGE_DELETE: 'messageDelete',
|
||||||
MESSAGE_UPDATE: 'messageUpdate',
|
MESSAGE_UPDATE: 'messageUpdate',
|
||||||
MESSAGE_BULK_DELETE: 'messageDeleteBulk',
|
MESSAGE_BULK_DELETE: 'messageDeleteBulk',
|
||||||
|
MESSAGE_REACTION_ADD: 'messageReactionAdd',
|
||||||
|
MESSAGE_REACTION_REMOVE: 'messageReactionRemove',
|
||||||
USER_UPDATE: 'userUpdate',
|
USER_UPDATE: 'userUpdate',
|
||||||
PRESENCE_UPDATE: 'presenceUpdate',
|
PRESENCE_UPDATE: 'presenceUpdate',
|
||||||
VOICE_STATE_UPDATE: 'voiceStateUpdate',
|
VOICE_STATE_UPDATE: 'voiceStateUpdate',
|
||||||
@@ -222,6 +233,8 @@ exports.WSEvents = {
|
|||||||
MESSAGE_DELETE: 'MESSAGE_DELETE',
|
MESSAGE_DELETE: 'MESSAGE_DELETE',
|
||||||
MESSAGE_UPDATE: 'MESSAGE_UPDATE',
|
MESSAGE_UPDATE: 'MESSAGE_UPDATE',
|
||||||
MESSAGE_DELETE_BULK: 'MESSAGE_DELETE_BULK',
|
MESSAGE_DELETE_BULK: 'MESSAGE_DELETE_BULK',
|
||||||
|
MESSAGE_REACTION_ADD: 'MESSAGE_REACTION_ADD',
|
||||||
|
MESSAGE_REACTION_REMOVE: 'MESSAGE_REACTION_REMOVE',
|
||||||
USER_UPDATE: 'USER_UPDATE',
|
USER_UPDATE: 'USER_UPDATE',
|
||||||
PRESENCE_UPDATE: 'PRESENCE_UPDATE',
|
PRESENCE_UPDATE: 'PRESENCE_UPDATE',
|
||||||
VOICE_STATE_UPDATE: 'VOICE_STATE_UPDATE',
|
VOICE_STATE_UPDATE: 'VOICE_STATE_UPDATE',
|
||||||
|
|||||||
11
src/util/parseEmoji.js
Normal file
11
src/util/parseEmoji.js
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
module.exports = function parseEmoji(text) {
|
||||||
|
if (text.includes(':')) {
|
||||||
|
const [name, id] = text.split(':');
|
||||||
|
return { name, id };
|
||||||
|
} else {
|
||||||
|
return {
|
||||||
|
name: text,
|
||||||
|
id: null,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -20,7 +20,7 @@ client.on('userUpdate', (o, n) => {
|
|||||||
console.log(o.username, n.username);
|
console.log(o.username, n.username);
|
||||||
});
|
});
|
||||||
|
|
||||||
client.on('guildMemberAdd', (g, m) => console.log(`${m.user.username} joined ${g.name}`));
|
client.on('guildMemberAdd', m => console.log(`${m.user.username} joined ${m.guild.name}`));
|
||||||
|
|
||||||
client.on('channelCreate', channel => {
|
client.on('channelCreate', channel => {
|
||||||
console.log(`made ${channel.name}`);
|
console.log(`made ${channel.name}`);
|
||||||
@@ -176,4 +176,14 @@ client.on('message', msg => {
|
|||||||
})
|
})
|
||||||
.catch(console.error);
|
.catch(console.error);
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
|
|
||||||
|
client.on('messageReactionAdd', (reaction, user) => {
|
||||||
|
if (reaction.message.channel.id !== '222086648706498562') return;
|
||||||
|
reaction.message.channel.sendMessage(`${user.username} added reaction ${reaction.emoji}, count is now ${reaction.count}`);
|
||||||
|
});
|
||||||
|
|
||||||
|
client.on('messageReactionRemove', (reaction, user) => {
|
||||||
|
if (reaction.message.channel.id !== '222086648706498562') return;
|
||||||
|
reaction.message.channel.sendMessage(`${user.username} removed reaction ${reaction.emoji}, count is now ${reaction.count}`);
|
||||||
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user