Add MessageUpdate handling

This commit is contained in:
hydrabolt
2016-04-18 18:12:57 +01:00
parent d437fd31cf
commit 00327a5776
5 changed files with 75 additions and 1 deletions

View File

@@ -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() {

View 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;

View File

@@ -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;

View File

@@ -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 = {

View File

@@ -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);
});