mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-09 16:13:31 +01:00
Add MessageUpdate handling
This commit is contained in:
@@ -39,6 +39,7 @@ class WebSocketPacketManager {
|
||||
this.register(Constants.WSEvents.TYPING_START, 'TypingStart');
|
||||
this.register(Constants.WSEvents.MESSAGE_CREATE, 'MessageCreate');
|
||||
this.register(Constants.WSEvents.MESSAGE_DELETE, 'MessageDelete');
|
||||
this.register(Constants.WSEvents.MESSAGE_UPDATE, 'MessageUpdate');
|
||||
}
|
||||
|
||||
get client() {
|
||||
|
||||
35
src/client/websocket/packets/handlers/MessageUpdate.js
Normal file
35
src/client/websocket/packets/handlers/MessageUpdate.js
Normal file
@@ -0,0 +1,35 @@
|
||||
'use strict';
|
||||
|
||||
const AbstractHandler = require('./AbstractHandler');
|
||||
const Structure = name => require(`../../../../structures/${name}`);
|
||||
const Constants = require('../../../../util/Constants');
|
||||
const CloneObject = require('../../../../util/CloneObject');
|
||||
|
||||
const Message = Structure('Message');
|
||||
const Guild = Structure('Guild');
|
||||
|
||||
class MessageUpdateHandler extends AbstractHandler {
|
||||
|
||||
constructor(packetManager) {
|
||||
super(packetManager);
|
||||
}
|
||||
|
||||
handle(packet) {
|
||||
let data = packet.d;
|
||||
let client = this.packetManager.client;
|
||||
let channel = client.store.get('channels', data.channel_id);
|
||||
|
||||
if (channel) {
|
||||
let message = channel.store.get('messages', data.id);
|
||||
if (message) {
|
||||
let oldMessage = CloneObject(message);
|
||||
message.patch(data);
|
||||
client.emit(Constants.Events.MESSAGE_UPDATE, oldMessage, message);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
module.exports = MessageUpdateHandler;
|
||||
@@ -28,6 +28,38 @@ class Message {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
patch(data) {
|
||||
if (data.author)
|
||||
this.author = this.guild.client.store.get('users', data.author.id);
|
||||
if (data.content)
|
||||
this.content = data.content;
|
||||
if (data.timestamp)
|
||||
this.timestamp = new Date(data.timestamp);
|
||||
if (data.edited_timestamp)
|
||||
this.editedTimestamp = data.edited_timestamp ? new Date(data.edited_timestamp) : null;
|
||||
if (data.tts)
|
||||
this.tts = data.tts;
|
||||
if (data.mention_everyone)
|
||||
this.mentionEveryone = data.mention_everyone;
|
||||
if (data.nonce)
|
||||
this.nonce = data.nonce;
|
||||
if (data.embeds)
|
||||
this.embeds = data.embeds;
|
||||
if (data.attachments)
|
||||
this.attachments = data.attachments;
|
||||
if (data.mentions) {
|
||||
for (let mention of data.mentions) {
|
||||
let user = this.guild.client.store.get('users', mention.id);
|
||||
if (user) {
|
||||
this.mentions.push(user);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (data.id)
|
||||
this.id = data.id;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Message;
|
||||
|
||||
@@ -11,7 +11,7 @@ const DefaultOptions = exports.DefaultOptions = {
|
||||
},
|
||||
},
|
||||
protocol_version: 4,
|
||||
max_message_cache: 20,
|
||||
max_message_cache: 200,
|
||||
};
|
||||
|
||||
const Package = exports.Package = require('../../package.json');
|
||||
@@ -101,6 +101,7 @@ const Events = exports.Events = {
|
||||
GUILD_MEMBERS_CHUNK: 'guildMembersChunk',
|
||||
MESSAGE_CREATE: 'message',
|
||||
MESSAGE_DELETE: 'messageDelete',
|
||||
MESSAGE_UPDATE: 'messageUpdate',
|
||||
};
|
||||
|
||||
const WSEvents = exports.WSEvents = {
|
||||
|
||||
@@ -75,3 +75,8 @@ client.on('message', message => {
|
||||
client.on('messageDelete', message => {
|
||||
console.log('Message deleted by', message.author.username);
|
||||
});
|
||||
|
||||
client.on('messageUpdate', (old, message) => {
|
||||
if (message.author.username === 'hydrabolt')
|
||||
console.log('Message updated from', old.content, 'to', message.content);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user